stred

Stred: Streaming Tree Editor. Like sed but for JSON
git clone http://shtanton.xyz/git/repo/stred
Log | Files | Refs

commit 6ec9d0a831849ffaf7d46b2eb4db7d56260809cf
parent 9b26559c8273fd4de6aa6a740d6472a665446274
Author: Charlie Stanton <charlie@shtanton.xyz>
Date:   Tue, 25 Apr 2023 09:48:43 +0100

Improves performance of pruneStates by modifying states in place

Diffstat:
Msubex/main.go | 12+++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/subex/main.go b/subex/main.go @@ -107,16 +107,18 @@ func equalStates(left SubexBranch, right SubexBranch) bool { // If two branches have the same state, only the first has a chance of being successful // This function removes all of the pointless execution branches to save execution time -func pruneStates(states []SubexBranch) (newStates []SubexBranch) { +func pruneStates(states []SubexBranch) []SubexBranch { + uniqueStates := 0 outer: for _, state := range states { - for _, newState := range newStates { - if equalStates(state, newState) { + for i := 0; i < uniqueStates; i++ { + if equalStates(state, states[i]) { continue outer } } - newStates = append(newStates, state) + states[uniqueStates] = state + uniqueStates++ } - return newStates + return states[:uniqueStates] } // Run the subex transducer