Skip to content

Commit

Permalink
Merge pull request #1 from bhmj/memory-optimization-v1
Browse files Browse the repository at this point in the history
- Zero memory consumption on evaluation (preallocated on parse time)-
- Minor speed optimization
- infinity comparison improved
- invalid hex number detection
- test coverage report added
  • Loading branch information
bhmj authored Nov 19, 2021
2 parents 51d08f3 + 6b6b6cb commit 2eac4e5
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 94 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"mode": "auto",
"program": "${workspaceFolder}/cmd/xpression/main.go",
"env": {},
"args": ["@.price>10"]
"args": ["0xDEFG + 1"]
}
]
}
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ help:
echo ""
echo " <command> is"
echo ""
echo " configure - install tools and dependencies"
echo " configure - install tools and dependencies (gocyclo and golangci-lint)"
echo " build - build jsonslice CLI"
echo " run - run jsonslice CLI"
echo " lint - run linters"
echo " test - run tests"
echo " cover - generate coverage report"
echo ""

configure:
go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
Expand All @@ -38,6 +40,10 @@ test:
go test -cover ./...
go test -benchmem -bench=.

cover:
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out

.PHONY: all configure help build run lint test

$(V).SILENT:
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ Expression examples:
// external data in expression (aka variables)
foobar := 123
varFunc := func(name []byte) (*xpression.Operand, error) {
varFunc := func(name []byte, result *xpression.Operator) error {
mapper := map[string]*int{
`foobar`: &foobar
}
return xpression.Number(float64(*mapper[string(name)])), nil
xpression.SetNumber(float64(*mapper[string(name)]))
return nil
}
tokens, err := xpression.Parse([]byte(`27 / foobar`))
result, err := xpression.Evaluate(tokens, varFunc)
Expand Down Expand Up @@ -81,35 +82,36 @@ Tests cover the majority of cases described in ECMAScript Language definition (s

Evaluate `(2) + (2) == (4)`

```diff
```golang
$ go test -bench=. -benchmem -benchtime=4s
goos: darwin
goarch: amd64
pkg: github.com/bhmj/xpression
cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Benchmark_ModifiedNumericLiteral_WithParsing-16 2714204 1853 ns/op 1272 B/op 26 allocs/op
Benchmark_ModifiedNumericLiteral_WithoutParsing-16 31129712 143.9 ns/op 128 B/op 2 allocs/op
Benchmark_ModifiedNumericLiteral_WithParsing-16 2622770 1811 ns/op 1272 B/op 26 allocs/op
Benchmark_ModifiedNumericLiteral_WithoutParsing-16 77455698 57.55 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/bhmj/xpression 12.363s
ok github.com/bhmj/xpression 11.548s
```

The same expression evaluated with [github.com/Knetic/govaluate](https://github.com/Knetic/govaluate) :

```diff
```golang
$ go test -bench='LiteralModifiers' -benchmem -benchtime=4s
goos: darwin
goarch: amd64
pkg: github.com/Knetic/govaluate
cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
BenchmarkEvaluationLiteralModifiers_WithParsing-16 1000000 4019 ns/op 2208 B/op 43 allocs/op
BenchmarkEvaluationLiteralModifiers-16 30173640 147.2 ns/op 8 B/op 1 allocs/op
BenchmarkEvaluationLiteralModifiers_WithParsing-16 1000000 4019 ns/op 2208 B/op 43 allocs/op
BenchmarkEvaluationLiteralModifiers-16 30173640 147.2 ns/op 8 B/op 1 allocs/op
PASS
ok github.com/Knetic/govaluate 9.810s
```


## Changelog

**0.9.0** (2021-11-19) -- Memory allocation reduced. Speed optimization.
**0.8.0** (2021-11-11) -- hex numbers support. Production ready.
**0.7.x** (2021-11-11) -- WIP
**0.7.0** (2021-11-10) -- project renamed to `xpression`
Expand All @@ -131,7 +133,7 @@ ok github.com/Knetic/govaluate 9.810s
- [x] parser test coverage
- [x] evaluator test coverage
- [x] add external reference type (node reference in jsonslice)
- [ ] optimize memory allocations
- [x] optimize memory allocations
- [ ] Unicode support!

## Contributing
Expand Down
8 changes: 5 additions & 3 deletions cmd/xpression/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ func main() {
}
}

func variableGetter(variable []byte) (*xpression.Operand, error) {
func variableGetter(variable []byte, result *xpression.Operand) error {
if string(variable) == "@.foobar" {
return xpression.Number(123), nil
result.SetNumber(123)
return nil
}
return xpression.Boolean(false), nil
result.SetBoolean(false)
return nil
}

func printParsedExpression(tokens []*xpression.Token) {
Expand Down
2 changes: 2 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var (
errUnexpectedEndOfString,
errMismatchedParentheses,
errNotEnoughArguments,
errInvalidHexadecimal,
errTooLongHexadecimal error
)

Expand All @@ -15,5 +16,6 @@ func init() {
errUnexpectedEndOfString = errors.New("unexpected end of string")
errMismatchedParentheses = errors.New("mismatched parentheses")
errNotEnoughArguments = errors.New("not enough arguments")
errInvalidHexadecimal = errors.New("invalid hexadecimal")
errTooLongHexadecimal = errors.New("too long hexadecimal")
}
Loading

0 comments on commit 2eac4e5

Please sign in to comment.