Stack Hash Optimization Notes (2026)
Context
Synthesis profiling with ~10k resources showed stack hashing and config serialization as the main CPU costs. The goal was to reduce hot-path overhead while preserving deterministic hashes and cache invalidation semantics.
All measurements below were taken with:
LOGURU_LEVEL=ERROR uv run libs/netherframe.core/tests/performance/perf_profile.py \
--mode explicit \
--count 10000 \
--no-results
(LOGURU_LEVEL is loguru's own variable: the level of the library's default logging.)
The benchmark creates 100, 1k, and 10k resources and reports synthesis time. Results are hardware-dependent; use them for relative comparisons only.
Key Changes (Kept)
1) Hash input uses cached resource configs
- compute_resource_hash now uses _to_config() or the resource attributes directly, rather than hashing the entire __dict__.
- Stack hashing reuses _get_resource_config_cached and hashes that payload for each resource.
2) Hash payload key rename
- Hash input now uses nf_config as the config key for clarity.
- This changes hash values once, which is expected and only affects cache invalidation.
3) Faster serialize_for_hash
- Reduced attribute lookups, simplified recursion, and moved runtime-value checks to avoid extra cost for common dict/list/tuple cases.
- Hot-path overhead drops and serialize_for_hash remains deterministic.
4) Faster cached config lookup
- _get_resource_config_cached now uses resource-id keyed caches instead of string keys, avoiding repeated _get_resource_key work.
- Uses the version tracker’s internal maps directly for lower overhead.
Results (Synth Time)
These are representative runs on the dev machine during this effort:
- Baseline (before hash path changes): ~0.344s at 10k resources
- After hash-path refactor: ~0.253s
- After
serialize_for_hashoptimizations: ~0.234s - After cached-config lookups by id: ~0.222s
Overall: ~35% reduction from the baseline for 10k-resource synthesis.
Experiments Reverted (Regressions or No Gain)
The following were tested and removed to keep only consistent wins:
- Hash serializer toggle (an environment switch) for json/orjson control
- Default path already uses orjson when available; explicit toggle did not improve runtime.
- Hash payload caching by version
- Added overhead on first-run synth; no clear win for this workload.
- Streamed stack hash mode
- Increased hash time vs the existing payload + sha256 approach.
If reintroducing any of these, measure both first-run and repeated synth cases.
Guidance for Future Work
If further improvements are needed:
- Focus on
_get_resource_config_cachedandresolve_valueinlibs/netherframe.core/netherframe/core/values/resolver.py. - Measure with the same 10k-resource profile to avoid noise from logging and I/O.
- Keep changes deterministic; hash stability is required for synthesis caching.
Files Touched
libs/netherframe.core/netherframe/core/constructs/caching.pylibs/netherframe.core/tests/performance/perf_profile.py(benchmark driver)