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

♻️ Refactor: reduce the memory usage of RoutePatternMatch #3335

Merged
merged 4 commits into from
Mar 3, 2025

Conversation

ksw2000
Copy link
Member

@ksw2000 ksw2000 commented Mar 1, 2025

Description

The string variable patternPretty in RoutePatternMatch can be changed into []byte. The byte slice provides greater flexibility for modifying its bytes. Thus, in the benchmark test, we can observe a reduction in memory usage and allocation.

  • Add the new function RemoveEscapeCharBytes
  • Change the returned value type of analyseConstantPart and analyseParameterPart. Because we only use the length of the returned string, we can just return the number.
goos: linux
goarch: amd64
pkg: github.com/gofiber/fiber/v3
cpu: AMD EPYC 7763 64-Core Processor
                                                                              │ route_pattern_match_old.txt │    route_pattern_match_new3.txt    │
                                                                              │           sec/op            │   sec/op     vs base               │
_RoutePatternMatch//api/v1/const_|_match_|_/api/v1/const-4                                      264.3n ± 2%   253.8n ± 2%  -3.95% (p=0.001 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1-4                                        258.5n ± 1%   247.6n ± 2%  -4.24% (p=0.000 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1/-4                                       260.8n ± 3%   249.7n ± 4%  -4.26% (p=0.003 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1/something-4                              265.4n ± 2%   256.1n ± 2%  -3.49% (p=0.000 n=10)
_RoutePatternMatch//api/:param/fixedEnd_|_match_|_/api/abc/fixedEnd-4                           783.8n ± 2%   777.5n ± 3%       ~ (p=0.218 n=10)
_RoutePatternMatch//api/:param/fixedEnd_|_not_match_|_/api/abc/def/fixedEnd-4                   797.8n ± 1%   773.6n ± 3%  -3.03% (p=0.001 n=10)
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity-4                                  920.3n ± 2%   926.0n ± 3%       ~ (p=0.896 n=10)
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity/-4                                 920.4n ± 4%   908.2n ± 2%       ~ (p=0.063 n=10)
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity/1-4                                927.9n ± 2%   919.0n ± 3%       ~ (p=0.579 n=10)
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v-4                                      920.4n ± 3%   889.5n ± 3%  -3.36% (p=0.007 n=10)
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v2-4                                     916.9n ± 2%   891.9n ± 2%  -2.73% (p=0.000 n=10)
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v1/-4                                    938.8n ± 5%   891.2n ± 2%  -5.07% (p=0.000 n=10)
geomean                                                                                         591.7n        575.5n       -2.73%

                                                                              │ route_pattern_match_old.txt │     route_pattern_match_new3.txt     │
                                                                              │            B/op             │    B/op     vs base                  │
_RoutePatternMatch//api/v1/const_|_match_|_/api/v1/const-4                                       168.0 ± 0%   152.0 ± 0%   -9.52% (p=0.000 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1-4                                         160.0 ± 0%   144.0 ± 0%  -10.00% (p=0.000 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1/-4                                        160.0 ± 0%   144.0 ± 0%  -10.00% (p=0.000 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1/something-4                               176.0 ± 0%   160.0 ± 0%   -9.09% (p=0.000 n=10)
_RoutePatternMatch//api/:param/fixedEnd_|_match_|_/api/abc/fixedEnd-4                            440.0 ± 0%   440.0 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/:param/fixedEnd_|_not_match_|_/api/abc/def/fixedEnd-4                    464.0 ± 0%   440.0 ± 0%   -5.17% (p=0.000 n=10)
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity-4                                   536.0 ± 0%   536.0 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity/-4                                  536.0 ± 0%   536.0 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity/1-4                                 536.0 ± 0%   536.0 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v-4                                       544.0 ± 0%   528.0 ± 0%   -2.94% (p=0.000 n=10)
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v2-4                                      544.0 ± 0%   528.0 ± 0%   -2.94% (p=0.000 n=10)
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v1/-4                                     544.0 ± 0%   528.0 ± 0%   -2.94% (p=0.000 n=10)
geomean                                                                                          353.7        337.9        -4.47%
¹ all samples are equal

                                                                              │ route_pattern_match_old.txt │     route_pattern_match_new3.txt     │
                                                                              │          allocs/op          │ allocs/op   vs base                  │
_RoutePatternMatch//api/v1/const_|_match_|_/api/v1/const-4                                       6.000 ± 0%   5.000 ± 0%  -16.67% (p=0.000 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1-4                                         6.000 ± 0%   5.000 ± 0%  -16.67% (p=0.000 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1/-4                                        6.000 ± 0%   5.000 ± 0%  -16.67% (p=0.000 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1/something-4                               6.000 ± 0%   5.000 ± 0%  -16.67% (p=0.000 n=10)
_RoutePatternMatch//api/:param/fixedEnd_|_match_|_/api/abc/fixedEnd-4                            13.00 ± 0%   13.00 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/:param/fixedEnd_|_not_match_|_/api/abc/def/fixedEnd-4                    14.00 ± 0%   13.00 ± 0%   -7.14% (p=0.000 n=10)
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity-4                                   14.00 ± 0%   14.00 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity/-4                                  14.00 ± 0%   14.00 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity/1-4                                 14.00 ± 0%   14.00 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v-4                                       15.00 ± 0%   14.00 ± 0%   -6.67% (p=0.000 n=10)
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v2-4                                      15.00 ± 0%   14.00 ± 0%   -6.67% (p=0.000 n=10)
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v1/-4                                     15.00 ± 0%   14.00 ± 0%   -6.67% (p=0.000 n=10)
geomean                                                                                          10.67        9.811        -8.08%
¹ all samples are equal

I'm still working on reducing memory usage in path.go. However, it does not always lead to a reduction in memory usage. Therefore, I am submitting this pull request first. If I succeed in further reducing memory usage and allocation times, I will submit additional pull requests.

Type of change

Please delete options that are not relevant.

  • Performance improvement (non-breaking change which improves efficiency)

Checklist

Before you submit your pull request, please make sure you meet these requirements:

  • Followed the inspiration of the Express.js framework for new functionalities, making them similar in usage.
  • Conducted a self-review of the code and provided comments for complex or critical parts.
  • Updated the documentation in the /docs/ directory for Fiber's documentation.
  • Added or updated unit tests to validate the effectiveness of the changes or new features.
  • Ensured that new and existing unit tests pass locally with the changes.
  • Verified that any new dependencies are essential and have been agreed upon by the maintainers/community.
  • Aimed for optimal performance with minimal allocations in the new code.
  • Provided benchmarks for the new code to analyze and improve upon.

```
goos: linux
goarch: amd64
pkg: github.com/gofiber/fiber/v3
cpu: AMD EPYC 7763 64-Core Processor
                                                                              │ route_pattern_match_old.txt │    route_pattern_match_new.txt     │
                                                                              │           sec/op            │   sec/op     vs base               │
_RoutePatternMatch//api/v1/const_|_match_|_/api/v1/const-4                                      263.4n ± 2%   249.0n ± 4%  -5.47% (p=0.001 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1-4                                        258.7n ± 4%   244.7n ± 2%  -5.43% (p=0.000 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1/-4                                       254.6n ± 4%   246.3n ± 2%  -3.26% (p=0.000 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1/something-4                              265.1n ± 4%   255.6n ± 3%  -3.60% (p=0.001 n=10)
_RoutePatternMatch//api/:param/fixedEnd_|_match_|_/api/abc/fixedEnd-4                           775.9n ± 3%   775.6n ± 2%       ~ (p=0.424 n=10)
_RoutePatternMatch//api/:param/fixedEnd_|_not_match_|_/api/abc/def/fixedEnd-4                   796.7n ± 3%   767.1n ± 2%  -3.72% (p=0.001 n=10)
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity-4                                  916.2n ± 1%   904.8n ± 3%       ~ (p=0.052 n=10)
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity/-4                                 913.8n ± 4%   909.1n ± 3%       ~ (p=0.393 n=10)
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity/1-4                                915.0n ± 3%   907.2n ± 2%       ~ (p=0.165 n=10)
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v-4                                      917.5n ± 2%   876.7n ± 2%  -4.46% (p=0.000 n=10)
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v2-4                                     918.5n ± 2%   886.8n ± 2%  -3.45% (p=0.000 n=10)
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v1/-4                                    935.6n ± 2%   901.9n ± 2%  -3.60% (p=0.000 n=10)
geomean                                                                                         588.3n        570.7n       -2.99%

                                                                              │ route_pattern_match_old.txt │     route_pattern_match_new.txt      │
                                                                              │            B/op             │    B/op     vs base                  │
_RoutePatternMatch//api/v1/const_|_match_|_/api/v1/const-4                                       168.0 ± 0%   152.0 ± 0%   -9.52% (p=0.000 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1-4                                         160.0 ± 0%   144.0 ± 0%  -10.00% (p=0.000 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1/-4                                        160.0 ± 0%   144.0 ± 0%  -10.00% (p=0.000 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1/something-4                               176.0 ± 0%   160.0 ± 0%   -9.09% (p=0.000 n=10)
_RoutePatternMatch//api/:param/fixedEnd_|_match_|_/api/abc/fixedEnd-4                            440.0 ± 0%   440.0 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/:param/fixedEnd_|_not_match_|_/api/abc/def/fixedEnd-4                    464.0 ± 0%   440.0 ± 0%   -5.17% (p=0.000 n=10)
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity-4                                   536.0 ± 0%   536.0 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity/-4                                  536.0 ± 0%   536.0 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity/1-4                                 536.0 ± 0%   536.0 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v-4                                       544.0 ± 0%   528.0 ± 0%   -2.94% (p=0.000 n=10)
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v2-4                                      544.0 ± 0%   528.0 ± 0%   -2.94% (p=0.000 n=10)
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v1/-4                                     544.0 ± 0%   528.0 ± 0%   -2.94% (p=0.000 n=10)
geomean                                                                                          353.7        337.9        -4.47%
¹ all samples are equal

                                                                              │ route_pattern_match_old.txt │     route_pattern_match_new.txt      │
                                                                              │          allocs/op          │ allocs/op   vs base                  │
_RoutePatternMatch//api/v1/const_|_match_|_/api/v1/const-4                                       6.000 ± 0%   5.000 ± 0%  -16.67% (p=0.000 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1-4                                         6.000 ± 0%   5.000 ± 0%  -16.67% (p=0.000 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1/-4                                        6.000 ± 0%   5.000 ± 0%  -16.67% (p=0.000 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1/something-4                               6.000 ± 0%   5.000 ± 0%  -16.67% (p=0.000 n=10)
_RoutePatternMatch//api/:param/fixedEnd_|_match_|_/api/abc/fixedEnd-4                            13.00 ± 0%   13.00 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/:param/fixedEnd_|_not_match_|_/api/abc/def/fixedEnd-4                    14.00 ± 0%   13.00 ± 0%   -7.14% (p=0.000 n=10)
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity-4                                   14.00 ± 0%   14.00 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity/-4                                  14.00 ± 0%   14.00 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity/1-4                                 14.00 ± 0%   14.00 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v-4                                       15.00 ± 0%   14.00 ± 0%   -6.67% (p=0.000 n=10)
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v2-4                                      15.00 ± 0%   14.00 ± 0%   -6.67% (p=0.000 n=10)
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v1/-4                                     15.00 ± 0%   14.00 ± 0%   -6.67% (p=0.000 n=10)
geomean                                                                                          10.67        9.811        -8.08%
¹ all samples are equal
```
…tPart

```
goos: linux
goarch: amd64
pkg: github.com/gofiber/fiber/v3
cpu: AMD EPYC 7763 64-Core Processor
                                                                              │ route_pattern_match_old.txt │    route_pattern_match_new3.txt    │
                                                                              │           sec/op            │   sec/op     vs base               │
_RoutePatternMatch//api/v1/const_|_match_|_/api/v1/const-4                                      264.3n ± 2%   253.8n ± 2%  -3.95% (p=0.001 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1-4                                        258.5n ± 1%   247.6n ± 2%  -4.24% (p=0.000 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1/-4                                       260.8n ± 3%   249.7n ± 4%  -4.26% (p=0.003 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1/something-4                              265.4n ± 2%   256.1n ± 2%  -3.49% (p=0.000 n=10)
_RoutePatternMatch//api/:param/fixedEnd_|_match_|_/api/abc/fixedEnd-4                           783.8n ± 2%   777.5n ± 3%       ~ (p=0.218 n=10)
_RoutePatternMatch//api/:param/fixedEnd_|_not_match_|_/api/abc/def/fixedEnd-4                   797.8n ± 1%   773.6n ± 3%  -3.03% (p=0.001 n=10)
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity-4                                  920.3n ± 2%   926.0n ± 3%       ~ (p=0.896 n=10)
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity/-4                                 920.4n ± 4%   908.2n ± 2%       ~ (p=0.063 n=10)
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity/1-4                                927.9n ± 2%   919.0n ± 3%       ~ (p=0.579 n=10)
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v-4                                      920.4n ± 3%   889.5n ± 3%  -3.36% (p=0.007 n=10)
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v2-4                                     916.9n ± 2%   891.9n ± 2%  -2.73% (p=0.000 n=10)
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v1/-4                                    938.8n ± 5%   891.2n ± 2%  -5.07% (p=0.000 n=10)
geomean                                                                                         591.7n        575.5n       -2.73%

                                                                              │ route_pattern_match_old.txt │     route_pattern_match_new3.txt     │
                                                                              │            B/op             │    B/op     vs base                  │
_RoutePatternMatch//api/v1/const_|_match_|_/api/v1/const-4                                       168.0 ± 0%   152.0 ± 0%   -9.52% (p=0.000 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1-4                                         160.0 ± 0%   144.0 ± 0%  -10.00% (p=0.000 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1/-4                                        160.0 ± 0%   144.0 ± 0%  -10.00% (p=0.000 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1/something-4                               176.0 ± 0%   160.0 ± 0%   -9.09% (p=0.000 n=10)
_RoutePatternMatch//api/:param/fixedEnd_|_match_|_/api/abc/fixedEnd-4                            440.0 ± 0%   440.0 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/:param/fixedEnd_|_not_match_|_/api/abc/def/fixedEnd-4                    464.0 ± 0%   440.0 ± 0%   -5.17% (p=0.000 n=10)
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity-4                                   536.0 ± 0%   536.0 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity/-4                                  536.0 ± 0%   536.0 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity/1-4                                 536.0 ± 0%   536.0 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v-4                                       544.0 ± 0%   528.0 ± 0%   -2.94% (p=0.000 n=10)
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v2-4                                      544.0 ± 0%   528.0 ± 0%   -2.94% (p=0.000 n=10)
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v1/-4                                     544.0 ± 0%   528.0 ± 0%   -2.94% (p=0.000 n=10)
geomean                                                                                          353.7        337.9        -4.47%
¹ all samples are equal

                                                                              │ route_pattern_match_old.txt │     route_pattern_match_new3.txt     │
                                                                              │          allocs/op          │ allocs/op   vs base                  │
_RoutePatternMatch//api/v1/const_|_match_|_/api/v1/const-4                                       6.000 ± 0%   5.000 ± 0%  -16.67% (p=0.000 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1-4                                         6.000 ± 0%   5.000 ± 0%  -16.67% (p=0.000 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1/-4                                        6.000 ± 0%   5.000 ± 0%  -16.67% (p=0.000 n=10)
_RoutePatternMatch//api/v1/const_|_not_match_|_/api/v1/something-4                               6.000 ± 0%   5.000 ± 0%  -16.67% (p=0.000 n=10)
_RoutePatternMatch//api/:param/fixedEnd_|_match_|_/api/abc/fixedEnd-4                            13.00 ± 0%   13.00 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/:param/fixedEnd_|_not_match_|_/api/abc/def/fixedEnd-4                    14.00 ± 0%   13.00 ± 0%   -7.14% (p=0.000 n=10)
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity-4                                   14.00 ± 0%   14.00 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity/-4                                  14.00 ± 0%   14.00 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/v1/:param/*_|_match_|_/api/v1/entity/1-4                                 14.00 ± 0%   14.00 ± 0%        ~ (p=1.000 n=10) ¹
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v-4                                       15.00 ± 0%   14.00 ± 0%   -6.67% (p=0.000 n=10)
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v2-4                                      15.00 ± 0%   14.00 ± 0%   -6.67% (p=0.000 n=10)
_RoutePatternMatch//api/v1/:param/*_|_not_match_|_/api/v1/-4                                     15.00 ± 0%   14.00 ± 0%   -6.67% (p=0.000 n=10)
geomean                                                                                          10.67        9.811        -8.08%
¹ all samples are equal
```
Copy link
Contributor

coderabbitai bot commented Mar 1, 2025

Walkthrough

The pull request refactors the routing logic in path.go by transitioning from string operations to byte slice manipulation. The RoutePatternMatch function now initializes and processes patterns as byte slices, using utils.ToLowerBytes instead of its string counterpart. Both analyseParameterPart and analyseConstantPart have been updated to return an integer representing the processed length. Additionally, the escape character removal functionality has been split into a string-based and a byte slice-based version.

Changes

File Summary
path.go - Updated RoutePatternMatch to use byte slices and utils.ToLowerBytes, converting the byte slice back to a string for literal comparisons.
- Modified analyseParameterPart and analyseConstantPart to return an integer length rather than a string.
- Split RemoveEscapeChar into two functions: one operating on strings and a new RemoveEscapeCharBytes for byte slices.

Sequence Diagram(s)

sequenceDiagram
    participant R as RoutePatternMatch
    participant U as utils.ToLowerBytes
    participant A as analyseParameterPart/ConstantPart
    participant E as RemoveEscapeCharBytes
    
    R->>U: Convert pattern to lowercase bytes
    U-->>R: Return lowercase bytes
    R->>A: Analyze pattern parts (return integer length)
    A-->>R: Return processed length and segment
    R->>E: Remove escape characters from byte slice
    E-->>R: Return cleaned bytes

Possibly related PRs

Suggested labels

🧹 Updates, v3

Suggested reviewers

  • sixcolors
  • ReneWerner87
  • efectn
  • gaby

Poem

I'm a little rabbit, hopping through the bytes,
Skipping strings for slices in my coding flights.
I nibble on improvements with a joyful cheer,
Watching patterns dance like carrots near.
In every line, a playful code delight!
Hop on, my friend, and keep those changes bright!

✨ Finishing Touches
  • 📝 Generate Docstrings

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Mar 1, 2025

Codecov Report

Attention: Patch coverage is 96.42857% with 1 line in your changes missing coverage. Please review.

Project coverage is 84.23%. Comparing base (6afba95) to head (9e5d98f).
Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
path.go 96.42% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3335      +/-   ##
==========================================
- Coverage   84.29%   84.23%   -0.07%     
==========================================
  Files         116      116              
  Lines       11572    11578       +6     
==========================================
- Hits         9755     9753       -2     
- Misses       1389     1395       +6     
- Partials      428      430       +2     
Flag Coverage Δ
unittests 84.23% <96.42%> (-0.07%) ⬇️

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

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

@ksw2000 ksw2000 marked this pull request as ready for review March 1, 2025 14:35
@ksw2000 ksw2000 requested a review from a team as a code owner March 1, 2025 14:35
@ksw2000 ksw2000 requested review from gaby, sixcolors, ReneWerner87 and efectn and removed request for a team March 1, 2025 14:35
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
path.go (2)

155-185: Consider consistent usage of byte slices or strings to reduce repeated conversions.

You've successfully switched to byte slices for patternPretty but frequently convert it back to a string (lines 167, 169, 172, 185). This repeated conversion could introduce unnecessary overhead. As a good-to-have improvement, consider either consistently using byte slices throughout the matching logic or, if strings are ultimately needed, maintaining a single string variable for comparisons.


629-639: In-place modification in RemoveEscapeCharBytes may impact external usage of the slice.

RemoveEscapeCharBytes performs in-place modifications on the provided slice and then slices it down. While this is efficient, be cautious if the slice is used elsewhere, as the original contents will be altered.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 86cf806 and 7b6ff31.

📒 Files selected for processing (1)
  • path.go (7 hunks)
🔇 Additional comments (3)
path.go (3)

192-204: Initialization of 'n' and 'seg' for route parsing looks good.

Declaring and assigning n and seg before entering the conditional branches simplifies readability. The logic for appending segments and parameters also appears consistent with the new return signatures.


279-291: Returning length instead of substring is cleanly implemented.

Changing analyseConstantPart to return just the processed length boosts performance by avoiding substring allocations. The method handles edge cases such as nextParamPosition being -1 gracefully.


294-399:

❓ Verification inconclusive

Parameter parsing refactor looks coherent, but verify no old function calls remain.

Shifting analyseParameterPart to return an integer for processed length aligns with the PR’s goal of reducing allocations. Please ensure no references to the old (string, *routeSegment) signature linger in the codebase or tests.


🏁 Script executed:

#!/bin/bash
# Verify that no references to the old return signatures exist in the repository:
ast-grep --pattern $'func (routeParser $_) analyseParameterPart($_) (string, $_) {\n $$$ \n}'
ast-grep --pattern $'func (*routeParser) analyseConstantPart($_) (string, $_) {\n $$$ \n}'

Length of output: 182


Action: Confirm No Legacy Signatures Remain

The parameter parsing refactor appears correct with the updated signature returning an integer and a pointer to a route segment. However, automated searches using ast-grep did not generate output, which means the absence of matches is inconclusive. Please manually verify that no lingering references to the old (string, *routeSegment) signature exist in the codebase or in tests—ensure that all call sites have been updated accordingly.

  • Files to check: path.go (lines 294–399) and any test files that invoke analyseParameterPart.
  • Verification: Confirm that no assignments expect a (string, *routeSegment) return type from this function.

@ksw2000
Copy link
Member Author

ksw2000 commented Mar 1, 2025

155-185: Consider consistent usage of byte slices or strings to reduce repeated conversions.

You've successfully switched to byte slices for patternPretty but frequently convert it back to a string (lines 167, 169, 172, 185). This repeated conversion could introduce unnecessary overhead. As a good-to-have improvement, consider either consistently using byte slices throughout the matching logic or, if strings are ultimately needed, maintaining a single string variable for comparisons.

In lines 169, 172, and 185, the program does not allocate memory when converting between strings and byte slices. In line 167, I am still working on reducing the memory usage of parseRoute.

@gaby
Copy link
Member

gaby commented Mar 1, 2025

@ksw2000 I actually ran into the same problem with path.go. It wouldn't consistently reduce memory, so I never pushed the code 😂

@gaby gaby added this to the v3 milestone Mar 1, 2025
@ksw2000
Copy link
Member Author

ksw2000 commented Mar 1, 2025

@ksw2000 I actually ran into the same problem with path.go. It wouldn't consistently reduce memory, so I never pushed the code 😂

I tried for the whole day, and I think this is a big issue that cannot be solved in one go. It needs to be optimized in multiple steps. Some functions can be refactored to be more concise. For example, findLastCharsetPosition and findNextCharsetPosition can return their values earlier.

Copy link
Member

@gaby gaby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 LGTM

@ReneWerner87 ReneWerner87 merged commit 9e6f4fd into gofiber:main Mar 3, 2025
17 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants