Skip to content

Rework sort() to sort in place in a Uint32Array. - #4

Open
zanesterling wants to merge 4 commits into
plotly:masterfrom
zanesterling:master
Open

zanesterling wants to merge 4 commits into
plotly:masterfrom
zanesterling:master

Conversation

@zanesterling

Copy link
Copy Markdown

This is ~5x faster than the existing sort() when run with ~65k points.

The main win here is that it makes only one copy of the input ids array, and then sorts this in place. The existing implementation allocates new copies of the sublists at each recursion layer. In my case of 65k points, the deepest branch of the tree has 15 layers, so that's up to 15 copies of the list, or 15 * 65k * 8B ~= 7.8MB. Dodging the reallocations means less GC thrashing, which in my experiments took up about half of the runtime of cluster().

I've chosen here to split sort() into three calls to partition(), first partitioning on x, and then on y. Partition can then be very simple. It grows the list of los from 0-index upward and the list of his from the last index downward.

0                                   ids.len
+--------+-----------------+--------+
| los  ->| ....unsorted... |<-  his |
+--------+-----------------+--------+

It begins by picking up the first element in the list. Then, as long as the unsorted section is not empty, it checks if the held item should go in los or his, and then swaps it with the unsorted item in the next spot next to los or his. At the end the unsorted section is empty and it still has one item in its hand, so if that item is a lo it goes in index 0, and otherwise it swaps with the highest-index lo, which then goes in index 0.

In this implementation of partition each element in the array is loaded exactly once from memory and written exactly once to memory.

I tried five different implementations of partition, including a couple that broke the sort-in-place rule, and this was the quickest out of the pack. There might yet be something quicker -- this implementation definitely is not super friendly to SIMD, for instance -- but this is the best I can find in the couple hours I spent on it today and yesterday.


Please let me know if there's anything you'd like changed / added / explained about this PR. I tried to maintain the coding style of the file, but happy to make changes if there are suggestions.

Cheers,
Zane

Zane Sterling added 4 commits February 26, 2026 16:50
This is ~5x faster than the existing sort() when run with ~65k points.

The main here is that it makes only one copy of the input ids array, and
then sorts this in place. The existing implementation allocates new
copies of the sublists at each recursion layer. In my case of 65k
points, the deepest branch of the tree has 15 layers, so that's up to 15
copies of the list, or 15 * 65k * 8B ~= 7.8MB. Dodging the reallocations
means less GC thrashing, which in my experiments took up about half of
the runtime of cluster().

I've chosen here to split sort() into three calls to partition(), first
partitioning on x, and then on y. Partition can then be very simple. It
grows the list of los from 0-index upward and the list of his from the
last index downward.

0                                   ids.len
+--------+-----------------+--------+
| los  ->| ....unsorted... |<-  his |
+--------+-----------------+--------+

It begins by picking up the first element in the list. Then, as long as
the unsorted section is not empty, it checks if the held item should go
in los or his, and then swaps it with the unsorted item in the next spot
next to los or his. At the end the unsorted section is empty and it
still has one item in its hand, so if that item is a lo it goes in index
0, and otherwise it swaps with the highest-index lo, which then goes in
index 0.

In this implementation of partition each element in the array is loaded
exactly once from memory and written exactly once to memory.

I tried five different implementations of partition, including a couple
that broke the sort-in-place rule, and this was the quickest out of the
pack. There might yet be something quicker -- this implementation
definitely is not super friendly to SIMD, for instance -- but this is
the best I can find in the couple hours I spent on it today and
yesterday.
Because the new sort does not maintain the original order of the
indexes, it may select different points to be the representative point
at different internal nodes in the tree. This will produce a different
ordering of the returned indexes, but is still an equally valid
quadtree. The tests need to be updated to expect this new order.
@zanesterling

Copy link
Copy Markdown
Author

Noticed the tests file. I ran the tests and fixed a couple bugs.

@zanesterling zanesterling changed the title Rework sort() to sort in place in a Float64Array. Rework sort() to sort in place in a Uint32Array. Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant