|
| 1 | +# Copyright DataStax, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +Micro-benchmark: pre-computed int32_pack constants for null/unset markers. |
| 17 | +
|
| 18 | +Measures the speedup from using pre-computed _INT32_NEG1/_INT32_NEG2 |
| 19 | +constants vs calling int32_pack(-1)/int32_pack(-2) per parameter. |
| 20 | +
|
| 21 | +Run: |
| 22 | + python benchmarks/bench_protocol_write_value.py |
| 23 | +""" |
| 24 | +import timeit |
| 25 | +import struct |
| 26 | + |
| 27 | +int32_pack = struct.Struct('>i').pack |
| 28 | + |
| 29 | +# Pre-computed constants (the optimization) |
| 30 | +_INT32_NEG1 = int32_pack(-1) |
| 31 | +_INT32_NEG2 = int32_pack(-2) |
| 32 | + |
| 33 | +_UNSET_VALUE = object() |
| 34 | + |
| 35 | + |
| 36 | +def bench_write_value_constants(): |
| 37 | + """Benchmark pre-computed constants vs per-call int32_pack.""" |
| 38 | + # Simulate a typical parameter list: mix of values, nulls, and unsets |
| 39 | + params = [b'\x00\x00\x00\x01'] * 8 + [None] * 3 + [_UNSET_VALUE] * 1 |
| 40 | + |
| 41 | + def run_precomputed(): |
| 42 | + parts = [] |
| 43 | + _parts_append = parts.append |
| 44 | + _i32 = int32_pack |
| 45 | + for param in params: |
| 46 | + if param is None: |
| 47 | + _parts_append(_INT32_NEG1) |
| 48 | + elif param is _UNSET_VALUE: |
| 49 | + _parts_append(_INT32_NEG2) |
| 50 | + else: |
| 51 | + _parts_append(_i32(len(param))) |
| 52 | + _parts_append(param) |
| 53 | + return b"".join(parts) |
| 54 | + |
| 55 | + def run_pack_each_time(): |
| 56 | + parts = [] |
| 57 | + _parts_append = parts.append |
| 58 | + _i32 = int32_pack |
| 59 | + for param in params: |
| 60 | + if param is None: |
| 61 | + _parts_append(_i32(-1)) |
| 62 | + elif param is _UNSET_VALUE: |
| 63 | + _parts_append(_i32(-2)) |
| 64 | + else: |
| 65 | + _parts_append(_i32(len(param))) |
| 66 | + _parts_append(param) |
| 67 | + return b"".join(parts) |
| 68 | + |
| 69 | + # Verify identical output |
| 70 | + assert run_precomputed() == run_pack_each_time() |
| 71 | + |
| 72 | + n = 500_000 |
| 73 | + t_precomputed = timeit.timeit(run_precomputed, number=n) |
| 74 | + t_pack = timeit.timeit(run_pack_each_time, number=n) |
| 75 | + |
| 76 | + print(f"Pre-computed constants ({n} iters, {len(params)} params): " |
| 77 | + f"{t_precomputed:.3f}s ({t_precomputed / n * 1e6:.2f} us/call)") |
| 78 | + print(f"Pack each time ({n} iters, {len(params)} params): " |
| 79 | + f"{t_pack:.3f}s ({t_pack / n * 1e6:.2f} us/call)") |
| 80 | + speedup = t_pack / t_precomputed |
| 81 | + print(f"Speedup: {speedup:.2f}x") |
| 82 | + |
| 83 | + |
| 84 | +def main(): |
| 85 | + bench_write_value_constants() |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == '__main__': |
| 89 | + main() |
0 commit comments