473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

Multiple assignments simplification

The current version of ShedSkin (http://shedskin.sourceforge.net/
experimental Python to C++ compiler) is rather alpha, and the
development work is focused on debugging and implementing some more
basic Python functionality. But hopefully in future versions more work
will be spent to make the resulting C++ programs as fast as possible.

One of the many possible details that may be improved, is the compiling
to C++ of the Python "parallel" assignments. There are complex
situations like this one:

| a = 1
| b = 2
| c = 3
| def fun(a):
| global b
| return c, b ** (b+a)
| (a, b), c = fun(a), a
| print a, b, c # prints 3 8 1
Probably it isn't necessary to find an optimal solution for complex
situations like this one, ShedSkin (SS) uses a basic and simple
algorithm to translate all the complex cases.

But maybe for simpler and more regular situations it can be useful to
find better/optimal solution, like with a swap:

a = 1
b = 2
a, b = b, a

At the moment SS translates it as something like:

int __0, __1, a, b;
int __main() {
a = 1;
b = 2;
__0 = b;
__1 = a;
a = __0;
b = __1;

SS just copies all the variables before the assignment.
If such swap is inside a very long loop, then maybe a simplification
can speed up a program a little (I don't know if C++ compilers can do
such optimizations).

This is another example of such "regular" situations:

a, b, c, d, e, f = range(6)
a, b, c, d, e = b, d, e, f, c
print a, b, c, d, e, f
At the moment SS translates its central part just as:

__1 = b;
__2 = d;
__3 = e;
__4 = f;
__5 = c;
a = __1;
b = __2;
c = __3;
d = __4;
e = __5;

The two sides of the assignment aren't just permutations, because some
variables can be different (like f), and some variables can be present
two or more times (duplication), some other can be absent.
A code like this can be faster (and hopefully still correct):

a = b
aux_1 = c
c = e
e = aux_1
b = d
d = f

That assignment line of code can be represented as:
[0, 1, 2, 3, 4], [1, 3, 4, 5, 2]
(Numbers represent variables. The first list is always sorted,
equivalent to range(n) ).

Do you know some algorithm (or you can give some suggestions) to
minimize the number of simple assignments needed for a "regular"
situation like that?

Note that in most situations the number of variables is quite small
(but it can be big).
(Also note that in the "regular" situation I've ignored the problem of
mixed variable types, this is something SS has to take care too).

Bye and thank you,
bearophile

Oct 13 '05 #1
3 2099
<be************@lycos.com> wrote:
[snipped]

Do you know some algorithm (or you can give some suggestions) to
minimize the number of simple assignments needed for a "regular"
situation like that?


You can formulate the task as a graph-theoretic problem by representing the set of assignments as a
digraph G(V,E), where:
- V = set(LHS) | set(RHS): the vertex set V is the union of all left and right hand side
expressions.
- E = set((v1,v2) for "v1 = v2" in assignments): there is an edge from v1 to v2 for every assignment
v1=v2.

Now, every edge v1->v2 where in-degree(v1)==0 corresponds to a safe assignment: v1 is not assigned
to any RHS, so it can be overwritten. After the assignment, both v1 and the edge (v1,v2) can be
removed, decrementing the in-degree of v2. This happens iteratively as long as there are nodes with
zero in-degree.

At this point, all remaining nodes have in-degree >=1 and they form one or more strongly connected
components. Since no RHS occurs more than once*, the out-degree of every vertex is less than or
equal to 1. Therefore, for every component Ci,
|Vi| >= sum(out-degree(v) for v in Vi) == |Ei|.

Since for a strongly connected component |Vi| <= |Ei|, the previous relationship is actually
equality |Vi| == |Ei|. Thus each component is a simple cycle v[1]->v[2]->...v[n]->v[1]. You can
break the cycle by introducing an auxiliary variable x in an arbitrary edge, say v[n]->v[1]. Then
the following assignments can take place: x = v[1]; v[1] = v[2]; v[2] = v[3]; ...; v[n-1] = v[n];
v[n] = x

So overall, you need just one auxiliary variable for each strongly component of G.

HTH,
George
* More than one assignments with the same RHS [v=a, v=b] are useless since only the last has an
effect. In any case, the useless assignments can be filtered out in the beginning.
Oct 13 '05 #2
be************@lycos.com wrote:
I don't know if C++ compilers can do such optimizations.


working on a Python to C/C++ translator without knowing what kind
of optimizations a C/C++ compiler can do for you sounds like a great
way to waste your time...

(I would be rather bit surprised if any contemporary C or C++ compiler
didn't generate optimal machine code for source code that contains swaps
like the one you posted. it won't look at just the swap statement, however;
the interesting thing is where the values came from, and what you're doing
with the values later on. minimizing the number of assignment statements in
the swap translation won't change a thing...)

</F>

Oct 13 '05 #3
Thank you George Sakkis for your fast and accurate answer. In my life I
am encountering lot of graph-based solutions to my problems. I'll try
to implement your solution as soon as possible.
Fredrik Lundh>working on a Python to C/C++ translator without knowing
what kind of optimizations a C/C++ compiler can do for you sounds like
a great way to waste your time...<

I am not the author of ShedSkin (Mark Dufour), I'm ignorant, he is much
more expert than me about C++. So the possibile waste of time is just
mine.

Some experimental timings have shown me that different C++ compilers
have different optimization capabilities, and such experiments show me
that sometimes they (g++ of MinGW) aren't capable of doing some
"obvious" optimizations (I haven't tested the assignments yet).
Fredrik Lundh>the interesting thing is where the values came from, and
what you're doing with the values later on. minimizing the number of
assignment statements in the swap translation won't change a thing...)<

I see. (It's just a detail, as I've said...)
If you Fredrik have some free time and you are interested, you can
probably help Mark Dufour improve that compiler. You can just email the
author or post things in the Sourceforge site, etc.

Bear hugs,
bearophile

Oct 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ben Hallert | last post by:
Hi guys, I'm trying to figure out what bone headed mistake I made on something I put together. I've got a form (named 'context') that has a variable number of select-multiple inputs on it. ...
3
by: michaelnewport | last post by:
Greetings, I have what seems a simple problem.... I want to save the output from the following queries in 1 file in 'SQL Query Analyser', but I can only save each 'grid' separately. I have...
0
by: Nagib Abi Fadel | last post by:
Hi, i'm getting the following error when updating a view : ERROR: Multiple assignments to same attribute "send_sco" When doing the same query directly on the table i'm not getting any error....
1
by: moondaddy | last post by:
This is a basic question but I cant seem to find the answer anywhere and have never tried it before. Here's one of many syntax I'm trying, but should at least show you what I'm trying to do: ...
2
by: Diego | last post by:
Hi everybody! I'm using DB2 PE v8.2.3 for linux. I've defined a database with the following schema: ANNOTATION(ID,AUTHOR,TEXT) ANNOTATION_BOOK(ANNOTATION_ID,OBJECT_ID)...
6
by: Suresh Jeevanandam | last post by:
Dear all, I read in "Python in a Nutshell" that when we have multiple assignments made on a single line, it is equivalent to have those many simple assignments and that the right side is evaluated...
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
21
by: vlsidesign | last post by:
This syntax does not to work nl, nt, ns = 0; The only one that get's initialized is ns. nl and nt because they don't initialize seem to get some junk from memory. I have done these two...
3
by: Bob | last post by:
Hi, I have a newbie question on event listening. I have a form that is monitoring progress in an application. The application consists of many classes, multithreaded, etc. The form listens for...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.