Visitar URL original
perf: use c allocator for shared io by GrapeBaBa · Pull Request #92 · ChainSafe/zapi · GitHub
Skip to content

perf: use c allocator for shared io - #92

Merged
GrapeBaBa merged 1 commit into
mainfrom
grapebaba/io-c-allocator
Nov 13, 2026
Merged

GrapeBaBa merged 1 commit into
mainfrom
grapebaba/io-c-allocator

Conversation

@GrapeBaBa

Copy link
Copy Markdown
Contributor

Motivation

The shared std.Io.Threaded uses its allocator for temporary task and future allocations. Using page_allocator sends those allocations directly through page mappings instead of allowing ordinary heap reuse.

Description

Use std.heap.c_allocator for shared I/O, matching the DSL's existing general-purpose allocator. The zapi module already links libc. This is an allocator-choice optimization; no performance improvement has been benchmarked.

Verified with 45 Zig tests, the example addon build, and 136 JS DSL/addon-isolation tests. Changed-file formatting passes; whole-source formatting reports an existing issue in src/create_callback.zig, also reproduced at the base revision.

AI assistance: Codex prepared this change and ran the checks.

@matthewkeil matthewkeil moved this from In Progress to Review Requested in Lodestar Team Coordination Nov 12, 2026
@GrapeBaBa
GrapeBaBa merged commit a736d8a into main Nov 13, 2026
5 checks passed
@github-project-automation github-project-automation Bot moved this from Review Requested to Done in Lodestar Team Coordination Nov 13, 2026
GrapeBaBa pushed a commit that referenced this pull request Nov 13, 2026
🤖 I have created a release *beep* *boop*
---


##
[4.4.0](zapi-v4.3.0...zapi-v4.4.0)
(2026-11-13)


### Features

* **js:** add exact BigInt conversions, toI128LowBits and fromWords
([#91](#91))
([b56bf2d](b56bf2d))


### Performance Improvements

* use c allocator for shared io
([#92](#92))
([a736d8a](a736d8a))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
GrapeBaBa added a commit to ChainSafe/lodestar that referenced this pull request Nov 13, 2026
…10612)

**Motivation**

`std.heap.page_allocator` maps at least one page per allocation and
unmaps it on free, with no reuse. It fits large, long-lived buffers, but
a few places in the Zig code use it for small per-call allocations.

**Description**

- `bindings/napi/pubkeys.zig`: keep `page_allocator` for the long-lived
pubkey cache storage, including `pkix.load`, since `pkix.install` swaps
the loaded storage and its allocator into the live cache. Per-call
allocations (path strings, aggregate indices, `syncPubkeys` pointer
arrays) now use `c_allocator`.
- `PubkeyCache.syncPubkeys` takes a scratch allocator for its `prepared`
and `batch_errors` arrays instead of borrowing the cache allocator.
- `era.getShortEraRoot` takes an allocator from the caller instead of
hardcoding `page_allocator`.

The same issue in zapi's shared `std.Io.Threaded` (which allocates every
`async`/`concurrent`/group task closure through its gpa) is fixed in
ChainSafe/zapi#92. The zapi bump to 4.4.0 will land in a separate PR.

**Testing**

- `zig build test:state_transition test:era test:tidy`
- `pnpm --filter @lodestar/native prepare-mainnet`, then binding tests
`pubkeys`, `rewards`, `proposerRewards`, `attestationRewards`

> This PR was written primarily by Claude Code.

---------

Co-authored-by: Cayman <caymannava@gmail.com>
GrapeBaBa added a commit to ChainSafe/lodestar that referenced this pull request Nov 13, 2026
**Motivation**

zapi 4.4.0 includes ChainSafe/zapi#92: the shared `std.Io.Threaded`
behind `js.io()` now allocates its `async`/`concurrent`/group task
closures with `c_allocator` instead of `page_allocator`, so each task no
longer costs an mmap/munmap pair. Follow-up to #10612.

**Description**

- Pin zapi to the `zapi-v4.4.0` tag commit `0cc6b2c`.
- zapi 4.2.0 renamed `Number.toU64Exact` to `toSafeInteger`
(ChainSafe/zapi#81) with unchanged semantics. Update the binding call
sites.

Other changes since the previous pin (4.1.0) are additive
(`js.scratch()`, exact BigInt conversions). 4.2.0 also requires
`setEnv`/`restoreEnv` to be paired; lodestar does not call them
directly.

**Testing**

- `zig build test:tidy`, bindings build
- `pnpm --filter @lodestar/native prepare-mainnet` and the full binding
test suite: 26 files passed, 1 skipped (506 tests passed, 11 skipped)

> This PR was written primarily by Claude Code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants