x/text/internal/gen: CodeWriter repeatedly encodes struct slices when hashing

Open Beginner friendly
#80,905 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
go
Domain
tooling

Research direction

Start at CodeWriter.writeSlice and inspect the struct-slice branch alongside the integer-slice branch. Add a regression test that compares CodeWriter's exact gob input with a stream containing the slice length followed by each struct element. Done means the current element is encoded once per iteration and the regression test passes.

Written by the indexing model from the issue text.

Description

FixPending

CodeWriter.writeSlice handles slices of structs by encoding v inside the per-element loop:

    for i := 0; i < v.Len(); i++ {
        x := v.Index(i).Interface()
        w.gob.EncodeValue(v)
        ...
    }

Here v is the entire slice, rather than the current element.

For a slice containing N structs, this encodes the N-element slice N times. The amount of data processed by gob is therefore O(N²). For example, a slice containing 10,000 structs causes roughly 100,000,000 struct values to be processed.

This is also inconsistent with the integer slice branch, which encodes each element individually. writeSlice already encodes the slice length
before entering the element-specific branch, so the intended checksum input appears to be the length followed by each element.

The generated Go values are not affected, but generation can be significantly slower and the checksum is computed over repeated whole-slice encodings instead of element-wise encodings.

The loop should encode x:

w.gob.Encode(x)

Equivalently, it could use:

w.gob.EncodeValue(v.Index(i))

A regression test can compare the exact gob input produced by CodeWriter with a stream containing the slice length followed by each struct element.

After this change, checksum comments in files produced by generators that write struct slices may change when those generators are rerun.

Dominant language
Go
Stars
139k
Forks
20k
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from golang/go

All issues in golang/go

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.