Skip to content

perf(vm): lazily clone the type-check cache per transaction#5901

Draft
omarsy wants to merge 1 commit into
gnolang:masterfrom
omarsy:pr/lazy-typecheck-cache-clone
Draft

perf(vm): lazily clone the type-check cache per transaction#5901
omarsy wants to merge 1 commit into
gnolang:masterfrom
omarsy:pr/lazy-typecheck-cache-clone

Conversation

@omarsy

@omarsy omarsy commented Jul 3, 2026

Copy link
Copy Markdown
Member

Summary

MakeGnoTransactionStore ran maps.Clone(vm.typeCheckCache) on every transaction (via the BeginTxHook), but only AddPackage and Run ever consume that cache — MsgCall and query eval, the common case, never type-check. Every such transaction paid for a full clone of the shared cache (~stdlib package count) and discarded it unused.

This stores a small holder that clones lazily on first access instead:

type typeCheckCacheHolder struct {
    base   gno.TypeCheckCache // shared, treated as read-only
    cloned gno.TypeCheckCache // per-tx working copy; nil until first get
}

func (h *typeCheckCacheHolder) get() gno.TypeCheckCache {
    if h.cloned == nil {
        h.cloned = maps.Clone(h.base)
    }
    return h.cloned
}

Transactions that never type-check skip the clone entirely; AddPackage/Run still get exactly one clone, shared across a tx's messages.

Why it's safe (no consensus / gas / behavior change)

  • Off the metered path. The clone happens before the gas meter is wired to the preprocess allocator (SetPreprocessAllocator / SetGasMeter run afterward), so it never consumed tx gas in either the old or new code. No gas goldens change.
  • Single-clone-per-tx preserved. BeginTxHook fires once per transaction, so all messages in a tx share one holder → one clone, identical to the previous eager behavior.
  • Isolation preserved. Each tx gets a distinct holder over a read-only base (vm.typeCheckCache, only written at init/restart); the consumer writes to the clone, never base.
  • Concurrency. A holder belongs to one tx's context and, like the rest of per-tx execution (store, allocator, gas meter), assumes a single driving goroutine; documented on the type. Verified with go test -race.

Impact

Pure node-side work reduction: eliminates one maps.Clone of the shared type-check cache for every non-type-checking transaction (the majority). No user-visible or consensus-visible effect.

Changed files

  • gno.land/pkg/sdk/vm/keeper.go — lazy holder + accessor (20 lines).

Verification

  • go test ./gno.land/pkg/sdk/vm/
  • go test -race ./gno.land/pkg/sdk/vm/
  • gofmt / go vet clean

🤖 Generated with Claude Code

MakeGnoTransactionStore ran maps.Clone(vm.typeCheckCache) on every
transaction via the BeginTxHook, but only AddPackage and Run consume
the cache — MsgCall and query eval, the common case, never type-check.
Every such transaction paid for a full clone of the shared cache
(~stdlib package count) and threw it away unused.

Store a holder that clones on first access instead. The clone is
created at most once per transaction and shared across a tx's messages,
so multi-message AddPackage transactions behave exactly as before;
transactions that never type-check now skip the clone entirely. Pure
node-side work reduction — no consensus- or gas-visible change.
@github-actions github-actions Bot added the 📦 ⛰️ gno.land Issues or PRs gno.land package related label Jul 3, 2026
@Gno2D2 Gno2D2 added the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Jul 3, 2026
@Gno2D2

Gno2D2 commented Jul 3, 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 6, 2026 15:28
@Gno2D2 Gno2D2 removed the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Jul 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

📦 ⛰️ gno.land Issues or PRs gno.land package related

Projects

Development

Successfully merging this pull request may close these issues.

2 participants