From 9fd4c38efd133fe84691f8c9b907fbb06df66d08 Mon Sep 17 00:00:00 2001 From: Michael Votaw Date: Sat, 5 Oct 2024 21:19:47 -0500 Subject: [PATCH] Keep a temporary State object around instead of allocating and deleting one nearly every time initResultState is called --- src/StepParityGenerator.cpp | 30 ++++++++++++++++++++++++++---- src/StepParityGenerator.h | 6 +++++- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/StepParityGenerator.cpp b/src/StepParityGenerator.cpp index ba839ed478..0fd0789770 100644 --- a/src/StepParityGenerator.cpp +++ b/src/StepParityGenerator.cpp @@ -108,13 +108,36 @@ void StepParityGenerator::addStateToGraph(State * resultState, StepParityNode * State * StepParityGenerator::initResultState(State * initialState, Row &row, const FootPlacement &columns) { - State * resultState = new State(row.columnCount); - resultState->columns = columns; + if(tmpState == nullptr) + { + tmpState = new State(row.columnCount); + } + State * resultState = tmpState; + + // reset resultState + + for(int i = 0; i < NUM_Foot; i++) + { + resultState->whereTheFeetAre[i] = INVALID_COLUMN; + resultState->whatNoteTheFootIsHitting[i] = INVALID_COLUMN; + resultState->didTheFootMove[i] = false; + resultState->isTheFootHolding[i] = false; + } + + for (unsigned long i = 0; i < columns.size(); i++) + { + resultState->columns[i] = NONE; + resultState->combinedColumns[i] = NONE; + resultState->movedFeet[i] = NONE; + resultState->holdFeet[i] = NONE; + } + // I tried to condense this, but kept getting the logic messed up for (unsigned long i = 0; i < columns.size(); i++) { + resultState->columns[i] = columns[i]; if(columns[i] == NONE) { continue; } @@ -155,12 +178,11 @@ State * StepParityGenerator::initResultState(State * initialState, Row &row, con if(maybeState != stateCache.end()) { State* cachedState = maybeState->second; - delete resultState; return maybeState->second; } stateCache.insert({stateHash, resultState}); - + tmpState = nullptr; return resultState; } diff --git a/src/StepParityGenerator.h b/src/StepParityGenerator.h index 53d7b3c23a..ab91b689cd 100644 --- a/src/StepParityGenerator.h +++ b/src/StepParityGenerator.h @@ -41,7 +41,7 @@ namespace StepParity { StepParity::StepParityNode * startNode = nullptr; StepParity::State * endingState = nullptr; StepParity::StepParityNode * endNode = nullptr; - + StepParity::State * tmpState = nullptr; public: std::unordered_map stateCache; std::vector nodes; @@ -72,6 +72,10 @@ namespace StepParity { { delete endingState; } + if(tmpState != nullptr) + { + delete tmpState; + } } /// @brief Analyzes the given NoteData to generate a vector of StepParity::Rows, with each step annotated with /// a foot placement.