Skip to content

Implement numerical index tracking to optimize adding to lists#8519

Open
UnderscoreTud wants to merge 3 commits intodev/patchfrom
patch/optimize-list-adding
Open

Implement numerical index tracking to optimize adding to lists#8519
UnderscoreTud wants to merge 3 commits intodev/patchfrom
patch/optimize-list-adding

Conversation

@UnderscoreTud
Copy link
Copy Markdown
Member

Problem

When adding an element to a list, Skript scans through the entire list to find an open spot for the element. This is a pretty slow operation with a time complexity of O(n), and adds up extremely fast, to the point where adding 10k elements to a list takes around 11 seconds on my machine

Solution

Use a custom tree map implementation that tracks the list's numerical indices and their positions. The implementation adds a shortcut if the numerical indices are consecutive, so for a best case scenario (no gaps in the indices), adding to a list will have a time complexity of O(1). If gaps are present on the other hand, the list will perform a binary search to find the next open slot, giving a time complexity of O(log n) for a worst case scenario. With this implementation, adding 1 million elements to a list takes ~1.5 seconds on my machine, that's over a 70,000% improvement over the old design!

Testing Completed

IndexTrackingTreeMapTest.java

Supporting Information


Completes: none
Related: none
AI assistance: Junie was used to generate the majority of test cases

@UnderscoreTud UnderscoreTud requested review from a team as code owners April 1, 2026 16:49
@UnderscoreTud UnderscoreTud requested review from APickledWalrus and TheMug06 and removed request for a team April 1, 2026 16:49
@skriptlang-automation skriptlang-automation bot added the needs reviews A PR that needs additional reviews label Apr 1, 2026
sovdeeth

This comment was marked as resolved.

Copy link
Copy Markdown
Contributor

@Absolutionism Absolutionism left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solid 💪

@skriptlang-automation skriptlang-automation bot added patch-ready A PR/issue that has been approved and is ready to be merged/closed for the next patch version. and removed needs reviews A PR that needs additional reviews labels Apr 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

patch-ready A PR/issue that has been approved and is ready to be merged/closed for the next patch version.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants