Skip to content

Improve add syntax for lists by saving the last used index#8539

Closed
Shroobz wants to merge 4 commits intoSkriptLang:dev/patchfrom
Shroobz:patch/fix-891
Closed

Improve add syntax for lists by saving the last used index#8539
Shroobz wants to merge 4 commits intoSkriptLang:dev/patchfrom
Shroobz:patch/fix-891

Conversation

@Shroobz
Copy link
Copy Markdown

@Shroobz Shroobz commented Apr 14, 2026

Problem

Adding to lists has been slow due to an O(N) lookup happening every single time a value is added. This means adding N values to a list separately is O(N²), while adding them all at the same time is O(N). This problem is also described in #891

Solution

I (somewhat) solved this problem in 2 ways:
1: Replaced repeated TreeMap.containsKey() calls with HashSet.contains() calls
2: Saving the last used index during an add operation. If another operation is done that could cause a gap in the list, the saved index is reset. This means the O(N) lookup will only happen once a gap can occur or when the server restarts.

Testing Completed

quickTest was completed succesfully. I tested if gaps in lists would get filled and the performance of the code in #891 (the performance is now pretty much the same for setting and adding, adding might even be faster (though I doubt it))

Supporting Information


Completes:: #891
Related: #891
AI assistance: none

p.s. I think some sort of auto formatter ran and removed a double tab around line 242

@Shroobz Shroobz requested review from a team as code owners April 14, 2026 12:04
@Shroobz Shroobz requested review from Burbulinis and UnderscoreTud and removed request for a team April 14, 2026 12:04
Copy link
Copy Markdown
Member

@Efnilite Efnilite left a comment

Choose a reason for hiding this comment

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

some benchmarks may be nice to show the actual improvement (and whether there is one)

Comment thread src/main/java/ch/njol/skript/lang/Variable.java Outdated
Comment thread src/main/java/ch/njol/skript/lang/Variable.java Outdated
Comment thread src/main/java/ch/njol/skript/lang/Variable.java
@Shroobz
Copy link
Copy Markdown
Author

Shroobz commented Apr 14, 2026

some benchmarks may be nice to show the actual improvement (and whether there is one)

I have no idea how I forgot those, here:

Before After
2026-04-14-141448_hyprshot 2026-04-14-141448_hyprshot
function test_blockloop_add(radius: integer, location: location):
  delete {blocks::*}
  set {_time} to now
  loop blocks in radius {_radius} around {_location}:
    add loop-block to {blocks::*}
  
  broadcast "It took %difference between now and {_time}% to loop blocks in a radius of %{_radius}% and add them to a list using add syntax."
  broadcast "%size of {blocks::*}% values were set"

function test_blockloop_set(radius: integer, location: location):
  delete {blocks::*}
  set {_time} to now
  set {_i} to 0
  loop blocks in radius {_radius} around {_location}:
    set {blocks::%{_i}%} to loop-block
    add 1 to {_i}
  broadcast "It took %difference between now and {_time}% to loop blocks in a radius of %{_radius}% and add them to a list using set syntax."
  broadcast "%{_i}% values were set"

Shroobz and others added 2 commits April 14, 2026 14:25
Some auto-formatter ran accidentally

Co-authored-by: Efnilite <35348263+Efnilite@users.noreply.github.com>
Co-authored-by: Efnilite <35348263+Efnilite@users.noreply.github.com>
@Shroobz
Copy link
Copy Markdown
Author

Shroobz commented Apr 14, 2026

I forgot to rename the usages of lastUsedIndex, 1 sec

@Efnilite
Copy link
Copy Markdown
Member

some benchmarks may be nice to show the actual improvement (and whether there is one)

I have no idea how I forgot those, here:
Before After
2026-04-14-141448_hyprshot 2026-04-14-141448_hyprshot

image

😝

@UnderscoreTud
Copy link
Copy Markdown
Member

This is a duplicate of #8519

@Shroobz
Copy link
Copy Markdown
Author

Shroobz commented Apr 14, 2026

This is a duplicate of #8519

no idea how I didn't see that, sorry

@UnderscoreTud
Copy link
Copy Markdown
Member

It's okay, we still appreciate your contribution! It's partly my fault for not referencing the issue you referenced (I didn't know it existed :p). Don't let this push you away from contributing in the future!

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.

3 participants