Skip to content

perf: first-slice string concat and buffered text I/O (#747) - #1337

Merged
godofecht merged 9 commits into
mainfrom
cursor/string-io-first-slice-2845
Oct 4, 2026
Merged

godofecht merged 9 commits into
mainfrom
cursor/string-io-first-slice-2845

Conversation

@godofecht

@godofecht godofecht commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Part of #747.

Intent

Audit strings, parse/format and file I/O for short-job Python comparisons, then ship a first slice that cuts per-join heap traffic and gives explicit, buffered stdlib paths.

This ports the archive/perf/747-string-io design (one-pass concat of pure-string + chains) to flowc and adds the linear-scan helpers the acceptance criteria need.

Overlap check

  • I searched open pull requests and recent commits for overlapping work.
  • This does not duplicate another active implementation.

No open PR implements #747. Nearby PRs (#1265 native baselines, #1267 parallel scaling, #1268 cold-start vs Python) touch the harness, not string concat or stdlib I/O.

What changed

  • Compiler: a + b + c + d of already-string operands lowers to one __flowc_str_concatn call (one length pass, one allocation, each part copied once). Two-part joins and mixed numeric chains keep pairwise __flowc_str_concat.
  • stdlib: std.string parses/formats i64 on the caller buffer with explicit overflow, empty, sign-only and trailing-junk errors (Python int() on ASCII). Line walking does not allocate. std.io reads and writes with fread/fwrite of a known size; a missing file is ok = false.
  • Harness: new runtime_string_concat, runtime_parse_format, runtime_buffered_io rows plus CPython twins. Existing cold_file_transform now uses the buffered path instead of per-character fgetc/fputc.

Evidence

Baseline before this change: nested __flowc_str_concat for every +; lib/stdlib/string.flow was strlen/strcmp/concat only; io.flow had no file helpers; cold/file_transform used fgetc/fputc.

Targeted proofs:

  • Host selftest: flowc: PASS (concatn present in the preamble)
  • ./compiler/scripts/bootstrap_from_c.sh --verify — PASS (fixed point at gen3)
  • ./flow tool tests/cgen/run.flow — 114 passed, 0 failed (includes string_concat_chain)
  • ./flow test-lang on test_string_concat_chain, test_stdlib_string_io, test_strings, test_string_concat_numbers, test_const_string_concat, test_cast_in_string_concat — 6/6
  • ./flow tool tests/bench_harness/run.flow — pass=43 fail=0
  • ./flow tool gen_stdlib_docs --check and stdlib_api_drift — pass
  • ./flow tool python_ratchet — PASS (new twins are under the exempt benchmarks/baselines/python/ tree)

./flow tool bench_harness --smoke on this Linux host (3 reps, medians in seconds):

workload Flow Python note
runtime_string_concat 0.0057 0.056 ~10×, eight-part + chain
runtime_parse_format 0.0041 0.121 ~30×, no per-token heap
runtime_buffered_io 0.0011 0.011 ~10×, one fread / in-place scan / one fwrite
cold_file_transform 0.199 0.009 cold flow run (compile+run) vs python3; not the I/O kernel

The four-part golden fails on nested pairwise joins. Parse tests fail if overflow wraps or junk is accepted.

Remaining gaps (not this PR)

  • Mixed "x=" + n + " y=" + m still uses nested pairwise joins.
  • MLIR still lowers string + to __flow_str_concat only.
  • No parse_float yet; float format still goes through __flowc_str_of_f64 / snprintf.
  • Concat results are still process-lifetime (malloc, never freed).
  • UTF-8 validation is not a separate pass; scans are byte/ASCII, matching Python int() on ASCII.
  • No allocation/copy counters wired into the new harness rows yet (memory/allocation_copy is still a stub).
  • Encoding-error policy for non-UTF-8 file reads is still "bytes as stored".
  • Cold-start one-shot file transform still loses to CPython because flow run pays compile; that belongs with [Performance #727] Minimize compiled-program cold start and runtime initialization overhead #746, not this I/O kernel.

Risk

  • C backend only for concatn. Bootstrap C regenerated in a follow-up commit on this branch.
  • New stdlib functions are additive. io_read_file refuses files larger than 2^31-1 rather than truncating.
  • No unresolved language/API design decision is being implemented implicitly.
  • Docs changed for the new stdlib surface.
  • No user-visible compatibility break; CHANGELOG updated.

Agent handoff

Bootstrap C is committed separately (fix: regenerate bootstrap C after n-way string concat). Do not merge.

Open in Web Open in Cursor 

cursoragent and others added 3 commits October 4, 2026 18:39
A chain like a + b + c + d allocated n-1 buffers and recopied every
prefix. Pure-string chains of three or more parts now lower to one
__flowc_str_concatn call. std.string parses and formats integers on the
caller buffer with explicit overflow and junk errors; std.io reads and
writes with fread/fwrite. Cross-harness rows cover concat, parse/format
and buffered file transform (Part of #747).

Co-authored-by: Abhishek Shivakumar <[email protected]>
Make need/wrote mutable, expect concatn for the four-part chain and
nested pairwise only for mixed numeric joins, and update the Stage-A
roundtrip strcat fixture. Regenerate stdlib-api.md.

Co-authored-by: Abhishek Shivakumar <[email protected]>
The checked-in bootstrap C now emits __flowc_str_concatn for
pure-string + chains, matching compiler/src/cgen.flow (#747).

Co-authored-by: Abhishek Shivakumar <[email protected]>
@godofecht
godofecht marked this pull request as ready for review October 4, 2026 18:51
cursoragent and others added 4 commits October 4, 2026 18:57
import io.flow now sees libc free (void) from the #747 buffered I/O
helpers, so assigning free(0) to i32 fails typecheck. Call it as void.

Co-authored-by: Abhishek Shivakumar <[email protected]>
The stdlib catalog already indexes export functions from lib/stdlib, so
live completion id 9 includes the nine new str_* APIs. The transcript
golden was still at 203 items.

Co-authored-by: Abhishek Shivakumar <[email protected]>
cgen.flow auto-merged: n-way string concat and record-update copy
elision both remain. Bootstrap C regenerated to the combined fixed
point after #1338.

Co-authored-by: Abhishek Shivakumar <[email protected]>
Generated C grew by the __flowc_str_concatn helper, so compile_hello,
compile_isolated_root and python_source file checksums needed a refresh
on linux and darwin. MLIR corpus digests follow the changed io.flow and
string.flow modules; file_transform is no longer accepted (std.io import).

Co-authored-by: Abhishek Shivakumar <[email protected]>
cursoragent and others added 2 commits October 4, 2026 20:18
cgen.flow auto-merged: n-way string concat and #739 SIMD loop hints
both remain. Bootstrap C regenerated to the combined fixed point.
MLIR corpus already includes the for_vectorize_pragma_skip digest.

Co-authored-by: Abhishek Shivakumar <[email protected]>
cgen and roundtrip auto-merged: n-way concat and #775 type identity
both remain. Bootstrap C regenerated to the combined fixed point.
MLIR corpus already includes test_type_identity.

Co-authored-by: Abhishek Shivakumar <[email protected]>
@godofecht
godofecht merged commit 7b2dccb into main Oct 4, 2026
40 checks passed
cursor Bot pushed a commit that referenced this pull request Oct 4, 2026
Rebased onto #1337 string/IO (7b2dccb). Bootstrap C is the fixed
point of compiler/src including concatn lowering and the
cross-module RT/lifetime summary slice.

Co-authored-by: Abhishek Shivakumar <[email protected]>
cursor Bot pushed a commit that referenced this pull request Oct 4, 2026
Fixed-point C after rebasing onto string/IO (#1337 / 7b2dccb).
bootstrap_from_c.sh --regen (FIXED POINT at gen3) then --verify PASS.

Co-authored-by: Abhishek Shivakumar <[email protected]>
cursor Bot pushed a commit that referenced this pull request Oct 4, 2026
cursor Bot pushed a commit that referenced this pull request Oct 4, 2026
Rebased onto 7b2dccb (#1337). Source already includes FLOWC_PROFILE.
Regen reached a fixed point at gen2; --verify matches compiler/src.

Co-authored-by: Abhishek Shivakumar <[email protected]>
godofecht added a commit that referenced this pull request Oct 4, 2026
* feat: emit and consult cross-module RT/lifetime summaries (#765)

Add a compact $fname effect/lifetime summary for public functions
(callback/frame safety, alloc/free, block/lock, I/O, declared domain,
one provenance edge) and consult it during type checking so imported
calls compose. Summaries ride in the module effect table and are
erased before codegen.

Co-authored-by: Abhishek Shivakumar <[email protected]>

* fix: use let mut and typed null in RT summary helpers

The edited compiler rejected immutable reassignment and a bare null
string in the new summary worklist and selftest.

Co-authored-by: Abhishek Shivakumar <[email protected]>

* fix: wire rt_summary into Stage-A roundtrip and MLIR corpus

typecheck and sem_check import the new module, so per-module compile
and the frontend link need its object. The overload-integration lang
test imports typecheck.flow, so its recorded MLIR digest picks up
the new module.

Co-authored-by: Abhishek Shivakumar <[email protected]>

* fix: regenerate bootstrap C after RT/lifetime summaries

Rebased onto #1337 string/IO (7b2dccb). Bootstrap C is the fixed
point of compiler/src including concatn lowering and the
cross-module RT/lifetime summary slice.

Co-authored-by: Abhishek Shivakumar <[email protected]>

---------

Co-authored-by: Cursor Agent <[email protected]>
cursor Bot pushed a commit that referenced this pull request Oct 4, 2026
godofecht added a commit that referenced this pull request Oct 4, 2026
* feat: opt-in runtime memory profiler for #740

Add a FLOW_MEM_PROFILE runtime in flowc-generated C that counts heap
allocations, peak live heap, peak RSS, compiler-temp bytes and copy
volume without rewriting workload sources. The #728 harness records
those fields from FLOW_MEM_PROFILE_OUT, and the memory/allocation
cross-harness rows now do real allocate/copy/RSS work.

Co-authored-by: Abhishek Shivakumar <[email protected]>

* test: tighten mem_profile golden around wrapped malloc

flow_mem_malloc(32) contains the substring malloc(32), so the
negative check now looks for the unwrapped call form.

Co-authored-by: Abhishek Shivakumar <[email protected]>

* test: retarget goldens after FLOW_MEM_PROFILE prelude

The profiler prelude wraps malloc/realloc, mentions temp lifetime
bytes, and uses a constructor attribute. Tighten the attribute and
lifetime goldens to the annotation-splice payload, expect the
counting wrappers in grow helpers, and refresh hello.c checksums.

Co-authored-by: Abhishek Shivakumar <[email protected]>

* test: refresh CLI hello.c checksums after #1337 concatn

Co-authored-by: Abhishek Shivakumar <[email protected]>

* test: update MLIR corpus digests for filled #740 workloads

Co-authored-by: Abhishek Shivakumar <[email protected]>

* fix: regenerate bootstrap C after rebase onto main

Co-authored-by: Abhishek Shivakumar <[email protected]>

---------

Co-authored-by: Cursor Agent <[email protected]>
@godofecht
godofecht deleted the cursor/string-io-first-slice-2845 branch October 12, 2026 23:02
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.

2 participants