From 6ec9d0a831849ffaf7d46b2eb4db7d56260809cf Mon Sep 17 00:00:00 2001 From: Charlie Stanton Date: Tue, 25 Apr 2023 09:48:43 +0100 Subject: Improves performance of pruneStates by modifying states in place --- subex/main.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'subex/main.go') 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 -- cgit v1.2.3