introduce js::Swap and use it in js::MergeSort




JavaScript Engine
7 years ago
4 years ago


(Reporter: Igor Bukanov, Unassigned)


Firefox Tracking Flags

(Not tracked)




7 years ago
From the bug 701560 comment 15 by Luke Wagner:

> > +                T tmp = array[j - 1];
> > +                array[j - 1] = array[j];
> > +                array[j] = tmp;
> Incidentally, gcc seems really bad manipulating uint64 by value on 32-bit. 
> I just looked with -O3 and got something awful.  When I broke it down into
> two manual uint32 swaps, the resulting code was 6 memory movs shorter.  This
> suggests replacing all these inlined swaps with a call to js::Swap(a,b) (in
> jsutil.h) which has an overload for js::Value that does the uint32 swaps.

Comment 1

7 years ago
To see how bad GCC 4.6 is at that swap on 32 bit consider the following C code:

void swap1(long long *a, long long *b)
    long long tmp = *a;
    *a = *b;
    *b = tmp;

void swap2(long long *a, long long *b)
    int *x = (int *) a;
    int *y = (int *) b;

    int tmp = *x;
    *x = *y;
    *y = tmp;


    tmp = *x;
    *x = *y;
    *y = tmp;

The compiler generates at -O3 -fomit-frame-pointer:

swap1:                       swap2:		      
subl	$12, %esp	     pushl	%ebx	      
movl	16(%esp), %edx	     movl	8(%esp), %edx 
movl	20(%esp), %eax	     movl	12(%esp), %eax
movl	%ebx, (%esp)	     movl	(%edx), %ecx  
movl	%esi, 4(%esp)	     movl	(%eax), %ebx  
movl	%edi, 8(%esp)	     movl	%ebx, (%edx)  
movl	4(%edx), %ebx	     movl	4(%eax), %ebx 
movl	(%eax), %esi	     movl	%ecx, (%eax)  
movl	4(%eax), %edi	     movl	4(%edx), %ecx 
movl	(%edx), %ecx	     movl	%ebx, 4(%edx) 
movl	%esi, (%edx)	     movl	%ecx, 4(%eax) 
movl	4(%esp), %esi	     popl	%ebx          
movl	%edi, 4(%edx)
movl	8(%esp), %edi
movl	%ebx, 4(%eax)
movl	(%esp), %ebx
movl	%ecx, (%eax)
addl	$12, %esp


4 years ago
Assignee: general → nobody
You need to log in before you can comment on or make changes to this bug.