golang / golang/go

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
FixPending
Dominant language
Go
Stars
139k
Forks
19.9k
PR merge metrics
PR metrics pending

Description

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

```go
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.

Contributor guide

Open the contributing guide

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.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.