Skip to content

fix/issues-79-80: Read and write artifact text without extra copies, and reuse the discovery copy buffer - #86

Merged
rahlk merged 3 commits into
mainfrom
fix/issues-79-80-discovery-performance
Nov 30, 2026
Merged

rahlk merged 3 commits into
mainfrom
fix/issues-79-80-discovery-performance

Conversation

@rahlk

@rahlk rahlk commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

tl;dr A large text file no longer costs several times its size in memory, and artifact discovery no longer makes a new copy buffer for each file.

Motivation and Context

  • --artifact-text uses approximately 5 times the file size in memory #80: with --artifact-text on (the default), one 512 MB text file made a JSON run peak at 2.72 GB and a Neo4j snapshot run at 3.57 GB. The text was copied at every stage:
    • Reading: the text went through a bytes.Buffer that doubles as it grows, and String() then copied it again: about 3 times the file's size. It is now read into a strings.Builder sized from the file's size, so it takes one allocation. The sha256 check that refuses a file that changed during the read stays.
    • JSON: the emitter wrote the rest of application, every artifact's text included, with one json.Marshal. It now writes the artifacts one at a time and escapes each source straight to the output. The output is byte-identical to json.Marshal.
    • Neo4j snapshot: each value was formatted into a string before it was written. Values are now written straight to graph.cypher.
    • Bolt push: node batches were limited only to 5,000 rows, so a batch of large files could exceed the server's transaction memory. A node batch is now also sent when its text reaches 16 MiB (boltBatchBytes). A larger row goes alone.
  • Perf: every run re-hashes the whole repo (with -t and on cache hit as well) #79: discovery is about 1% of a run (14 ms of a 1.4 s run on nats-server, about 170 ms on gitea), so there is no cache: a cache would risk a stale sha256. Most of what it allocated was a new 32 KB buffer from io.Copy for each file; discovery now reuses one from a pool.

Measurements (macOS; allocation is the total, garbage included; peak is the whole run's):

Case Before After
gitea v1.25.3 discovery (2,843 files, 14.1 MB), no text 104 MB allocated 11 MB
gitea v1.25.3 discovery, with text 225 MB allocated 26 MB
One 512 MB text file, JSON run 2.72 GB peak 0.56 GB
One 512 MB text file, Neo4j snapshot run 3.57 GB peak 0.56 GB

The remaining peak is about the text itself, which the analysis holds once.

How Has This Been Tested?

  • Unit tests:
    • a 64 MB text file is read with about one allocation of its size (the old code allocated 192 MB);
    • discovery of 500 small files allocates under 4,000 KB (the old code allocated 16,994 KB);
    • a file that changes during the text read is still refused;
    • the JSON string writer gives the same bytes as json.Marshal on fixed cases (HTML characters, control characters, U+2028/U+2029, invalid UTF-8) and on 2,000 random byte strings;
    • an envelope with artifacts is byte-identical to the old json.Marshal output, and a 64 MB source is written with under 8 MB allocated;
    • the streaming Cypher writers give the same text as the string ones, and a 64 MB source is written to the snapshot with under 16 MB allocated (the old code allocated 553 MB);
    • Bolt node batches are split by size.
  • The eight live Neo4j integration tests (-tags neo4j_it) pass with the new batching.
  • The allocation tests fail on the old code. go test ./... and golangci-lint pass.

Breaking Changes

None. The JSON and Cypher output is unchanged byte for byte. Only the Bolt push may now send more, smaller node transactions.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the Codellm-Devkit Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Rahul Krishna and others added 3 commits November 30, 2026 09:08
…y buffer (#79, #80)

- #80: the text read went through a bytes.Buffer that doubles as it grows,
  then String() copied it again: about 3 times the file size. readText now
  reads into a strings.Builder sized from the file, one allocation. A 64 MB
  file allocated 192 MB, now about its size. The hash check that refuses a
  file changed between the two reads stays.
- #79: io.Copy made a new 32 KB buffer for every file. readArtifact now
  takes one from a pool. On gitea v1.25.3 (2,843 non-Go files, 14 MB)
  discovery allocated 104 MB without text and 225 MB with text; now 11 MB and
  26 MB. Time is unchanged (about 170 ms, about 1% of a run).

Tests: the text read allocates about its size, discovery of 500 small files
does not allocate a buffer per file, and a file changed during the read is
refused. The first two fail on the old code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The JSON writer streamed the symbol table one module at a time, but wrote the
rest of application, with every artifact's text, in one json.Marshal: it
built the escaped text in a growing buffer and then copied it. For one
512 MB text file that was about 1.1 GB on top of the text itself.

The envelope now holds a stand-in for the artifacts. They are written one at
a time in its place, and each source is escaped straight to the writer by
writeJSONString, which writes the bytes encoding/json writes (HTML escaping,
\b \f \n \r \t, \u00XX, U+2028/U+2029, U+FFFD for invalid UTF-8). The output
is unchanged.

Tests: writeJSONString equals json.Marshal on fixed cases and 2,000 random
byte strings; Encode with artifacts equals json.Marshal; Encode of a 64 MB
source allocates under 8 MB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…atches by size (#80)

Since #83 the :Artifact node carries source, so the Neo4j writers handle
whole files.

- Snapshot: each node row was rendered as strings first (cypherString, then
  cypherMap's concatenation and strings.Join, then fmt.Fprintf): a 64 MB
  source allocated 553 MB. writeCypherValue, writeCypherString and
  writeCypherMap now write straight to the file, and cypherValue /
  cypherString / cypherMap wrap them, so the output is unchanged.
- Bolt: a node batch was limited only by count (5,000 rows), so a batch of
  large files could exceed the server's transaction memory. A node group is
  now also flushed when its rows' text reaches boltBatchBytes (16 MiB); a
  larger row goes alone.

With one 512 MB text file, both a JSON run and a Neo4j snapshot run now peak
at 0.56 GB (before this branch: 2.72 GB and 3.57 GB).

Tests: the value writers match the string functions; the snapshot of a 64 MB
source allocates under 16 MB (553 MB on the old code); Bolt node batches stay
under the byte limit and a larger row goes alone. All live Neo4j integration
tests pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rahlk
rahlk merged commit ac57714 into main Nov 30, 2026
2 of 3 checks passed
@rahlk
rahlk deleted the fix/issues-79-80-discovery-performance branch November 30, 2026 15:49
rahlk added a commit that referenced this pull request Nov 30, 2026
…-performance

fix/issues-79-80: Read and write artifact text without extra copies, and reuse the discovery copy buffer
rahlk added a commit that referenced this pull request Dec 1, 2026
…/json does

Go 1.26 escapes an invalid UTF-8 byte as \ufffd; Go 1.27 writes the character
U+FFFD. writeJSONString always wrote the character, so on CI's Go 1.26 it
differed from json.Marshal and TestWriteJSONString_EqualsMarshal failed on
every run since #86. It now asks encoding/json once how it writes one.
@rahlk rahlk added the bug Something isn't working label Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--artifact-text uses approximately 5 times the file size in memory Perf: every run re-hashes the whole repo (with -t and on cache hit as well)

1 participant