x/text/internal/gen: CodeWriter repeatedly encodes struct slices when hashing
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 82/100
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
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from golang/go
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Documentation NeedsFix
Difficulty 1/5 Under an hour Newbie friendliness 88/100
-
FixPending
Difficulty 2/5 Half a day Newbie friendliness 84/100
-
FixPending GoCommand
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
NeedsInvestigation
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 84/100
-
enhancement needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
kind/cleanup
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
kubernetes-sigs/kueue#15947 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
sympozium-ai/sympozium#627 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100