Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
27fad93
Add recursion limits to parsers and expand test coverage
CDRayn Dec 3, 2026
0393812
Fix typo
CDRayn Dec 3, 2026
c18d71f
Add minor bugfixes and increase test coverage
CDRayn Dec 3, 2026
7181537
Fix parsing / handling of archive lines with '@' characters
CDRayn Dec 4, 2026
b4b04a1
Combine unit tests into one test with a test table
CDRayn Dec 4, 2026
0eb7c4f
Fix handling of options on a line that has an include
CDRayn Dec 4, 2026
c34e9c2
Update escapes handling
CDRayn Dec 4, 2026
b9495fb
Match pip's behavior for U+001F handling
CDRayn Dec 4, 2026
2a8ec95
Fix handling of large GPG signatures
CDRayn Dec 4, 2026
e1459f5
Update comment for accuracy & add test coverage
CDRayn Dec 4, 2026
572e7dc
Add test cases
CDRayn Dec 4, 2026
f3eda98
Update Flatten() so it can not produce large ouputs
CDRayn Dec 4, 2026
8956e27
Improve performance of parsing Option lines
CDRayn Dec 4, 2026
39d1eb1
Minor bugfixes and improvements
CDRayn Dec 4, 2026
7932ede
Add optimizations and benchmark tests
CDRayn Dec 4, 2026
ec882c7
Performance improvements
CDRayn Dec 4, 2026
8856897
Minor tweaks & fixes
CDRayn Dec 4, 2026
5882376
Address PR feedback
CDRayn Dec 4, 2026
ccbb538
Address PR feedback
CDRayn Dec 4, 2026
8c2813a
Remove named return values from function signature.
CDRayn Dec 4, 2026
46c366f
Address PR feedback
CDRayn Dec 4, 2026
4aa813c
Address PR feedback
CDRayn Dec 4, 2026
81a00c5
Performance optimization
CDRayn Dec 4, 2026
7ed85e8
Address PR feedback
CDRayn Dec 4, 2026
0bc38f2
Address PR feedback
CDRayn Dec 4, 2026
6564384
Address PR feedback
CDRayn Dec 4, 2026
8fd1412
Address PR feedback
CDRayn Dec 4, 2026
66dbd19
Address PR feedback
CDRayn Dec 4, 2026
e69b2aa
Address PR feedback
CDRayn Dec 4, 2026
43dcbf4
Address PR feedback
CDRayn Dec 4, 2026
527695b
Address PR feedback
CDRayn Dec 5, 2026
68aded7
Address PR feedback
CDRayn Dec 5, 2026
21a3bea
Move item to the "Breaking" section
CDRayn Dec 5, 2026
48a7ee9
Address PR feedback
CDRayn Dec 5, 2026
9d54be2
Fix flakey test
CDRayn Dec 5, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
402 changes: 402 additions & 0 deletions CHANGELOG.md

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,43 @@ Keep the random seed. `rapid.Check` takes no seed option by design; a fixed seed
degenerates into a slow, unreadable table test. Name every `Draw` so failures are
reproducible from the printed output.

## Performance

Each package with a hot path (`version`, `requirement`, `marker`, `reqtxt`,
`wheelname`, `tags`, `distribution`) has a `bench_test.go` with benchmarks and
`TestAllocBudget_*` tests over the same inputs.

**Allocation budgets run in CI.** An allocation count does not depend on the machine,
so `internal/allocbudget.Check` fails when a call allocates more *or fewer* times than
its budget, within 0.1% (so a budget under 1,000 is exact). Each slowdown found while
reviewing PR #71 also added allocations, and these tests catch them.

- **Over budget:** find the new allocation. If the change needs it, raise the budget to
the count the failure prints, and say why in the PR.
- **Under budget:** lower the budget to the printed count, so the next regression is
caught.
- The tests skip under `-race`, which adds allocations; CI's plain `go test ./...` runs
them. Counts are the same on arm64 and amd64.
- Code that uses a `sync.Pool` (such as `archive/zip`) or builds maps can move by an
allocation or two between runs. Use `allocbudget.CheckWithin` with a small tolerance,
and say why at the call.
- A new Go release can change the counts. Update the budgets in that PR.
- Each budget test checks first that its input parses, so a budget does not measure an
error path by accident. A row that measures one on purpose says so.

**Benchmarks do not run in CI**, because timing on shared runners is too noisy to gate
on. Before you merge a change to a hot path, compare it with `main`:

```bash
git worktree add /tmp/gpp-main main
(cd /tmp/gpp-main && go test -run '^$' -bench . -count 10 ./reqtxt) > /tmp/old.txt
go test -run '^$' -bench . -count 10 ./reqtxt > /tmp/new.txt
go run golang.org/x/perf/cmd/benchstat@latest /tmp/old.txt /tmp/new.txt
git worktree remove /tmp/gpp-main
```

Explain any change that `benchstat` marks as significant.

## Code Style

- Follow standard Go conventions.
Expand Down
122 changes: 122 additions & 0 deletions distribution/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

package distribution_test

import (
"archive/tar"
"archive/zip"
"bytes"
"compress/gzip"
"fmt"
"math/rand/v2"
"os"
"path/filepath"
"testing"

distribution "github.com/posit-dev/go-python-packaging/distribution"
"github.com/posit-dev/go-python-packaging/internal/allocbudget"
"github.com/stretchr/testify/require"
)

// Benchmarks and allocation budgets for Parse. The budget tests run in CI
// and fail on any change in allocations; the benchmarks are for comparing a
// branch with main (see CLAUDE.md, "Performance").

const benchMetadata = "Metadata-Version: 2.1\nName: example\nVersion: 1.0\nSummary: An example\n" +
"Requires-Dist: requests>=2.8.1\nRequires-Dist: numpy; python_version >= \"3.8\"\n"

// benchSource is the random contents of n source files of size bytes each.
// Random bytes do not compress, so the archive is as large as a real one.
func benchSource(n, size int) [][]byte {
rng := rand.NewChaCha8([32]byte{7})
files := make([][]byte, n)
for i := range files {
files[i] = make([]byte, size)
_, _ = rng.Read(files[i])
}
return files
}

// writeBenchSDist writes a 3 MB .tar.gz sdist whose PKG-INFO comes after
// its 200 source files, as some build tools write it.
func writeBenchSDist(tb testing.TB) string {
tb.Helper()
var buf bytes.Buffer
gzw := gzip.NewWriter(&buf)
tw := tar.NewWriter(gzw)
add := func(name string, data []byte) {
require.NoError(tb, tw.WriteHeader(&tar.Header{Name: name, Mode: 0o644, Size: int64(len(data))}))
_, err := tw.Write(data)
require.NoError(tb, err)
}
for i, data := range benchSource(200, 16<<10) {
add(fmt.Sprintf("example-1.0/src/mod%d.py", i), data)
}
add("example-1.0/PKG-INFO", []byte(benchMetadata))
require.NoError(tb, tw.Close())
require.NoError(tb, gzw.Close())
path := filepath.Join(tb.TempDir(), "example-1.0.tar.gz")
require.NoError(tb, os.WriteFile(path, buf.Bytes(), 0o600))
return path
}

// writeBenchWheel writes a 3 MB wheel with 200 source files.
func writeBenchWheel(tb testing.TB) string {
tb.Helper()
var buf bytes.Buffer
zw := zip.NewWriter(&buf)
add := func(name string, data []byte) {
w, err := zw.Create(name)
require.NoError(tb, err)
_, err = w.Write(data)
require.NoError(tb, err)
}
for i, data := range benchSource(200, 16<<10) {
add(fmt.Sprintf("example/mod%d.py", i), data)
}
add("example-1.0.dist-info/METADATA", []byte(benchMetadata))
add("example-1.0.dist-info/WHEEL", []byte("Wheel-Version: 1.0\nRoot-Is-Purelib: true\nTag: py3-none-any\n"))
require.NoError(tb, zw.Close())
path := filepath.Join(tb.TempDir(), "example-1.0-py3-none-any.whl")
require.NoError(tb, os.WriteFile(path, buf.Bytes(), 0o600))
return path
}

// benchPackages are the files that Parse reads. budget is its allocation
// budget for each.
var benchPackages = []struct {
name string
write func(testing.TB) string
budget float64
}{
{name: "sdist", write: writeBenchSDist, budget: 2778},
{name: "wheel", write: writeBenchWheel, budget: 955},
}

func BenchmarkParse(b *testing.B) {
for _, p := range benchPackages {
b.Run(p.name, func(b *testing.B) {
path := p.write(b)
b.ReportAllocs()
for b.Loop() {
if _, err := distribution.Parse(path); err != nil {
b.Fatal(err)
}
}
})
}
}

func TestAllocBudget_Parse(t *testing.T) {
for _, p := range benchPackages {
t.Run(p.name, func(t *testing.T) {
path := p.write(t)
_, err := distribution.Parse(path)
require.NoError(t, err, "the budget must not be an error path")
// archive/zip and compress/flate keep decompressors in a
// sync.Pool, so a garbage collection can add or save an
// allocation: the count moves by up to 1 or 2.
allocbudget.CheckWithin(t, p.budget, 2, func() { _, _ = distribution.Parse(path) })
})
}
}
16 changes: 16 additions & 0 deletions distribution/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

package distribution

import "github.com/posit-dev/go-python-packaging/distribution/internal/archiver"

// Errors that Parse can wrap. Match them with errors.Is.
var (
// ErrTooLarge means that a METADATA or PKG-INFO candidate is larger than
// 64 MiB, or that a GPG signature file is larger than 1 MiB.
ErrTooLarge = archiver.ErrTooLarge

// ErrUnknownArchiveFormat means that a file is not a zip archive or a
// gzipped tar archive.
ErrUnknownArchiveFormat = archiver.ErrUnknownFormat
)
46 changes: 46 additions & 0 deletions distribution/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

package distribution_test

import (
"path/filepath"
"strings"
"testing"
"time"

distribution "github.com/posit-dev/go-python-packaging/distribution"
"github.com/posit-dev/go-python-packaging/internal/fuzzseed"
)

// FuzzParseWheelMetadata puts the fuzzed bytes in a wheel's METADATA file
// and parses the wheel through the public entry point.
func FuzzParseWheelMetadata(f *testing.F) {
fuzzseed.Add(f,
"Metadata-Version: 2.4\nName: fz\nVersion: 1.0\nRequires-Dist: a>=1; python_version >= \"3.8\"\n\nbody\n",
"Metadata-Version: 2.1\r\nName: fz\r\nVersion: 1.0\r\n\r\n",
"Metadata-Version: 1.0\nName: fz\nVersion: 1\nAuthor: J\xf6rg\n",
"Metadata-Version: 9.9\nName: fz\n\n",
)
f.Add("Metadata-Version: 2.1\nName: fz\n" + strings.Repeat("Classifier: x\n", 5000) + "\n")

f.Fuzz(func(t *testing.T, metadata string) {
path := filepath.Join(t.TempDir(), "fz-1.0-py3-none-any.whl")
writeWheelFixture(t, path, []fixtureEntry{
{name: "fz-1.0.dist-info/METADATA", contents: metadata},
{name: "fz-1.0.dist-info/WHEEL", contents: "Wheel-Version: 1.0\nTag: py3-none-any\n"},
})
start := time.Now()
pkgs, err := distribution.Parse(path)
fuzzseed.Within(t, start, metadata)
if err != nil {
if pkgs != nil {
t.Fatalf("Parse returned packages and an error: %v", err)
}
return
}
if len(pkgs) != 1 || pkgs[0] == nil {
t.Fatalf("Parse of one wheel returned %d packages and no error", len(pkgs))
}
_ = pkgs[0].MetadataMap()
})
}
Loading