Skip to content

fix: build json dict in key order when parsing - #71

Merged
swordqiu merged 1 commit into
yunionio:masterfrom
swordqiu:hotfix/qj-dict-key-accumulate
Sep 16, 2026
Merged

swordqiu merged 1 commit into
yunionio:masterfrom
swordqiu:hotfix/qj-dict-key-accumulate

Conversation

@swordqiu

Copy link
Copy Markdown
Member

变更

parseDict 原先每读到一个键就 sortedmap.Add 一次。有序表插入乱序键时需要搬移整个尾部,键名降序排列时每次插入都搬移全部已有元素。

改为先把键值收集到 map,循环结束后 sort.Strings 排序再按序插入 —— 有序插入退化为 append,整体由 O(n²) 降为 O(n log n)。

键名重复时仍是后出现者覆盖先出现者,与原行为一致。

测试

新增 parse_dict_test.go:

  • TestParseDictKeyOrder 覆盖升序/降序两种键序
  • TestParseDictDuplicateKey 确认重复键的覆盖语义未变
  • TestParseDictManyKeys 以 20 万降序键作为回归守卫(含耗时上限)
  • BenchmarkParseDictDescendingKeys 供后续观察

实测 20 万降序键:修复前约 95 s,修复后 0.33 s。

go test ./... 全量通过。

Collect the keys while parsing an object and insert them into the sorted
map in key order, instead of inserting each key as it is read. Inserting
out of order into a sorted map shifts the whole tail on every insert.
@swordqiu
swordqiu merged commit d898807 into yunionio:master Sep 16, 2026
1 check passed
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