Skip to content

refactor(zero-c): consolidate duplicated dynamic-array grow helpers (#390)#406

Open
Osamaali313 wants to merge 1 commit into
vercel-labs:mainfrom
Osamaali313:fix/consolidate-grow-helpers
Open

refactor(zero-c): consolidate duplicated dynamic-array grow helpers (#390)#406
Osamaali313 wants to merge 1 commit into
vercel-labs:mainfrom
Osamaali313:fix/consolidate-grow-helpers

Conversation

@Osamaali313

Copy link
Copy Markdown

Summary

Fixes #390.

The same dynamic-array grow helper was duplicated across four files in
native/zero-c/src:

  • ir_grow_items (ir.c), lower_grow_items (program_graph_lower.c), and
    checker_grow_items (checker.c) shared an identical body.
  • ir_grow_tracked_items (the variant that also accounts ir->mir_bytes) was
    defined verbatim in both ir.c and program_graph_mir.c.

As the issue notes, the duplicated mir_bytes accounting can silently diverge
if one copy is edited without the others.

Change

Hoist both helpers into include/zero.h as shared static inline functions
(z_grow_items and ir_grow_tracked_items), placed right after the
IrProgram definition so the tracked variant can reference ir->mir_bytes.
The five local definitions are removed and their call sites repointed at the
shared helpers.

Pure refactor — the helper bodies are byte-for-byte the originals, so there is
no behavior change. Net +61 / -81 lines across 5 files.

Validation

  • make -C native/zero-c builds clean under -Wall -Wextra -Wpedantic.
  • Host-target end-to-end works: zero build --emit exe --target linux-x64
    on conformance/run/pass/hello.graph produces an exe that runs and prints
    the expected output.
  • All conformance/check/pass/*.graph fixtures pass zero check (this is the
    path that exercises the modified checker.c and ir.c lowering).

Transparency per AGENTS.md: I developed this on Linux (Ubuntu via WSL) where
the linux-x64 host target is fully runnable, but pnpm run agent:checks
could not complete because the linux-musl-x64 build steps require a
musl-capable C compiler that isn't installed in my environment. That step
fails identically on a clean checkout (BLD003: cross target 'linux-musl-x64' requires a target-capable C compiler ... install the bundled-toolchain default), so it is pre-existing and unrelated to this
change. CI (with the musl toolchain) should exercise the full matrix.

…ercel-labs#390)

The same dynamic-array grow helper was duplicated across four files in
native/zero-c/src: ir_grow_items (ir.c), lower_grow_items
(program_graph_lower.c) and checker_grow_items (checker.c) shared an
identical body, and ir_grow_tracked_items was defined verbatim in both
ir.c and program_graph_mir.c. As noted in vercel-labs#390, the duplicated mir_bytes
accounting can silently diverge if one copy is edited without the others.

Hoist both into include/zero.h as shared `static inline` helpers
(z_grow_items and ir_grow_tracked_items), placed after the IrProgram
definition so the tracked variant can reference ir->mir_bytes, and drop
the five local definitions, repointing call sites at the shared helpers.
Pure refactor; no behavior change.

Validation: native/zero-c builds clean under -Wall -Wextra -Wpedantic;
host-target (linux-x64) build+run works; all conformance check graph
fixtures pass `zero check`. The linux-musl-x64 build steps in
agent:checks fail identically on a clean checkout in this environment
(BLD003: no musl-capable C compiler installed), unrelated to this change.
Copilot AI review requested due to automatic review settings June 11, 2026 20:44
@vercel

vercel Bot commented Jun 11, 2026

Copy link
Copy Markdown

@Osamaali313 is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Refactors dynamic array growth helpers by centralizing them into shared inline functions, reducing duplicated logic across several C source files.

Changes:

  • Removed per-file *_grow_items helpers in ir.c, program_graph_lower.c, checker.c, and program_graph_mir.c.
  • Introduced shared z_grow_items (and ir_grow_tracked_items) helpers in include/zero.h.
  • Updated call sites to use z_grow_items.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
native/zero-c/src/program_graph_mir.c Removes local tracked grow helper in favor of shared header implementation.
native/zero-c/src/program_graph_lower.c Replaces local grow helper usage with z_grow_items.
native/zero-c/src/ir.c Removes local grow helpers and switches call sites to z_grow_items.
native/zero-c/src/checker.c Removes local grow helper and switches call sites to z_grow_items.
native/zero-c/include/zero.h Adds shared inline grow helpers (z_grow_items, ir_grow_tracked_items).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1077 to +1079
/* As z_grow_items, but also accounts the new allocation against an IrProgram's
* mir_bytes total. Replaces the verbatim copies in ir.c and program_graph_mir.c. */
static inline void *ir_grow_tracked_items(IrProgram *ir, void *items, size_t len, size_t *cap, size_t initial, size_t item_size) {
Comment on lines +1069 to +1075
static inline void *z_grow_items(void *items, size_t len, size_t *cap, size_t initial, size_t item_size) {
if (len + 1 > *cap) {
*cap = z_grow_capacity(*cap, len + 1, initial);
return z_checked_reallocarray(items, *cap, item_size);
}
return items;
}
Comment on lines +1081 to +1084
size_t next_cap = z_grow_capacity(*cap, len + 1, initial);
void *next_items = z_checked_reallocarray(items, next_cap, item_size);
if (ir) ir->mir_bytes += next_cap * item_size;
*cap = next_cap;
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.

Consolidate duplicated dynamic-array grow helpers in zero-c

2 participants