Repository navigation
fix/issues-79-80: Read and write artifact text without extra copies, and reuse the discovery copy buffer - #86
Merged
Conversation
…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
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.
This was referenced Dec 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-textuses approximately 5 times the file size in memory #80: with--artifact-texton (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:bytes.Bufferthat doubles as it grows, andString()then copied it again: about 3 times the file's size. It is now read into astrings.Buildersized from the file's size, so it takes one allocation. The sha256 check that refuses a file that changed during the read stays.application, every artifact's text included, with onejson.Marshal. It now writes the artifacts one at a time and escapes eachsourcestraight to the output. The output is byte-identical tojson.Marshal.graph.cypher.boltBatchBytes). A larger row goes alone.-tand 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 stalesha256. Most of what it allocated was a new 32 KB buffer fromio.Copyfor each file; discovery now reuses one from a pool.Measurements (macOS; allocation is the total, garbage included; peak is the whole run's):
The remaining peak is about the text itself, which the analysis holds once.
How Has This Been Tested?
json.Marshalon fixed cases (HTML characters, control characters, U+2028/U+2029, invalid UTF-8) and on 2,000 random byte strings;json.Marshaloutput, and a 64 MB source is written with under 8 MB allocated;-tags neo4j_it) pass with the new batching.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
Checklist
Additional context
--no-artifact-textdrops the text.docs/design/specs/2026-11-24-neo4j-streaming-push.md(the Bolt writer,boltBatchBytes).-tand on cache hit as well) #79. Fixes--artifact-textuses approximately 5 times the file size in memory #80.