FAQ
The code below is optimized by the compiler:

var m map[string]string
val, ok := m[string(net.IPv4zero)]

http://play.golang.org/p/MdG2PszRDK

testing: warning: no tests to run
PASS
BenchmarkOtpimized-4 200000000 6.70 ns/op 0 B/op 0 allocs/op
BenchmarkNotOtpimized-4 100000000 20.8 ns/op 0 B/op 0 allocs/op
BenchmarkNetIP-4 200000000 6.72 ns/op 0 B/op 0 allocs/op





Le dimanche 17 avril 2016 19:07:25 UTC+2, Jérôme LAFORGE a écrit :
Hello,
According to David's pres (
http://go-talks.appspot.com/github.com/davecheney/presentations/writing-high-performance-go.slide#30)
<http://go-talks.appspot.com/github.com/davecheney/presentations/writing-high-performance-go.slide#30>,
if you want use []byte as map key, the compiler implements some
optimization (avoid the conversion []byte -> string) when you do this:

var m map[string]string
v, ok := m[string(bytes)]



but not:

key := string(bytes)
val, ok := m[key]



As net.IP is []byte, does the compiler implement the same optimization
when you do this :

var m map[string]string
val, ok := m[string(net.IPv4zero)]

or I have to do this:
var m map[string]string
val, ok := m[string([]byte(net.IPv4zero))]

Thx in adv (special thx to Dave for this pres and his kindness :) )
Jérôme
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 4 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 17, '16 at 5:07p
activeApr 18, '16 at 7:46p
posts4
users2
websitegolang.org

2 users in discussion

Jérôme LAFORGE: 3 posts Jakob Borg: 1 post

People

Translate

site design / logo © 2022 Grokbase