Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid char array allocation in Starlark format #23763

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

fmeum
Copy link
Collaborator

@fmeum fmeum commented Sep 25, 2024

Positional access via String#charAt is slightly faster than precreating the char array for Latin-1 strings and much faster for UTF-8 strings. It allocates less in both cases.

Also adds a --latin1 flag to the Benchmarks tool that allows benchmarking against Bazel's way of parsing Starlark files.

Before:

INFO: Running command line: bazel-bin/src/test/java/net/starlark/java/eval/Benchmarks --filter bench_format --seconds 20
File src/test/java/net/starlark/java/eval/testdata/bench_string.star:
benchmark                        ops     cpu/op    wall/op   steps/op   alloc/op
bench_format                16777215     1.46µs     1.46µs          7     2.26KB

INFO: Running command line: bazel-bin/src/test/java/net/starlark/java/eval/Benchmarks --filter bench_format --seconds 20 --latin1
File src/test/java/net/starlark/java/eval/testdata/bench_string.star:
benchmark                        ops     cpu/op    wall/op   steps/op   alloc/op
bench_format                16777215     1.47µs     1.46µs          7     2.26KB

After:

INFO: Running command line: bazel-bin/src/test/java/net/starlark/java/eval/Benchmarks --filter bench_format --seconds 20
File src/test/java/net/starlark/java/eval/testdata/bench_string.star:
benchmark                        ops     cpu/op    wall/op   steps/op   alloc/op
bench_format                16777215     1.79µs     1.79µs          7     1.67KB

INFO: Running command line: bazel-bin/src/test/java/net/starlark/java/eval/Benchmarks --filter bench_format --seconds 20 --latin1
File src/test/java/net/starlark/java/eval/testdata/bench_string.star:
benchmark                        ops     cpu/op    wall/op   steps/op   alloc/op
bench_format                16777215     1.37µs     1.37µs          7     1.67KB

@github-actions github-actions bot added the awaiting-review PR is awaiting review from an assigned reviewer label Sep 25, 2024
@iancha1992 iancha1992 added team-Remote-Exec Issues and PRs for the Execution (Remote) team team-Starlark-Interpreter Issues involving the Starlark interpreter used by Bazel and removed team-Remote-Exec Issues and PRs for the Execution (Remote) team labels Sep 25, 2024
@fmeum
Copy link
Collaborator Author

fmeum commented Nov 21, 2024

@tetromino Friendly ping

Copy link
Contributor

@tetromino tetromino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on some internal testing on bazel query $VERY_LARGE_TARGET, this delivers a measurable CPU time savings, but does not translate into a statistically significant effect on overall wall time.

My only concern is in bench_string.star - you were running "".format(...), so the measurements you showed are probably not representative.

@fmeum
Copy link
Collaborator Author

fmeum commented Nov 25, 2024

Thanks, I fixed and reran the benchmark. It now shows that the new implementation is only better with the Latin-1 string encoding, but worse in UTF-8 standalone Starlark. Not sure whether that makes it a good idea.

@tetromino
Copy link
Contributor

Interesting. In that case, we may want different behavior depending on whether the string is Latin1...

@fmeum
Copy link
Collaborator Author

fmeum commented Nov 25, 2024

Interesting. In that case, we may want different behavior depending on whether the string is Latin1...

You will probably be interested in #24417 :-)

Positional access via `String#charAt` is slightly faster than precreating the char array for Latin-1 strings and much faster for UTF-8 strings. It allocates less in both cases.

Also adds a `--latin1` flag to the `Benchmarks` tool that allows benchmarking against Bazel's way of parsing Starlark files.

Before:
```
INFO: Running command line: bazel-bin/src/test/java/net/starlark/java/eval/Benchmarks --filter bench_format --seconds 20
File src/test/java/net/starlark/java/eval/testdata/bench_string.star:
benchmark                        ops     cpu/op    wall/op   steps/op   alloc/op
bench_format               134217727      169ns      168ns          7       495B

INFO: Running command line: bazel-bin/src/test/java/net/starlark/java/eval/Benchmarks --filter bench_format --seconds 20 --latin1
File src/test/java/net/starlark/java/eval/testdata/bench_string.star:
benchmark                        ops     cpu/op    wall/op   steps/op   alloc/op
bench_format               268435455      122ns      121ns          7       495B
```

After:
```
INFO: Running command line: bazel-bin/src/test/java/net/starlark/java/eval/Benchmarks --filter bench_format --seconds 20
File src/test/java/net/starlark/java/eval/testdata/bench_string.star:
benchmark                        ops     cpu/op    wall/op   steps/op   alloc/op
bench_format               268435455      110ns      109ns          7       479B

INFO: Running command line: bazel-bin/src/test/java/net/starlark/java/eval/Benchmarks --filter bench_format --seconds 20 --latin1
File src/test/java/net/starlark/java/eval/testdata/bench_string.star:
benchmark                        ops     cpu/op    wall/op   steps/op   alloc/op
bench_format               268435455      113ns      112ns          7       479B
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-review PR is awaiting review from an assigned reviewer team-Starlark-Interpreter Issues involving the Starlark interpreter used by Bazel
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants