Uh oh!
There was an error while loading. Please reload this page.
Rework sort() to sort in place in a Uint32Array. - #4
Open
zanesterling wants to merge 4 commits into
Open
Conversation
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
commented
Feb 26, 2026
Author
Noticed the tests file. I ran the tests and fixed a couple bugs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
idsarray, 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 ofcluster().I've chosen here to split
sort()into three calls topartition(), first partitioning on x, and then on y. Partition can then be very simple. It grows the list oflosfrom 0-index upward and the list of his from the last index downward.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
losorhis, and then swaps it with the unsorted item in the next spot next tolosorhis. 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