perf(version.bzl): parse the release-only versions without the parser The vast majority of the versions that are parsed only have the release segment, e.g. `3.11` or `3.11.4`, and going through the parser, which handles one character at a time, takes around 900 steps for them. Handle them with `str.split` instead, which takes around 50 steps. This makes the evaluation of the markers that compare the Python version several times cheaper, because the versions on both sides of the comparison are parsed every time.
diff --git a/python/private/version.bzl b/python/private/version.bzl index 9be9a48..410708d 100644 --- a/python/private/version.bzl +++ b/python/private/version.bzl
@@ -535,6 +535,40 @@ """ return _parse(version, strict = True)["norm"] +def _parse_release_only(version, is_prefix): + """Parse a version that only has the release segment, e.g. `3.11.4`. + + The vast majority of the versions that we handle are like this and using + the string methods is an order of magnitude cheaper than the parser. + + Args: + version: {type}`str` the version without the leading and trailing + whitespace. + is_prefix: {type}`bool` whether the `.*` suffix was removed. + + Returns: + The same as `_parse` or `None` if the full parser needs to be used. + """ + if not version.replace(".", "").isdigit(): + return None + + parts = version.split(".") + if "" in parts: + return None + + # PEP 440: Integer Normalization + norm = ".".join([str(int(part)) for part in parts]) + return { + "dev": "", + "epoch": "", + "is_prefix": is_prefix, + "local": "", + "norm": norm, + "post": "", + "pre": "", + "release": norm, + } + def _parse(version_str, strict = True, _fail = fail): """Split the version into its normalized segments. @@ -558,6 +592,10 @@ if is_prefix: version = version[:-len(".*")] + release_only = _parse_release_only(version, is_prefix) + if release_only: + return release_only + parser = _new(version) accept(parser, _in(["v", "V"]), "") # PEP 440: Preceding v character accept_epoch(parser)
diff --git a/tests/perf/README.md b/tests/perf/README.md index 665a17c..ad58251 100644 --- a/tests/perf/README.md +++ b/tests/perf/README.md
@@ -80,11 +80,11 @@ | Workload | Steps | Bazel, µs | |---|---:|---:| -| `evaluate_long` | 11056 | 2009.7 | +| `evaluate_long` | 6376 | 781.4 | | `evaluate_non_strict` | 703 | 74.2 | | `evaluate_string` | 362 | 34.9 | | `evaluate_two_strings` | 750 | 81.3 | -| `evaluate_version` | 1975 | 432.7 | +| `evaluate_version` | 525 | 69.9 | | `tokenize_long` | 4870 | 424.0 | | `tokenize_no_whitespace` | 563 | 49.6 | | `tokenize_version` | 305 | 27.5 | @@ -93,9 +93,19 @@ | `version_is_ge` | 54 | 6.2 | | `version_is_lt` | 48 | 6.3 | | `version_key` | 26 | 2.7 | -| `version_normalize` | 950 | 218.5 | -| `version_parse_all_segments` | 1514 | 335.6 | -| `version_parse_invalid` | 628 | 137.6 | -| `version_parse_pre_release` | 1016 | 239.6 | -| `version_parse_prefix` | 784 | 178.4 | -| `version_parse_release` | 881 | 201.1 | +| `version_normalize` | 24 | 4.8 | +| `version_parse_all_segments` | 1518 | 329.1 | +| `version_parse_invalid` | 632 | 136.1 | +| `version_parse_pre_release` | 1020 | 238.3 | +| `version_parse_prefix` | 52 | 9.6 | +| `version_parse_release` | 53 | 9.5 | + +## History + +The notable changes of the results, the numbers are the steps. + +| Change | Workload | Before | After | +|---|---|---:|---:| +| Parse the release-only versions without the parser | `version_parse_release` | 881 | 53 | +| | `evaluate_version` | 1975 | 525 | +| | `evaluate_long` | 11056 | 6376 |
diff --git a/tests/perf/steps_test.py b/tests/perf/steps_test.py index 4fc64a4..8a4e10e 100644 --- a/tests/perf/steps_test.py +++ b/tests/perf/steps_test.py
@@ -9,11 +9,11 @@ # numbers with the output of `bazel run //tests/perf:benchmark`. _STEPS = { # keep sorted - "evaluate_long": 11056, + "evaluate_long": 6376, "evaluate_non_strict": 703, "evaluate_string": 362, "evaluate_two_strings": 750, - "evaluate_version": 1975, + "evaluate_version": 525, "tokenize_long": 4870, "tokenize_no_whitespace": 563, "tokenize_version": 305, @@ -22,12 +22,12 @@ "version_is_ge": 54, "version_is_lt": 48, "version_key": 26, - "version_normalize": 950, - "version_parse_all_segments": 1514, - "version_parse_invalid": 628, - "version_parse_pre_release": 1016, - "version_parse_prefix": 784, - "version_parse_release": 881, + "version_normalize": 24, + "version_parse_all_segments": 1518, + "version_parse_invalid": 632, + "version_parse_pre_release": 1020, + "version_parse_prefix": 52, + "version_parse_release": 53, } # The number of the steps slightly depends on the Python version.
diff --git a/tests/version/version_test.bzl b/tests/version/version_test.bzl index 15a4681..7e1ecea 100644 --- a/tests/version/version_test.bzl +++ b/tests/version/version_test.bzl
@@ -162,6 +162,30 @@ _tests.append(_test_parse_prefix) +def _test_parse_release_only(env): + # The versions with only the release segment are not parsed by the full + # parser, which is used if there is the `v` prefix. + for input in [ + "0", + "1", + "3.11", + "3.11.4", + "03.011.04", + "2023.07.19", + "1.2.3.4.5.6", + "3.11.*", + " 3.11 ", + ]: + got = version.parse(input) + want = version.parse("v" + input.strip()) + where = env.expect.where(input = input) + where.that_str(got.string).equals(want.string) + where.that_bool(got.is_prefix).equals(want.is_prefix) + where.that_bool(version.key(got) == version.key(want)).equals(True) + where.that_str(version.normalize(input.removesuffix(".*"))).equals(want.string) + +_tests.append(_test_parse_release_only) + def _test_ordering(env): want = [ # Taken from https://peps.python.org/pep-0440/#summary-of-permitted-suffixes-and-relative-ordering