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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃殌 Make IP validation 2x faster #2158

Merged
merged 4 commits into from Oct 18, 2022
Merged

馃殌 Make IP validation 2x faster #2158

merged 4 commits into from Oct 18, 2022

Conversation

sadfun
Copy link
Contributor

@sadfun sadfun commented Oct 17, 2022

Description

This commit makes c.IP() and c.IPs() faster, especially when IP validation is enabled.

Benchmarks on my machine after this commit:

> go test -v -run=ctx_test.go -bench=Benchmark_Ctx_IP
goos: darwin
goarch: amd64
pkg: github.com/gofiber/fiber/v2
cpu: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
Benchmark_Ctx_IPs
Benchmark_Ctx_IPs-12                                             5892488               202.4 ns/op            48 B/op          1 allocs/op
Benchmark_Ctx_IPs_v6
Benchmark_Ctx_IPs_v6-12                                          3978466               302.2 ns/op           128 B/op          1 allocs/op
Benchmark_Ctx_IPs_With_IP_Validation
Benchmark_Ctx_IPs_With_IP_Validation-12                          4453332               268.4 ns/op            48 B/op          1 allocs/op
Benchmark_Ctx_IPs_v6_With_IP_Validation
Benchmark_Ctx_IPs_v6_With_IP_Validation-12                       2458620               488.3 ns/op           128 B/op          1 allocs/op
Benchmark_Ctx_IP_With_ProxyHeader
Benchmark_Ctx_IP_With_ProxyHeader-12                            24604593                48.82 ns/op            0 B/op          0 allocs/op
Benchmark_Ctx_IP_With_ProxyHeader_and_IP_Validation
Benchmark_Ctx_IP_With_ProxyHeader_and_IP_Validation-12          12709902                93.63 ns/op            0 B/op          0 allocs/op
Benchmark_Ctx_IP
Benchmark_Ctx_IP-12                                             27120162                43.37 ns/op            8 B/op          1 allocs/op

Before this commit:

> go test -v -run=ctx_test.go -bench=Benchmark_Ctx_IP
goos: darwin
goarch: amd64
pkg: github.com/gofiber/fiber/v2
cpu: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
Benchmark_Ctx_IPs
Benchmark_Ctx_IPs-12                                             4376821               265.9 ns/op            48 B/op          1 allocs/op
Benchmark_Ctx_IPs_v6
Benchmark_Ctx_IPs_v6-12                                          2381112               503.9 ns/op           256 B/op          4 allocs/op
Benchmark_Ctx_IPs_With_IP_Validation
Benchmark_Ctx_IPs_With_IP_Validation-12                          2151280               495.0 ns/op           112 B/op          4 allocs/op
Benchmark_Ctx_IPs_v6_With_IP_Validation
Benchmark_Ctx_IPs_v6_With_IP_Validation-12                       1527709               781.4 ns/op           320 B/op          7 allocs/op
Benchmark_Ctx_IP_With_ProxyHeader
Benchmark_Ctx_IP_With_ProxyHeader-12                            24476161                48.81 ns/op            0 B/op          0 allocs/op
Benchmark_Ctx_IP_With_ProxyHeader_and_IP_Validation
Benchmark_Ctx_IP_With_ProxyHeader_and_IP_Validation-12           5742741               208.6 ns/op            32 B/op          2 allocs/op
Benchmark_Ctx_IP
Benchmark_Ctx_IP-12                                             27599205                43.04 ns/op            8 B/op          1 allocs/op

Type of change

  • [+] New feature (non-breaking change which adds functionality)

Checklist:

  • [+] I have performed a self-review of my own code
  • [+] I have commented my code, particularly in hard-to-understand areas
  • [+] I have added tests that prove my fix is effective or that my feature works
  • [+] New and existing unit tests pass locally with my changes
  • [+] I tried to make my code as fast as possible with as few allocations as possible
  • [+] For new code I have written benchmarks so that they can be analyzed and improved

@efectn
Copy link
Member

efectn commented Oct 17, 2022

Can you create a PR for https://github.com/gofiber/utils

Copy link
Member

@efectn efectn left a comment

Choose a reason for hiding this comment

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

Please add some benchmarks and tests for ips like other utils.

That was a surprise that testing style in gofiber/utils and in gofiber/fiber/utils are different 馃槼
@ReneWerner87 ReneWerner87 merged commit 5316f08 into gofiber:master Oct 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants