mirror of
https://ghfast.top/https://github.com/aeroxw/easy_tdx_max.git
synced 2026-09-12 19:14:19 +08:00
fix: 寻优指标缓存键改内容哈希 — 修复 v1.25 起两段式加速静默失效
缓存键此前用数组对象 id 做签名,而引擎每次 run 会重建数组对象, id 逐网格点漂移导致跨点永不命中——ParamGridOptimizer 的指标缓存 全程 0 命中,两段式加速自 v1.25 引入起就没生效过。正确性不受影响 (缓存未命中即走重算路径,结果与无缓存逐位一致),属纯性能回归; test_optimizer_cache_reuse_across_grid_points 因此一直在失败。 - IndicatorCache._atom 数组签名由 (id, shape) 改为 (dtype, shape, blake2b 内容摘要 16 字节):同值不同对象视为同一 数据;NaN 按字节参与哈希,位模式不同只多算一次、不会误命中 - 删除防 id 复用的 _array_refs 强引用表(内容寻址下不再需要, 缓存不再延长数组生命周期) - 新增缓存级回归测试:同内容不同对象必须命中;原失败测试转绿 - 实测 16 点网格命中率 38%(修复前恒为 0);全量 pytest 1613 通过、ruff/mypy 全绿
This commit is contained in:
@@ -62,6 +62,22 @@ def test_indicator_cache_distinguishes_arrays():
|
||||
assert cache.misses == 2 # 不同数组不误命中
|
||||
|
||||
|
||||
def test_indicator_cache_hits_copied_array_with_same_content():
|
||||
"""同内容、不同对象(引擎每次 run 重建数组的情形)必须命中。
|
||||
|
||||
回归:键曾用对象 id 做数组签名,引擎逐 run 重建数组对象导致寻优
|
||||
缓存全程 0 命中(两段式加速静默失效)。
|
||||
"""
|
||||
from easy_tdx.MyTT import MA
|
||||
|
||||
arr = _pool_df(100)["close"].to_numpy()
|
||||
cache = IndicatorCache()
|
||||
cache.get_or_compute(MA, (arr, 5), {})
|
||||
cache.get_or_compute(MA, (arr.copy(), 5), {}) # 同内容不同对象
|
||||
assert cache.hits == 1
|
||||
assert cache.misses == 1
|
||||
|
||||
|
||||
# ── 优化器集成(缓存命中 + 结果一致 + 并行)─────────────────────────────────
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user