Skip to content

--artifact-text uses approximately 5 times the file size in memory #80

Backlog

Description

@rahlk

Describe the bug

PR #55 hashes every artifact in constant memory (fixed in d2ae0e4). With --artifact-text, the text read is not constant:

  1. The second read copies the file into a bytes.Buffer. The buffer grows by doubling, so its capacity can be up to 2 times the file size, and the old and new buffers are both live while it grows.
  2. text.String() copies the bytes again into Source.
  3. The JSON writer then makes one more copy of the text for analysis.json.

To Reproduce

  1. Make a project with one main.go and one 512 MB data.csv.
  2. Run cango -i . -o out, then cango -i . -o out --artifact-text.
  3. Compare the peak memory (/usr/bin/time -l on macOS).

Expected behavior

The peak memory is approximately the size of the captured text plus the size of the output, not a multiple of it.

Logs

Build of PR #55 head (d2ae0e4), macOS, one 512 MB text file:

Run Peak RSS
no flag 27.8 MB
--artifact-text 2.72 GB (approximately 5.3 × the file)

analysis.json was 521 MB. This measures the full run. It does not split the cost between the read and the JSON write.

How it works (PR #55 head)

Each non-.go file goes through four stages. The example is one 512 MB data.csv.

Stage 1: hash and classify (always). readArtifact streams the file once, in small chunks, through a writer that computes the sha256 and the size, and checks if the bytes are binary. It never holds the whole file. Memory: approximately 0. This is why the run with no flag peaks at 27.8 MB.

Stage 2: capture the text (only with --artifact-text, only for text files). It goes back to the start of the file and reads it a second time:

var text bytes.Buffer
io.Copy(io.MultiWriter(h, &text), f) // hash again and collect the bytes
// if the second hash is not the first hash, the file changed: skip it
art.Source = text.String()           // copy the bytes into a Go string
  • bytes.Buffer does not know the file size. It starts small and doubles: 32 KB, 64 KB, … 256 MB, 512 MB. Each time it grows, it allocates a new block and copies the old block into it. For a short time, both blocks are in memory (for example, 256 MB + 512 MB). At the end, its capacity is between 512 MB and 1 GB.
  • text.String() makes a second 512 MB copy. This copy stays in art.Source until the program stops.
  • The buffer is now garbage, but the Go garbage collector frees it later, not immediately.

Stage 3: emit (internal/syntactic_analysis/v2emit/repository.go). The artifact goes into the output tree. Only the string reference is copied, not the bytes. Memory: approximately 0.

Stage 4: write analysis.json. The emitter streams the symbol table one module at a time. The other parts of application, which include the artifacts, go through one json.Marshal call (emit.go:137):

  • json.Marshal writes the escaped text into its own buffer, which doubles like the Stage 2 buffer (0.5–1 GB).
  • It then returns a copy of that buffer (521 MB).

Estimated memory at the peak (only the total was measured):

Held in memory Size
art.Source (kept for the full run) 512 MB
Stage 2 buffer, not yet collected 0.5–1 GB
Stage 4 json.Marshal buffer and its copy 1–1.5 GB
Total approximately 2–3 GB (measured: 2.72 GB)

Examples

Captured text Stage 2 (now) Stage 2 (after fix 1) Effect
A 4 KB config.yaml approximately 12 KB 4 KB No effect on a normal project
1,000 files of 50 KB (50 MB) approximately 150 MB 50 MB Small
One 512 MB data.csv approximately 1.5 GB 512 MB Large. Stage 4 adds 1–1.5 GB more

Additional context

  • Location: internal/repository/repository.go:196 (readArtifact, the second read).
  • Keep the size uncapped by default. Users who pass --artifact-text ask for the text.
  • Fix 1 (Stage 2, small change): read into a strings.Builder with Grow(int(info.Size())). The read then makes one allocation at the exact size, with no doubling and no String() copy. This removes approximately 0.5–1 GB in the example.
  • Fix 2 (Stage 4, larger change): stream the artifacts into analysis.json one at a time, as the emitter already does for the symbol table, so json.Marshal never holds all the text at once. This can be a separate issue.
  • Optional: a flag (for example --artifact-text-max-bytes) for users who want a limit. A file above the limit gets extraction: "none" and a warning. Check codeanalyzer-python for parity first.
  • After fix 1, measure again to confirm the Stage 4 share before you start fix 2.

Activity

  1. added
    bug
    Performance
    on Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Performancebug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions