ArrayBufferView.sort Broken (I think!!)

Posted on
  • I have refactored some code to save memory using Typed Arrays and have come in to a problem which I have managed to replicate using the following code:

    x=new Uint32Array([30474260, 124846100, 219217940, 313589780, 407961620, 502333460, 596705300, 33423370, 127795210, 222167050, 316538890, 410910730, 505282570, 599654410, 49152018, 143523858, 237895698, 332267538, 426639378, 521011218, 615383058, 53084170, 147456010, 241827850, 336199690, 430571530, 524943370, 619315210, 64880661, 159252501, 253624341, 347996181, 442368021, 536739861, 631111701, 86507535, 180879375, 275251215, 369623055, 463994895, 558366735, 652738575]);
    
    console.log(x);
    
    x.sort(function(a, b){return (a - b);});
    
    console.log(x);
    

    I get the following Output for the second console.log (the first is as expected):

    new Uint32Array([30474260, 33423370, 49152018, 124846100, 53084170, 124846100, 124846100, 64880661, 124846100, 86507535, 124846100, 143523858, 147456010, 237895698, 407961620, 407961620, 241827850, 407961620, 336199690, 407961620, 407961620, 127795210, 407961620, 159252501, 407961620, 253624341, 410910730, 316538890, 410910730, 180879375, 410910730, 410910730, 410910730, 442368021, 442368021, 410910730, 442368021, 369623055, 410910730, 0, 410910730, 0])
    

    As you can see its a bit of a mess - some values missing, others repeated.

    I haven't exceeded the maximum value for a Uint32 ( 4,294,967,295).

    Any ideas?

  • I'm afraid this definitely looks like a bug! I'll try and sort it out next week.

    Thanks for coming up with such a clear demonstration of it :)

  • That would be great - I appreciate you must be extremely busy with project Pico!

    I only have about 40 elements so I am calling a simple bubble sort routine to work around the issue.

  • I just looked into this and it appears to be some issue with ArrayBuffers where the element size is >1 byte - so normal Uint8Array works (probably why it got through testing). For ~40 elements you could use a normal array for now, but hopefully I'll be able to get this fixed quite soon.

  • Ok, I know what the issue is, however it turns out that back in March I merged in a pull request where someone had modified the Makefile and had disabled all the debug checks in the code.

    So on Monday I've now got to go through and fix all the issues that should have been detected since March but weren't. On the plus side, it should be super-reliable after all that!

  • Hi Dave, I just fixed this - it'll be in the 1v71 release

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

ArrayBufferView.sort Broken (I think!!)

Posted by Avatar for DaveNI @DaveNI

Actions