Skip to content

perf: add fast paths for cleanPath#4735

Open
james-yusuke wants to merge 1 commit into
gin-gonic:masterfrom
james-yusuke:perf-path-clean-fast-path
Open

perf: add fast paths for cleanPath#4735
james-yusuke wants to merge 1 commit into
gin-gonic:masterfrom
james-yusuke:perf-path-clean-fast-path

Conversation

@james-yusuke

Copy link
Copy Markdown

Summary

This PR adds fast paths to cleanPath for common already-simple or near-simple paths:

  • single clean relative path segments, e.g. abc
  • paths with repeated leading slashes followed by a single clean segment, e.g. //abc
  • clean absolute paths ending with /segment/.., e.g. /abc/b/..

These cases can be normalized without going through the full buffer-based path cleaning loop.

Benchmarks

Before:

BenchmarkPathCleanLong-10    ~4.02 ms/op    3.22 MB/op    4683 allocs/op

After:

BenchmarkPathCleanLong-10    ~2.63 ms/op    0.81 MB/op    1234 allocs/op

BenchmarkPathClean also reduced allocations from 17 to 15 allocs/op in local runs.

Tests

go test ./...
go test -run='TestPathClean|TestRemoveRepeatedChar'
go test -run=^$ -bench='BenchmarkPathClean(Long)?$' -benchmem -count=5

Checklist

  • Open your pull request against the master branch.
  • All tests pass in available continuous integration systems (e.g., GitHub Actions).
  • Tests are added or modified as needed to cover code changes.
  • If the pull request introduces a new feature, the feature is documented in the docs/doc.md.

This PR is a performance optimization and does not introduce a new user-facing feature, so no documentation update is needed.

@codecov

codecov Bot commented Jul 9, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.48936% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.04%. Comparing base (3dc1cd6) to head (7595ce6).
⚠️ Report is 289 commits behind head on master.

Files with missing lines Patch % Lines
path.go 91.48% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4735      +/-   ##
==========================================
- Coverage   99.21%   98.04%   -1.18%     
==========================================
  Files          42       48       +6     
  Lines        3182     3219      +37     
==========================================
- Hits         3157     3156       -1     
- Misses         17       47      +30     
- Partials        8       16       +8     
Flag Coverage Δ
?
--ldflags="-checklinkname=0" -tags sonic 98.02% <91.48%> (?)
-tags go_json 97.96% <91.48%> (?)
-tags nomsgpack 98.01% <91.48%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 ?
go-1.25 98.04% <91.48%> (?)
go-1.26 98.04% <91.48%> (?)
macos-latest 98.04% <91.48%> (-1.18%) ⬇️
ubuntu-latest 98.04% <91.48%> (-1.18%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant