Skip to content

Commit

Permalink
fix: better performance for long-running scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
RainbowwPhoenixx committed Nov 18, 2023
1 parent ebc8910 commit fd645b5
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/Features/Tas/TasPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,16 +298,31 @@ void TasPlayer::AdvanceFrame() {

// returns raw framebulk that should be used for given tick
TasFramebulk TasPlayer::GetRawFramebulkAt(int slot, int tick) {
int closestTime = INT_MAX;
TasFramebulk closest;
for (TasFramebulk framebulk : playbackInfo.slots[slot].framebulks) {
int timeDist = tick - framebulk.tick;
if (timeDist >= 0 && timeDist < closestTime) {
closestTime = timeDist;
closest = framebulk;
// Do binary search for bulks immediately before and after our target tick
uint32_t before = 0;
uint32_t after = playbackInfo.slots[slot].framebulks.size() - 1;

if (playbackInfo.slots[slot].framebulks[before].tick == tick) {
return playbackInfo.slots[slot].framebulks[before];
}
if (playbackInfo.slots[slot].framebulks[after].tick == tick) {
return playbackInfo.slots[slot].framebulks[after];
}

while (before + 1 != after) {
uint32_t middle = (before + after) / 2;
TasFramebulk middle_bulk = playbackInfo.slots[slot].framebulks[middle];

if (middle_bulk.tick < tick) {
before = middle;
} else if (middle_bulk.tick > tick) {
after = middle;
} else {
return middle_bulk;
}
}
return closest;

return playbackInfo.slots[slot].framebulks[before];
}

TasPlayerInfo TasPlayer::GetPlayerInfo(int slot, void *player, CUserCmd *cmd, bool clientside) {
Expand Down

0 comments on commit fd645b5

Please sign in to comment.