Skip to content

perf(params): flatten tuple rows -- 52% less peak memory - #920

Open
dexhunter wants to merge 1 commit into
polakowo:masterfrom
dexhunter:perf/flatten-tuple-rows
Open

dexhunter wants to merge 1 commit into
polakowo:masterfrom
dexhunter:perf/flatten-tuple-rows

Conversation

@dexhunter

Copy link
Copy Markdown

TL;DR

  • create_param_combs saves 47,433,280 bytes of peak allocation when building a grid of 398,000 window-pair and threshold combinations.
  • Read each column by tuple index to skip an iterator allocation for every row.
  • With allocation tracing enabled, median runtime fell from 667.1 ms to 338.4 ms.
  • All five native parameter, mapping, and enum tests passed on Python 3.12.

What & Why

Transposing rows with zip(*rows) keeps an iterator alive for each row. Plain tuples already support direct access by index.

The helper takes a snapshot of the rows and reads one column at a time. The shortest row sets the column count, so uneven rows still truncate at the same point.

Why this is safe and easy to maintain

The change fits in the existing flattening helper. The output stays unchanged, and other iterables still use the existing transpose operation. The parameter tests check column order for both simple and nested combinations.

Validation

The table shows median peak traced bytes for one call, including the returned lists.

Situation Before After Change
200 windows, 20 thresholds (398,000 rows) 90,549,384 B 43,116,104 B 52.4% less
30 windows, 4 thresholds (1,740 rows) 419,440 B 212,688 B 49.3% less
30 windows, return 435 pairs at depth 1 28,648 B 28,648 B 0%

Five window and threshold selections gave these results for the full grid and its edge cases.

Statistic Value
Old -> new (mean) 86.4 MiB -> 41.1 MiB (52.4% less peak memory)
95% CI of the new/old ratio (bootstrap, 10,000 resamples) 0.476–0.476 (52.4–52.4% less peak memory)
Mean ± SD of the change per input -45.2 MiB ± 0 bytes
95% CI of the mean change (t-interval) -45.2 MiB to -45.2 MiB
Number of fixed inputs (n) 5 (3 runs)
Runtime on the same harness (median wall) 667.1 ms -> 338.4 ms (48.9% faster)

Without allocation tracing, the median call took 97.7 ms before and 91.7 ms after (wall ratio 0.939) with default thread settings.

To reproduce the large grid measurement on each checkout, run this from the repository root.

python - <<'PY'
import gc
from itertools import combinations, product
import tracemalloc
from vectorbt.utils.params import create_param_combs

grid = (product, (combinations, list(range(2, 202)), 2), [i / 1000 for i in range(20)])
gc.collect()
tracemalloc.start()
columns = create_param_combs(grid)
print(tracemalloc.get_traced_memory()[1])
PY

Native test command (5 passed, one NumPy deprecation warning):

uv run --no-cache --no-project --python 3.12 --with '.[test]' python -B -m pytest -p no:cacheprovider -q tests/test_utils.py::TestParams tests/test_utils.py::TestMapping tests/test_utils.py::TestEnum

AI assistance

This PR was prepared with AI assistance from Aiden. Candidates were tracked with Weco Observe.

Weco share link: https://dashboard.weco.ai/share/dsHaLmQEI7IQ3YPUXLd_9PX5Y1_0c8DG

Read columns by index for plain tuple rows. Keep the shortest row as the column limit.

Co-Authored-By: Aiden <aiden@weco.ai>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant