I just wrote a for-loop like this:
string foo="gazonk";
for(int i=0;i<sizeof(foo);i++) {
... do stuff ...
if (sometimes) foo = foo[1..];
... do stuff ...
}
The problem I discoverd was that when the length of foo is changed, the loop still tries to loop over the original length of foo. The problem dissappeard when I replace sizeof(foo) with a variable and update it when changing the length of foo.
Is this the correct behaviour or is the optimizer doing more than it should here?
I would say it's the correct behaviour.
/ Peter Bortas
Previous text:
2003-03-20 10:26: Subject: for-loop optimization
I just wrote a for-loop like this:
string foo="gazonk";
for(int i=0;i<sizeof(foo);i++) {
... do stuff ...
if (sometimes) foo = foo[1..];
... do stuff ...
}
The problem I discoverd was that when the length of foo is changed, the loop still tries to loop over the original length of foo. The problem dissappeard when I replace sizeof(foo) with a variable and update it when changing the length of foo.
Is this the correct behaviour or is the optimizer doing more than it should here?
/ Marcus Agehall (Trådlös)
Why? The input to sizeof isn't constant. It's not even the same referenced array here. You might just as well expand any '<' operation to a constant.
/ Mirar
Previous text:
2003-03-20 10:37: Subject: for-loop optimization
Neither has to bee, but sizeof(foo) should expand to a constant.
/ Peter Bortas
That seems like a bug to me. for (a;b;c) d; should always expand to something like
| a; | while (b) | { | d; | c; | }
in my opinion.
/ Mirar
Previous text:
2003-03-20 10:26: Subject: for-loop optimization
I just wrote a for-loop like this:
string foo="gazonk";
for(int i=0;i<sizeof(foo);i++) {
... do stuff ...
if (sometimes) foo = foo[1..];
... do stuff ...
}
The problem I discoverd was that when the length of foo is changed, the loop still tries to loop over the original length of foo. The problem dissappeard when I replace sizeof(foo) with a variable and update it when changing the length of foo.
Is this the correct behaviour or is the optimizer doing more than it should here?
/ Marcus Agehall (Trådlös)
In my opinion sizeof(b) should always be expanded to a constant b2 before doing anything else.
/ Peter Bortas
Previous text:
2003-03-20 10:32: Subject: for-loop optimization
That seems like a bug to me. for (a;b;c) d; should always expand to something like
| a; | while (b) | { | d; | c; | }
in my opinion.
/ Mirar
Really?
array fifo=({}); while (sizeof(fifo)<20) { fifo+=({getstuff()}); }
That loop would never exit.
/ Mirar
Previous text:
2003-03-20 10:36: Subject: for-loop optimization
In my opinion sizeof(b) should always be expanded to a constant b2 before doing anything else.
/ Peter Bortas
I'll admit it is inconsitent, but I should have said "sizeof(b) should always be expanded to a constant b2 in the for expression". That is becouse for some reason I expect the optimizer to give me a set number of loops. I'll admit that it might not be as my mind makes it. ;-)
/ Peter Bortas
Previous text:
2003-03-20 10:39: Subject: for-loop optimization
Really?
array fifo=({}); while (sizeof(fifo)<20) { fifo+=({getstuff()}); }
That loop would never exit.
/ Mirar
Expressions should *never* expand to constants unless they *are* constants. I suppose that a good optimizer could see if 'foo' were being changed in the loop, though.
Anyway, I can't repeat the problem:
int a=0; array foo=({1,2,3}); for (a=0; a<sizeof(foo); a++) { werror("%O\n",foo[a]); foo=foo[1..]; }
This happily outputs 1, 3 and exits for me.
/ Mirar
Previous text:
2003-03-20 10:47: Subject: for-loop optimization
I'll admit it is inconsitent, but I should have said "sizeof(b) should always be expanded to a constant b2 in the for expression". That is becouse for some reason I expect the optimizer to give me a set number of loops. I'll admit that it might not be as my mind makes it. ;-)
/ Peter Bortas
It's incorrect. But often a very useful optimization, since code that loops over sizeof(X) is rather common, and this speeds it up a factor of 2 or more.
We need dependency analysis.
/ Per Hedbor ()
Previous text:
2003-03-20 10:26: Subject: for-loop optimization
I just wrote a for-loop like this:
string foo="gazonk";
for(int i=0;i<sizeof(foo);i++) {
... do stuff ...
if (sometimes) foo = foo[1..];
... do stuff ...
}
The problem I discoverd was that when the length of foo is changed, the loop still tries to loop over the original length of foo. The problem dissappeard when I replace sizeof(foo) with a variable and update it when changing the length of foo.
Is this the correct behaviour or is the optimizer doing more than it should here?
/ Marcus Agehall (Trådlös)
And subexpression elimination.
/ Mirar
Previous text:
2003-03-20 10:57: Subject: for-loop optimization
It's incorrect. But often a very useful optimization, since code that loops over sizeof(X) is rather common, and this speeds it up a factor of 2 or more.
We need dependency analysis.
/ Per Hedbor ()
I don't believe you.
$ cat bug_l9908261.pike int main(int argc, array(string) argv) { string foo="gazonk"; int i;
for(i=0;i<sizeof(foo);i++) { if (i%2) { foo = foo[1..]; } } werror("i:%O\n", i); } $ ./pike -mmaster.pike bug_l9908261.pike i:4
Please provide an example that actually triggers the alleged bug.
/ Henrik Grubbström (Lysator)
Previous text:
2003-03-20 10:26: Subject: for-loop optimization
I just wrote a for-loop like this:
string foo="gazonk";
for(int i=0;i<sizeof(foo);i++) {
... do stuff ...
if (sometimes) foo = foo[1..];
... do stuff ...
}
The problem I discoverd was that when the length of foo is changed, the loop still tries to loop over the original length of foo. The problem dissappeard when I replace sizeof(foo) with a variable and update it when changing the length of foo.
Is this the correct behaviour or is the optimizer doing more than it should here?
/ Marcus Agehall (Trådlös)
I can drop by the Pike-lab in about an hour and show you. I can't seem to minimize the test-case, so it might be something else.
/ Marcus Agehall (Trådlös)
Previous text:
2003-03-20 12:50: Subject: for-loop optimization
I don't believe you.
$ cat bug_l9908261.pike int main(int argc, array(string) argv) { string foo="gazonk"; int i;
for(i=0;i<sizeof(foo);i++) { if (i%2) { foo = foo[1..]; } } werror("i:%O\n", i); } $ ./pike -mmaster.pike bug_l9908261.pike i:4
Please provide an example that actually triggers the alleged bug.
/ Henrik Grubbström (Lysator)
Pike version?
/ Johan Sundström (folkskådare)
Previous text:
2003-03-20 10:26: Subject: for-loop optimization
I just wrote a for-loop like this:
string foo="gazonk";
for(int i=0;i<sizeof(foo);i++) {
... do stuff ...
if (sometimes) foo = foo[1..];
... do stuff ...
}
The problem I discoverd was that when the length of foo is changed, the loop still tries to loop over the original length of foo. The problem dissappeard when I replace sizeof(foo) with a variable and update it when changing the length of foo.
Is this the correct behaviour or is the optimizer doing more than it should here?
/ Marcus Agehall (Trådlös)
Ok, with an actual sample of code that broke it was easy to fix. The bug was not in the optimization rule itself, which was sound, but in the dependency analyser. It didn't check arguments to side-effect free functions for written variables. Fixed in Pike 7.0, 7.2, 7.4 and 7.5.
/ Henrik Grubbström (Lysator)
Previous text:
2003-03-20 10:26: Subject: for-loop optimization
I just wrote a for-loop like this:
string foo="gazonk";
for(int i=0;i<sizeof(foo);i++) {
... do stuff ...
if (sometimes) foo = foo[1..];
... do stuff ...
}
The problem I discoverd was that when the length of foo is changed, the loop still tries to loop over the original length of foo. The problem dissappeard when I replace sizeof(foo) with a variable and update it when changing the length of foo.
Is this the correct behaviour or is the optimizer doing more than it should here?
/ Marcus Agehall (Trådlös)
pike-devel@lists.lysator.liu.se