Skip to content

cache+optimize#17

Open
HEIHUAa wants to merge 3 commits intoCodenameCrew:codename-devfrom
HEIHUAa:cache
Open

cache+optimize#17
HEIHUAa wants to merge 3 commits intoCodenameCrew:codename-devfrom
HEIHUAa:cache

Conversation

@HEIHUAa
Copy link
Copy Markdown

@HEIHUAa HEIHUAa commented Mar 29, 2026

It implements a variable location caching system that stores where each variable is found (global, customClass, scriptObject, etc.) to speed up repeated lookups during script execution.

Add variable caching to improve performance in most scenarios.

var i = 0;
while(i < $iterationsPerFrame) {
    var songPos = Conductor.songPosition;
    note.x = note.x + songPos * 0.0001;
    note.y = note.y + FlxG.elapsed;
    i = i + 1;
}
note.x + note.y;

↑This code, which runs multiple times per frame, saw a 1.33x speedup in testing.

var songPos = Conductor.songPosition;
var crochet = Conductor.crochet;
var bpm = Conductor.bpm;

var diff = songPos - note.strumTime;
var progress = diff / crochet;

note.x = 100 + progress * 50;
note.y = 200 + Math.sin(songPos * 0.01) * 20;
note.alpha = 1 - progress * 0.5;
note.angle = progress * 360;
note.scale.x = 1 + progress * 0.2;
note.scale.y = 1 + progress * 0.2;

note.x + note.y;

↑This complex per-frame script saw a 1.05x speedup.

var songPos = songPosition;
var c = crochet;
var b = bpm;
songPos + c + b;

↑Per-frame script execution on scriptObject saw a 2x speedup!

var localX = 0;
var localY = 0;
var i = 0;
while(i < $iterationsPerFrame) {
    localX = localX + Conductor.songPosition * 0.001;
    localY = localY + FlxG.elapsed;
    if(localX > 100) localX = 0;
    if(localY > 100) localY = 0;
    i = i + 1;
}
localX + localY;

↑This code using local variables showed no performance improvement.

In short, for scripts with almost no repeated calls, this could potentially reduce performance, but such scenarios are practically non-existent in real-world use. And even if they do occur, they wouldn't become the primary performance bottleneck.

Based on my testing, I haven't found any runtime issues — at least, I've run several large mods without encountering any problems.

@Raltyro Raltyro added the enhancement New feature or request label Mar 30, 2026
@HEIHUAa HEIHUAa changed the title cache cache+optimize Apr 1, 2026
@HEIHUAa
Copy link
Copy Markdown
Author

HEIHUAa commented Apr 1, 2026

Added Binop operator optimization, replacing the original StringMap lookup with enum-based switch matching.

Test code:

var sum = 0;
var a = 1;
var b = 2;
var c = 3;
for (i in 0...10000) {
    sum = sum + a * b + c - a / b;
    a = a + 1;
    b = b + 1;
    c = c + 1;
    if (sum > 1000000) sum = 0;
    if (a > 100) a = 1;
    if (b > 200) b = 2;
    if (c > 300) c = 3;
}
sum;

Here are the original benchmark results:

  • StringMap: Average 1672.511 ms, Min 1653.792 ms, Max 1718.162 ms
  • Switch Enum: Average 1336.131 ms, Min 1328.087 ms, Max 1350.583 ms

Benchmarking shows this is approximately 20% faster than the original StringMap approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants