Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce allocations #39

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Commits on Sep 25, 2024

  1. fix(perf): Fix benchmark

    The reader was being read only once in the first iteration, and
    afterwards it was always empty, thus `parseCodeowners` was parsing an
    empty file.
    
    These were the metrics before the change:
    
        $ go test -benchmem -bench=.
        goos: darwin
        goarch: arm64
        pkg: github.com/hairyhenderson/go-codeowners
        BenchmarkParseCodeowners-10      2677503               465.3 ns/op          4096 B/op          1 allocs/op
        PASS
        ok      github.com/hairyhenderson/go-codeowners 2.255s
    
    After the change it's not showing more realistic data:
    
        $ go test -benchmem -bench=.
        goos: darwin
        goarch: arm64
        pkg: github.com/hairyhenderson/go-codeowners
        BenchmarkParseCodeowners-10        31054             38910 ns/op           61424 B/op        641 allocs/op
        PASS
        ok      github.com/hairyhenderson/go-codeowners 3.529s
    inkel committed Sep 25, 2024
    Configuration menu
    Copy the full SHA
    3a36d2e View commit details
    Browse the repository at this point in the history
  2. feat(perf): Precompile getPattern regexps

    This greatly improves the allocations:
    
        $ go test -benchmem -bench=.
        goos: darwin
        goarch: arm64
        pkg: github.com/hairyhenderson/go-codeowners
        BenchmarkParseCodeowners-10        70927             16857 ns/op           27211 B/op        242 allocs/op
        PASS
        ok      github.com/hairyhenderson/go-codeowners 5.297s
    
    This reduces the allocations in 62%:
    
        $ benchstat main.log precompile.log
        goos: darwin
        goarch: arm64
        pkg: github.com/hairyhenderson/go-codeowners
                           │  main.log   │           precompile.log            │
                           │   sec/op    │   sec/op     vs base                │
        ParseCodeowners-10   38.72µ ± 2%   17.63µ ± 6%  -54.46% (p=0.000 n=10)
    
                           │   main.log   │            precompile.log            │
                           │     B/op     │     B/op      vs base                │
        ParseCodeowners-10   59.95Ki ± 0%   26.56Ki ± 0%  -55.70% (p=0.000 n=10)
    
                           │  main.log  │           precompile.log           │
                           │ allocs/op  │ allocs/op   vs base                │
        ParseCodeowners-10   641.0 ± 0%   242.0 ± 0%  -62.25% (p=0.000 n=10)
    inkel committed Sep 25, 2024
    Configuration menu
    Copy the full SHA
    fa2c30d View commit details
    Browse the repository at this point in the history
  3. feat(perf): Replace regexp replace with strings replace

    These functions are faster than the regexp ones.
    inkel committed Sep 25, 2024
    Configuration menu
    Copy the full SHA
    483c183 View commit details
    Browse the repository at this point in the history