Closed p5pRT closed 19 years ago
sorting a list of numbers as integers (via 'use integer') in descending order gives an unstable sort:
$ perl
use sort stable; @a=(2.7\, 1.7\, 1\, 4.8\, 4.3\, 0.8); use integer; @u = sort { $b \<=> $a } @a; @s = sort { int $b \<=> int $a } @a;
sub pa { print join("\, "\, @_)\, "\n"} pa @a; pa @s; pa @u'
__END__
2.7\, 1.7\, 1\, 4.8\, 4.3\, 0.8 4.8\, 4.3\, 2.7\, 1.7\, 1\, 0.8 4.3\, 4.8\, 2.7\, 1\, 1.7\, 0.8
Cheers\,
- Salvador.
I have found it is not only affecting integer sorts:
$ perl
use sort stable;
@a=map{(int rand 5).(q(#)\, q(@))[rand 2]} 0..5; @u = sort { $b \<=> $a } @a; @s = sort { int $b \<=> int $a } @a;
sub pa { print join("\, "\, @_)\, "\n"} pa @a; pa @s; pa @u'
__END__
2#\, 2@\, 1#\, 3@\, 3#\, 1@ 3@\, 3#\, 2#\, 2@\, 1#\, 1@ 3#\, 3@\, 2@\, 2#\, 1@\, 1#
@salva - Status changed from 'new' to 'open'
perlbug-followup@perl.org wrote:
# New Ticket Created by Salvador Fandiño # Please include the string: [perl #36350] # in the subject line of all future correspondence about this issue. # \<URL: https://rt-archive.perl.org/perl5/Ticket/Display.html?id=36350 >
This is a bug report for perl from sfandino@yahoo.com\, generated with the help of perlbug 1.35 running under perl v5.8.5.
----------------------------------------------------------------- [Please enter your report here]
sorting a list of numbers as integers (via 'use integer') in descending order gives an unstable sort:
$ perl
use sort stable; @a=(2.7\, 1.7\, 1\, 4.8\, 4.3\, 0.8); use integer; @u = sort { $b \<=> $a } @a; @s = sort { int $b \<=> int $a } @a;
sub pa { print join("\, "\, @_)\, "\n"} pa @a; pa @s; pa @u'
__END__
2.7\, 1.7\, 1\, 4.8\, 4.3\, 0.8 4.8\, 4.3\, 2.7\, 1.7\, 1\, 0.8 4.3\, 4.8\, 2.7\, 1\, 1.7\, 0.8
Cheers\,
- Salvador.
I have not been able to reproduce the bug on any perl compiled by myself\, but anyway\, I am sure the problem is caused by the nasty macro SORTHINTS(hintsv)\, that relays on the evaluation order of something like
result = (v = foo()\, bar(v));
The patch attached breaks the macro in two: dSORTHINTS and SORTHINTS\, they do the same operation but in two steps.
Cheers\,
- Salvador.
On Tue\, Jun 21\, 2005 at 07:50:35PM +0200\, Salvador Fandino wrote:
I have not been able to reproduce the bug on any perl compiled by myself\, but anyway\, I am sure the problem is caused by the nasty macro SORTHINTS(hintsv)\, that relays on the evaluation order of something like
result = (v = foo()\, bar(v));
The patch attached breaks the macro in two: dSORTHINTS and SORTHINTS\, they do the same operation but in two steps.
I think the evaulation order here is quite well defined but\, of course\, perl has a fine tradition of working around broken compilers.
- /* Sun's Compiler (cc: WorkShop Compilers 4.2 30 Oct 1996 C 4.2) used - to miscompile this function under optimization -O. If you get test - errors related to picking the correct sort() function\, try recompiling - this file without optimiziation. -- A.D. 4/2002. - */
So yes\, hopefully this is fixed too but it would be interesting to hear from someone who can test under this compiler.
-- Paul Johnson - paul@pjcj.net http://www.pjcj.net
On Wed\, 22 Jun 2005\, Paul Johnson wrote:
On Tue\, Jun 21\, 2005 at 07:50:35PM +0200\, Salvador Fandino wrote:
I have not been able to reproduce the bug on any perl compiled by myself\, but anyway\, I am sure the problem is caused by the nasty macro SORTHINTS(hintsv)\, that relays on the evaluation order of something like
result = (v = foo()\, bar(v));
The patch attached breaks the macro in two: dSORTHINTS and SORTHINTS\, they do the same operation but in two steps.
I think the evaulation order here is quite well defined but\, of course\, perl has a fine tradition of working around broken compilers.
- /* Sun's Compiler (cc: WorkShop Compilers 4.2 30 Oct 1996 C 4.2) used - to miscompile this function under optimization -O. If you get test - errors related to picking the correct sort() function\, try recompiling - this file without optimiziation. -- A.D. 4/2002. - */
So yes\, hopefully this is fixed too but it would be interesting to hear from someone who can test under this compiler.
That'd be me. What\, precisely\, would you like me to test? I haven't had any recent problems with that compiler under -O\, so as far as I know\, but I haven't compiled bleadperl for a few weeks.
-- Andy Dougherty doughera@lafayette.edu
On Wed\, 22 Jun 2005\, Andy Dougherty wrote:
On Wed\, 22 Jun 2005\, Paul Johnson wrote:
On Tue\, Jun 21\, 2005 at 07:50:35PM +0200\, Salvador Fandino wrote:
I have not been able to reproduce the bug on any perl compiled by myself\, but anyway\, I am sure the problem is caused by the nasty macro SORTHINTS(hintsv)\, that relays on the evaluation order of something like
result = (v = foo()\, bar(v));
The patch attached breaks the macro in two: dSORTHINTS and SORTHINTS\, they do the same operation but in two steps.
I think the evaulation order here is quite well defined but\, of course\, perl has a fine tradition of working around broken compilers.
- /* Sun's Compiler (cc: WorkShop Compilers 4.2 30 Oct 1996 C 4.2) used - to miscompile this function under optimization -O. If you get test - errors related to picking the correct sort() function\, try recompiling - this file without optimiziation. -- A.D. 4/2002. - */
So yes\, hopefully this is fixed too but it would be interesting to hear from someone who can test under this compiler.
That'd be me. What\, precisely\, would you like me to test? I haven't had any recent problems with that compiler under -O\, so as far as I know\, but I haven't compiled bleadperl for a few weeks.
Ok\, I've looked at the RT ticket now. Yes\, this patch works fine for me with Sun's old compiler. (It was also working fine without the patch\, but that's ok. The patch didn't break anything!)
My original problem also had to do with embedding SORTINTS(hintsv) inside an if() statement. My solution back in 2002 was to break it out as two separate steps. Then the optimizer didn't get confused. (This is Change #15911.)
I32 hints = SORTHINTS(hintsv); if (hints & HINT_SORT_QUICKSORT) ...
This patch looks similar in spirit to me and is probably fine.
-- Andy Dougherty doughera@lafayette.edu
@rgs - Status changed from 'open' to 'resolved'
Salvador Fandino wrote:
I have not been able to reproduce the bug on any perl compiled by myself\, but anyway\, I am sure the problem is caused by the nasty macro SORTHINTS(hintsv)\, that relays on the evaluation order of something like
result = \(v = foo\(\)\, bar\(v\)\);
The patch attached breaks the macro in two: dSORTHINTS and SORTHINTS\, they do the same operation but in two steps.
Thanks\, applied as change #24951.
Migrated from rt.perl.org#36350 (status was 'resolved')
Searchable as RT36350$