diff options
| author | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-21 12:51:25 +0100 | 
|---|---|---|
| committer | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-21 12:51:25 +0100 | 
| commit | 26bce7119200f37f8b9f3ddc1a2c76c85f7c88be (patch) | |
| tree | 21a2aa762215e2230ba676454828c1497a568cc6 /subex | |
| parent | 184118c1522ee4e78a0588fcac8eb235f512b599 (diff) | |
| download | stred-go-26bce7119200f37f8b9f3ddc1a2c76c85f7c88be.tar | |
Changes the implementation of Atomise and Compound to no longer use goroutines
This results in a massive performance boost, ~4x speedup
Diffstat (limited to 'subex')
| -rw-r--r-- | subex/arithmetic.go | 10 | ||||
| -rw-r--r-- | subex/main.go | 13 | 
2 files changed, 14 insertions, 9 deletions
| 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 | 
