Performance of the Starlark code

The version and the environment marker handling is executed many times when pip.parse is evaluated: for every requirement, for every target platform and for every Python version. This directory has the tools to ensure that this code stays fast.

The measured workloads are in workloads.bzl.

Counting the steps

Bazel cannot count how many steps the Starlark interpreter executes. However, the .bzl files that only have pure functions are almost valid Python, so starlark_shim.py executes them with CPython and counts the executed lines of the .bzl files, which we call steps.

The number of steps does not depend on the machine and it changes when the amount of work that the code does changes. It only slightly depends on the Python version, hence the test below has a tolerance of 5%.

The shim is not a Starlark interpreter, it only supports what the measured files need. If you start using something that is not supported, extend the shim.

Print the number of steps that the workloads take:

bazel run //tests/perf:benchmark

Only run the workloads that have version in the name:

bazel run //tests/perf:benchmark -- version

//tests/perf:steps_test runs as part of the regular tests and it fails if the number of steps of a workload changes by more than that. If the code got slower, then either fix the code or, if the extra work is necessary, update the numbers in steps_test.py. If the code got faster, then update the numbers, so that it stays that way. In both cases update the results below.

Measuring with Bazel

The steps are a proxy, so there is also a benchmark that measures the workloads with Bazel itself:

bazel run //tests/perf:bazel_benchmark

It builds //tests/perf:bazel_benchmark_analysis, which executes the workload that is selected with --//tests/perf:workload for --//tests/perf:iterations times during the analysis. Starlark does not have a clock, so the time of a single iteration is the difference of the wall time of 2 builds divided by the difference of the iterations. This takes a long time and the results depend on the machine, hence it is not a test.

If you run Bazel with startup options, pass the same ones to the benchmark, otherwise the Bazel server restarts for every build:

bazel --host_jvm_args=-Xmx2g run //tests/perf:bazel_benchmark -- \
    --bazel="bazel --host_jvm_args=-Xmx2g"

The same target can be used to profile the code with --starlark_cpu_profile, see https://bazel.build/rules/performance#performance-profiling:

bazel build //tests/perf:bazel_benchmark_analysis \
    --//tests/perf:workload=evaluate_long \
    --//tests/perf:iterations=10000 \
    --starlark_cpu_profile=/tmp/profile.gz

Results

The time is of Bazel 9.1.1 on a laptop, only the ratios are meaningful.

WorkloadStepsBazel, µs
evaluate_long1974383.6
evaluate_long_parsed693114.1
evaluate_non_strict28658.4
evaluate_string12224.5
evaluate_two_strings22943.0
evaluate_version28356.4
evaluate_version_parsed13221.3
parse_long1284231.5
tokenize_long514112.9
tokenize_no_whitespace8423.0
tokenize_version4810.8
version_is_compatible648.6
version_is_eq_prefix102.5
version_is_ge546.2
version_is_lt486.3
version_key262.7
version_normalize244.8
version_parse_all_segments1518329.1
version_parse_invalid632136.1
version_parse_pre_release1020238.3
version_parse_prefix529.6
version_parse_release539.5

History

The notable changes of the results, the numbers are the steps.

ChangeWorkloadBeforeAfter
Parse the release-only versions without the parserversion_parse_release88153
evaluate_version1975525
evaluate_long110566376
Rewrite the marker evaluationtokenize_long4870514
evaluate_string362122
evaluate_version525283
evaluate_long63761974
Evaluate the markers that are parsed with parseevaluate_version_parsed283132
evaluate_long_parsed1974693