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

net: IPv4 String method is not efficient and could be improved #24306

Closed
vkuzmin-uber opened this issue Mar 8, 2018 · 2 comments

Comments

Projects
None yet
4 participants
@vkuzmin-uber
Copy link
Contributor

commented Mar 8, 2018

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

1.9, 1.10

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

darwin_amd64, linux_amd64

What did you do?

Converted ip.net to human readable string, namely called net.IP.String(). On a scale 1000 IPs, pprof reported it as a one of the most expensive operation.

String representation is often used as a key in map containers, for stringset but pprof will show again that this is a most expensive operation and you will need to provide some custom converter from net.IP to key as a [4]byte or [16]byte.

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.

What did you expect to see?

A better, more efficient implementation

What did you see instead?

A very expensive operation and custom is much more efficient.

@vkuzmin-uber

This comment has been minimized.

Copy link
Contributor Author

commented Mar 8, 2018

I have a better implementation. https://play.golang.org/p/-LG8jshs4ak

Also did BenchMark test:

Before fix:

../bin/go test -bench=BenchmarkIPString -run=^ip ./net/
pkg: net
BenchmarkIPString/IPv4-8 5000000 271 ns/op
BenchmarkIPString/IPv6-8 1000000 1287 ns/op
PASS
ok net 2.986s

After fix:

pkg: net
BenchmarkIPString/IPv4-8 10000000 155 ns/op
BenchmarkIPString/IPv6-8 1000000 1189 ns/op
PASS
ok net 2.938s

This is optimization namely for IPv4 and it gives ~75% improvement.

@vkuzmin-uber vkuzmin-uber changed the title src/net/ip: ip.net.String implementation is not efficient net/ip: ip.net.String implementation is not efficient Mar 8, 2018

@vkuzmin-uber vkuzmin-uber changed the title net/ip: ip.net.String implementation is not efficient net/ip: IPv4 ip.net.String implementation is not efficient Mar 8, 2018

@gopherbot

This comment has been minimized.

Copy link

commented Mar 8, 2018

Change https://golang.org/cl/99395 mentions this issue: net/ip: IPv4 ip.net.String() is not efficient

@andybons andybons added the NeedsFix label Mar 8, 2018

@andybons andybons added this to the Go1.11 milestone Mar 8, 2018

@odeke-em odeke-em changed the title net/ip: IPv4 ip.net.String implementation is not efficient net: IPv4 String method is not efficient and could be improved Mar 8, 2018

@ALTree ALTree added the Performance label Mar 8, 2018

@gopherbot gopherbot closed this in b2d1cd2 Mar 9, 2018

@golang golang locked and limited conversation to collaborators Mar 10, 2019

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.