Skip to content

fix(gnovm): meter GC traversal of large primitive-keyed maps#5884

Draft
omarsy wants to merge 1 commit into
gnolang:masterfrom
omarsy:fix/gc-map-visit-gas
Draft

fix(gnovm): meter GC traversal of large primitive-keyed maps#5884
omarsy wants to merge 1 commit into
gnolang:masterfrom
omarsy:fix/gc-map-visit-gas

Conversation

@omarsy

@omarsy omarsy commented Jul 2, 2026

Copy link
Copy Markdown
Member

Fixes #5883.

Problem

GC visit accounting undercharged the O(N) traversal of a fully-primitive map (map[int]int etc.). MapValue.VisitAssociated walks the MapList in O(N), but the visit counter (GCVisitorFn) only increments when vis is called on a boxed key/value object. For map[int]int both TypedValue.V are nil (data lives in .N), so vis is never called for the entries — an N-entry map counted as ~1 visit and gcVisitGas charged the ~29-gas flat minimum for an O(N) traversal.

Fix

When visiting a MapValue, count one visit per entry whose key and value are both unboxed (Key.V == nil && Value.V == nil) — exactly the entries that otherwise contribute zero. Boxed keys/values are already counted when vis reaches them, so object/mixed-keyed maps are unaffected (no gas change). Deterministic (walks MapList in insertion order, not the Go vmap).

Test

gnovm/pkg/gnolang/gc_map_visit_test.go: a 1000-entry map[int]int now yields visitCount = 1001 (was 1); gcVisitGas 40040 (was 29). Verified red→green (fails on unfixed code).

Verification (consensus-gas change)

  • go build ./gnovm/... + go vet + gofmt clean.
  • ./gno.land/pkg/sdk/vm -run Gas, full ./gno.land/pkg/integration -run TestTestdata (txtar), ./gnovm/pkg/gnolang -run Files -test.shortall green. Object/mixed maps get +0, so no gas goldens shift.
  • Adversarially reviewed across 14 map types (map[int]int/bool/string, map[[1]int]int, map[int]*S, slice/struct/interface values, nested, interface{} holding primitive-vs-pointer): fully-unboxed entries counted, boxed never double-counted; overflow-safe (~9 orders of magnitude headroom).

Note

A fully-primitive map is list-walked twice per GC (once to count, once in VisitAssociated). Kept surgical to avoid changing gas for object maps; a single-walk form would need threading the count through the Visitor signature — happy to adjust if you'd prefer that.

🤖 Generated with Claude Code

@github-actions github-actions Bot added the 📦 🤖 gnovm Issues or PRs gnovm related label Jul 2, 2026
@Gno2D2 Gno2D2 added the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Jul 2, 2026
@Gno2D2

Gno2D2 commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

🛠 PR Checks Summary

All Automated Checks passed. ✅

Manual Checks (for Reviewers):
  • IGNORE the bot requirements for this PR (force green CI check)
Read More

🤖 This bot helps streamline PR reviews by verifying automated checks and providing guidance for contributors and reviewers.

✅ Automated Checks (for Contributors):

🟢 Maintainers must be able to edit this pull request (more info)
🟢 Pending initial approval by a review team member, or review from tech-staff

☑️ Contributor Actions:
  1. Fix any issues flagged by automated checks.
  2. Follow the Contributor Checklist to ensure your PR is ready for review.
    • Add new tests, or document why they are unnecessary.
    • Provide clear examples/screenshots, if necessary.
    • Update documentation, if required.
    • Ensure no breaking changes, or include BREAKING CHANGE notes.
    • Link related issues/PRs, where applicable.
☑️ Reviewer Actions:
  1. Complete manual checks for the PR, including the guidelines and additional checks if applicable.
📚 Resources:
Debug
Automated Checks
Maintainers must be able to edit this pull request (more info)

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 The base branch matches this pattern: ^master$
    └── 🟢 The pull request was created from a fork (head branch repo: omarsy/gno)

Then

🟢 Requirement satisfied
└── 🟢 Maintainer can modify this pull request

Pending initial approval by a review team member, or review from tech-staff

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 The base branch matches this pattern: ^master$
    └── 🟢 Not (🔴 Pull request author is a member of the team: tech-staff)

Then

🟢 Requirement satisfied
└── 🟢 If
    ├── 🟢 Condition
    │   └── 🟢 Or
    │       ├── 🔴 At least one of these user(s) reviewed the pull request: [aronpark1007 davd-gzl jefft0 notJoon omarsy MikaelVallenet] (with state "APPROVED")
    │       ├── 🔴 At least 1 user(s) of the team tech-staff reviewed pull request
    │       └── 🟢 This pull request is a draft
    └── 🟢 Then
        └── 🟢 Not (🔴 This label is applied to pull request: review/triage-pending)

Manual Checks
**IGNORE** the bot requirements for this PR (force green CI check)

If

🟢 Condition met
└── 🟢 On every pull request

Can be checked by

  • Any user with comment edit permission

@omarsy omarsy marked this pull request as draft July 2, 2026 13:30
@Gno2D2 Gno2D2 removed the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Jul 2, 2026
MapValue.VisitAssociated walks the map's MapList in O(N), but the GC visit counter (GCVisitorFn) only increments when it calls vis on a boxed key/value object. For a map whose entries are unboxed primitives (map[int]int etc. — data in TypedValue.N, V == nil), vis is never called for the entries, so an N-entry map was counted as ~1 visit and gcVisitGas charged the flat ~29-gas minimum for an O(N) traversal (gnolang#5883).

When visiting a MapValue, count one visit per entry whose key AND value are both unboxed (Key.V == nil && Value.V == nil) — exactly the entries that otherwise contribute zero. Boxed keys/values are already counted when vis reaches them, so object/mixed-keyed maps are unaffected (no gas change). Deterministic (walks MapList in insertion order).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@omarsy omarsy force-pushed the fix/gc-map-visit-gas branch from 101c53e to fb16708 Compare July 2, 2026 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

📦 🤖 gnovm Issues or PRs gnovm related

Projects

Development

Successfully merging this pull request may close these issues.

GC visit gas undercharges O(N) traversal of large primitive-keyed maps

2 participants