google / starlark-go

Starlark in Go: the Starlark configuration language, implemented in Go
BSD 3-Clause "New" or "Revised" License
2.32k stars 212 forks source link

struct: store members as starlark.StringDict for faster access #401

Closed emcfarlane closed 2 years ago

emcfarlane commented 2 years ago

Wraps starlark.StringDict for storing fields. Improves the performance on the Attr method for accessing fields.

I added a benchmark to check the happy path for access to a fields value.

OLD: BenchmarkAttr_4-4 44931932 24.03 ns/op BenchmarkAttr_8-4 39342510 28.64 ns/op BenchmarkAttr_16-4 33596271 32.18 ns/op BenchmarkAttr_32-4 30505212 37.62 ns/op BenchmarkAttr_64-4 24424558 43.33 ns/op BenchmarkAttr_128-4 17947136 64.29 ns/op

NEW: BenchmarkAttr_4-4 48772784 21.45 ns/op BenchmarkAttr_8-4 34554286 30.95 ns/op BenchmarkAttr_16-4 56185027 19.59 ns/op BenchmarkAttr_32-4 58317289 19.60 ns/op BenchmarkAttr_64-4 58073444 20.30 ns/op BenchmarkAttr_128-4 52069291 21.30 ns/op

emcfarlane commented 2 years ago

Looked at how starlark.Dict internally does it and found a similar optimisation: https://github.com/google/starlark-go/pull/402

emcfarlane commented 2 years ago

Closing for https://github.com/google/starlark-go/pull/403