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 authored and ThisAMJ committed Jul 21, 2024
1 parent c5c873e commit f3c31f3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
57 changes: 47 additions & 10 deletions src/Features/Tas/TasPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ void TasPlayer::Activate(TasPlaybackInfo info) {

for (int slot = 0; slot < 2; ++slot) {
playbackInfo.slots[slot].ClearGeneratedContent();

currentInputFramebulkIndex[slot] = 0;
currentToolsFramebulkIndex[slot] = 0;
}

active = true;
Expand Down Expand Up @@ -303,16 +306,50 @@ 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];
}

// returns raw framebulk using more efficient method with cached incremented index of last used tick
TasFramebulk TasPlayer::GetRawFramebulkAt(int slot, int tick, int& cachedIndex) {
if (cachedIndex < 0) {
cachedIndex = 0;
}

auto maxIndex = playbackInfo.slots[slot].framebulks.size() - 1;

if (cachedIndex >= maxIndex) {
return playbackInfo.slots[slot].framebulks[maxIndex];
}

while (cachedIndex < maxIndex && playbackInfo.slots[slot].framebulks[cachedIndex + 1].tick <= tick) {
cachedIndex++;
}

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

TasPlayerInfo TasPlayer::GetPlayerInfo(int slot, void *player, CUserCmd *cmd, bool clientside) {
Expand Down Expand Up @@ -494,7 +531,7 @@ void TasPlayer::FetchInputs(int slot, TasController *controller) {
// to actually hook at _Host_RunFrame_Input or CL_Move.
int tick = currentTick + 1;

TasFramebulk fb = GetRawFramebulkAt(slot, tick);
TasFramebulk fb = GetRawFramebulkAt(slot, tick, currentInputFramebulkIndex[slot]);

int fbTick = fb.tick;

Expand Down Expand Up @@ -563,7 +600,7 @@ void TasPlayer::PostProcess(int slot, void *player, CUserCmd *cmd) {
return;
}

TasFramebulk fb = GetRawFramebulkAt(slot, tasTick);
TasFramebulk fb = GetRawFramebulkAt(slot, tasTick, currentToolsFramebulkIndex[slot]);

// update all tools that needs to be updated
auto fbTick = fb.tick;
Expand Down
4 changes: 4 additions & 0 deletions src/Features/Tas/TasPlayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class TasPlayer : public Feature {

int wasEnginePaused = false; // Used to check if we need to revert incrementing a tick

// used to cache last used framebulk to quickly access it for playback
int currentInputFramebulkIndex[2];
int currentToolsFramebulkIndex[2];
public:
void Update();
void UpdateServer();
Expand Down Expand Up @@ -99,6 +102,7 @@ class TasPlayer : public Feature {
void AdvanceFrame();

TasFramebulk GetRawFramebulkAt(int slot, int tick);
TasFramebulk GetRawFramebulkAt(int slot, int tick, int &cachedIndex);

TasPlayerInfo GetPlayerInfo(int slot, void *player, CUserCmd *cmd, bool clientside = false);

Expand Down

0 comments on commit f3c31f3

Please sign in to comment.