From 26bce7119200f37f8b9f3ddc1a2c76c85f7c88be Mon Sep 17 00:00:00 2001 From: Charlie Stanton Date: Fri, 21 Apr 2023 12:51:25 +0100 Subject: Changes the implementation of Atomise and Compound to no longer use goroutines This results in a massive performance boost, ~4x speedup --- subex/arithmetic.go | 10 +++++----- subex/main.go | 13 +++++++++---- 2 files changed, 14 insertions(+), 9 deletions(-) (limited to 'subex') diff --git a/subex/arithmetic.go b/subex/arithmetic.go index 52f576d..a7dc73a 100644 --- a/subex/arithmetic.go +++ b/subex/arithmetic.go @@ -10,7 +10,7 @@ func sumValues(atoms []walk.Atom) ([]walk.Atom, error) { allBools := true var sum float64 = 0 var any bool = false - values, err := walk.MemoryCompound(atoms) + values, err := walk.Compound(atoms) if err != nil { return nil, err } @@ -50,7 +50,7 @@ func multiplyValues(atoms []walk.Atom) ([]walk.Atom, error) { allBools := true var product float64 = 1 var all bool = false - values, err := walk.MemoryCompound(atoms) + values, err := walk.Compound(atoms) if err != nil { return nil, err } @@ -89,7 +89,7 @@ func multiplyValues(atoms []walk.Atom) ([]walk.Atom, error) { // Does tries to cast all to numbers and negates them func negateValues(atoms []walk.Atom) ([]walk.Atom, error) { var negatedNumbers []walk.Atom - values, err := walk.MemoryCompound(atoms) + values, err := walk.Compound(atoms) if err != nil { return nil, err } @@ -123,7 +123,7 @@ func negateValues(atoms []walk.Atom) ([]walk.Atom, error) { // Else errors func reciprocalValues(atoms []walk.Atom) ([]walk.Atom, error) { var reciprocals []walk.Atom - values, err := walk.MemoryCompound(atoms) + values, err := walk.Compound(atoms) if err != nil { return nil, err } @@ -156,7 +156,7 @@ func reciprocalValues(atoms []walk.Atom) ([]walk.Atom, error) { // If all are castable to booleans, NOTs all and returns them // Else errors func notValues(atoms []walk.Atom) (notted []walk.Atom, err error) { - values, err := walk.MemoryCompound(atoms) + values, err := walk.Compound(atoms) if err != nil { return nil, err } diff --git a/subex/main.go b/subex/main.go index 9824f10..bb688e9 100644 --- a/subex/main.go +++ b/subex/main.go @@ -103,13 +103,13 @@ func pruneStates(states []SubexBranch) (newStates []SubexBranch) { } // Run the subex transducer -func RunTransducer(transducer SubexState, input <-chan walk.Atom) (output []walk.Atom, err bool) { +func RunTransducer(transducer SubexState, input []walk.Atom) (output []walk.Atom, err bool) { states := []SubexBranch{{ state: transducer, outputStack: OutputStackNil{}.push(nil), store: make(Store), }} - for piece := range input { + for _, piece := range input { var newStates []SubexBranch for _, state := range states { newStates = append(newStates, state.eat(piece)...) @@ -149,7 +149,12 @@ func Main() { close(out) }(jsonStream, tokenStream) - atoms := walk.Atomise(tokenStream) + var tokens []walk.WalkValue + for token := range tokenStream { + tokens = append(tokens, token) + } + + atoms := walk.Atomise(tokens) output, err := RunTransducer(transducer, atoms) if err { @@ -157,7 +162,7 @@ func Main() { return } - valueOut, error := walk.MemoryCompound(output) + valueOut, error := walk.Compound(output) if error != nil { fmt.Println(error.Error()) return -- cgit v1.2.3