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

martian/header: optimized ViaModifier implementation #974

Merged
merged 2 commits into from
Dec 5, 2024
Merged

Conversation

mmatczuk
Copy link
Contributor

@mmatczuk mmatczuk commented Dec 4, 2024

No description provided.

if v := req.Header.Get("Via"); v != "" {
if m.hasLoop(v) {
if via != "" {
if strings.Contains(via, m.requestedBy+"-"+m.boundary) {
Copy link
Contributor

Choose a reason for hiding this comment

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

How about precomputing m.requestedBy+"-"+m.boundary instead of concatenating on every modify?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure.

@Choraden
Copy link
Contributor

Choraden commented Dec 4, 2024

LGTM.

I feel that optimisations are correct, but it would be useful to have #676.

- ViaModifier immutable
- Remove regex
- Remove splitting
- Allocate single chunk of memory (strings.Builder)
- Precompute tag
- Precompute common http literals

Fixes #924
@mmatczuk
Copy link
Contributor Author

mmatczuk commented Dec 4, 2024

@Choraden check the new implementation

@mmatczuk
Copy link
Contributor Author

mmatczuk commented Dec 5, 2024

I'm on it we can wait and see

@mmatczuk
Copy link
Contributor Author

mmatczuk commented Dec 5, 2024

Here we could have a microbenchmark

@Choraden
Copy link
Contributor

Choraden commented Dec 5, 2024

Here we could have a microbenchmark

That's true.

@mmatczuk
Copy link
Contributor Author

mmatczuk commented Dec 5, 2024

 michal@SL-2094:martian/header (mmt/has_loop2)$ benchstat old.txt new.txt
goos: darwin
goarch: arm64
pkg: github.com/saucelabs/forwarder/internal/martian/header
cpu: Apple M1 Pro
            │    old.txt    │               new.txt               │
            │    sec/op     │    sec/op     vs base               │
ViaModifier   2237.0n ± ∞ ¹   152.8n ± ∞ ¹  -93.17% (p=0.029 n=4)
¹ need >= 6 samples for confidence interval at level 0.95

            │   old.txt    │              new.txt               │
            │     B/op     │    B/op      vs base               │
ViaModifier   1440.0 ± ∞ ¹   160.0 ± ∞ ¹  -88.89% (p=0.029 n=4)
¹ need >= 6 samples for confidence interval at level 0.95

            │   old.txt    │              new.txt               │
            │  allocs/op   │  allocs/op   vs base               │
ViaModifier   31.000 ± ∞ ¹   4.000 ± ∞ ¹  -87.10% (p=0.029 n=4)
¹ need >= 6 samples for confidence interval at level 0.95

benchstat old.txt new.txt
goos: darwin
goarch: arm64
pkg: github.com/saucelabs/forwarder/internal/martian/header
cpu: Apple M1 Pro
            │   old.txt    │               new.txt               │
            │    sec/op    │   sec/op     vs base                │
ViaModifier   2299.0n ± 3%   154.3n ± 2%  -93.29% (p=0.000 n=10)

            │   old.txt   │              new.txt               │
            │    B/op     │    B/op     vs base                │
ViaModifier   1440.0 ± 0%   160.0 ± 0%  -88.89% (p=0.000 n=10)

            │   old.txt   │              new.txt               │
            │  allocs/op  │ allocs/op   vs base                │
ViaModifier   31.000 ± 0%   4.000 ± 0%  -87.10% (p=0.000 n=10)
@mmatczuk
Copy link
Contributor Author

mmatczuk commented Dec 5, 2024

It was indeed slow

Copy link
Contributor

@Choraden Choraden left a comment

Choose a reason for hiding this comment

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

💪

@mmatczuk mmatczuk merged commit bd69ea3 into main Dec 5, 2024
6 checks passed
@mmatczuk mmatczuk deleted the mmt/has_loop2 branch December 5, 2024 10:43
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.

2 participants