Skip to content

[1209] 优化 lolly hashmap 性能 - #4355

Merged
da-liii merged 5 commits into
mainfrom
da/1209/hashmap
Aug 18, 2026
Merged

[1209] 优化 lolly hashmap 性能#4355
da-liii merged 5 commits into
mainfrom
da/1209/hashmap

Conversation

@da-liii

@da-liii da-liii commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

概要

优化 `lolly/Kernel/Containers/hashmap` 运行时性能,先以 nanobench 基线对比,再做三处优化:

  1. resize 重挂节点:`resize()` 原为每个条目重新分配 list 节点并析构旧节点、逐个重哈希 key;改为 raw 指针 + 手工 `ref_count` 调整做节点所有权转移,并复用条目已存的 `code`(对 string key 省一遍字符串扫描)。为此在 `list.hpp` 给 `hashmap_rep` 加了 friend。
  2. 桶下标混合:新增 `hash_bucket(hv, n)`,`(unsigned)hv * 2654435769u & (n-1)`。`hash(int)` 为恒等函数,原 `hv & (n-1)` 在等差键/对齐指针键下会挤进同一桶退化为链表扫描;所有取桶下标的调用点统一改用。
  3. join 复用哈希码:`join()` 直接用源条目的 `code` 定位/插入,免去二次哈希。

Benchmark(releasedbg,linux/x86_64,200k int 键)

场景 提升
insert 7.89ms 6.51ms ~17%
lookup 命中 1.16ms 0.91ms ~22%
contains 753µs 594µs ~21%
reset 全部 425µs 358µs ~16%
operator== 1.58ms 1.40ms ~11%
单次扩容 381ns 288ns ~24%

验证

  • `xmake test -P lolly` 全量 35 例连跑 3 次稳定通过(唯一失败的 `shared_lib_test` 在干净树上同样失败,环境问题)
  • `xmake b stem` 构建通过

任务文档:`devel/1209.md`

🤖 Generated with Claude Code

da-liii and others added 5 commits August 18, 2026 01:19
Co-Authored-By: Claude <noreply@anthropic.com>
…pp 并覆盖 hashset

Co-Authored-By: Claude <noreply@anthropic.com>
@da-liii
da-liii merged commit 757d132 into main Aug 18, 2026
7 checks passed
@da-liii
da-liii deleted the da/1209/hashmap branch August 18, 2026 03:01
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.

2 participants