Skip to content

perf(HeatMap): build weighted layers -- 46.7% less peak memory - #2376

Open
dexhunter wants to merge 1 commit into
python-visualization:mainfrom
dexhunter:perf/heatmap-weighted-rows
Open

dexhunter wants to merge 1 commit into
python-visualization:mainfrom
dexhunter:perf/heatmap-weighted-rows

Conversation

@dexhunter

Copy link
Copy Markdown

TL;DR

  • Building a HeatMap from 300,000 weighted points saves 34.3 MiB of peak memory.
  • Build each row at its final size, reuse common weights, and check the rows directly for NaNs.
  • With memory tracing on, median constructor runtime fell from 3643.5 ms to 3251.8 ms.
  • All 109 pytest cases passed across the heatmap, marker cluster, and utility tests.

What & Why

The constructor built lists with spare room and copied the full dataset into a NumPy array to check for NaNs.
It now joins the validated coordinates and weights into one list per point and scans equal-length rows in place.

A small dictionary keeps up to 256 converted weights for reuse. Each point still gets its own list, and negative zero keeps its sign.

Validation

The memory tests used weighted NumPy arrays on Python 3.12 with 101 different weights. A separate timing test used unique random weights.

Situation Before After Change
One weighted point 1,944 B 2,240 B 296 B more
10,000 weighted points 2,566,224 B 1,374,072 B 1,192,152 B less
300,000 weighted points 77,002,744 B 41,010,568 B 35,992,176 B less
300,000 points, unique random weights, median wall time 670 ms 670 ms No change

The large survey gave these results across five row orders.

Statistic Value
Old -> new (median) 73.4 MiB -> 39.1 MiB (46.7% less peak memory)
95% CI of the new/old ratio (bootstrap, 10,000 resamples) 0.533–0.533 (46.7–46.7% less peak memory)
Mean ± SD of the change per input -34.3 MiB ± 0 bytes
95% CI of the mean change (t-interval) -34.3 MiB to -34.3 MiB
Number of fixed inputs (n) 5 (3 runs)
Runtime on the same harness (median wall) 3643.5 ms -> 3251.8 ms (10.6% faster)

With default thread settings and tracing off, median wall time was 667 ms before and 659 ms after (new/old 0.988, seven calls each).

To measure peak memory from either checkout, run:

python - <<'PY'
import gc
import tracemalloc
import numpy as np
from folium.plugins import HeatMap

i = np.arange(300_000, dtype=float)
np.random.default_rng(1).shuffle(i)
data = np.column_stack((35 + i % 1000 / 1000, -120 + (i // 1000) / 1000, (i % 101) / 100))
HeatMap([[1., 2., 3.]])
gc.collect()
tracemalloc.start()
layer = HeatMap(data)
print(tracemalloc.get_traced_memory()[1])
PY

The native tests ran with this command and reported one pytest deprecation warning.

python -m pip install --no-cache-dir -r requirements.txt pytest pandas && python -B -m pytest -p no:cacheprovider tests/plugins/test_heat_map.py tests/plugins/test_fast_marker_cluster.py tests/plugins/test_marker_cluster.py tests/test_utilities.py -q

Why this is safe and easy to maintain

The constructor keeps the same output and uses the existing coordinate validator. NumPy still reports inconsistent row lengths.
Tests in test_heat_map.py cover rendering and coordinate bounds. Separate checks covered row order, independent lists, numeric types, warnings, and errors for invalid inputs.

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/PeiGnOxZPFCx4S_2249l_JmuNDCvDWyM

Build compact point lists, reuse common weight values, and check equal-length rows for NaNs in place.

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