diff options
author | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-25 09:48:43 +0100 |
---|---|---|
committer | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-25 09:48:43 +0100 |
commit | 6ec9d0a831849ffaf7d46b2eb4db7d56260809cf (patch) | |
tree | cba0bab46614d74b898b91517e6cdb5e804c090e | |
parent | 9b26559c8273fd4de6aa6a740d6472a665446274 (diff) | |
download | stred-go-6ec9d0a831849ffaf7d46b2eb4db7d56260809cf.tar |
Improves performance of pruneStates by modifying states in place
-rw-r--r-- | subex/main.go | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/subex/main.go b/subex/main.go index 635b1de..638e0f5 100644 --- 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 |