Skip to content

Commit 05e97fd

Browse files
committed
fix ip4only and more bce
1 parent 4d78abe commit 05e97fd

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ func main() {
2626
goos: windows
2727
goarch: amd64
2828
pkg: github.com/phuslu/iploc
29-
cpu: 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
29+
cpu: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
3030
BenchmarkIPCountryForIPv4
31-
BenchmarkIPCountryForIPv4-8 74145014 14.96 ns/op 0 B/op 0 allocs/op
31+
BenchmarkIPCountryForIPv4-8 80750439 13.57 ns/op 0 B/op 0 allocs/op
3232
BenchmarkIPCountryForIPv6
33-
BenchmarkIPCountryForIPv6-8 59173639 22.73 ns/op 0 B/op 0 allocs/op
33+
BenchmarkIPCountryForIPv6-8 57166812 20.44 ns/op 0 B/op 0 allocs/op
3434
PASS
35-
ok github.com/phuslu/iploc 2.637s
35+
ok github.com/phuslu/iploc 2.360s
3636
```
3737

3838
### Acknowledgment

iploc.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
// Package iploc provides fastest Geolocation Country library for Go.
1+
// Package iploc provides fastest IP to Country library for Go.
22
//
33
// package main
44
//
55
// import (
66
// "fmt"
7-
// "net"
7+
// "net/netip"
88
// "github.com/phuslu/iploc"
99
// )
1010
//
1111
// func main() {
12-
// fmt.Printf("%s", iploc.Country(net.ParseIP("2001:4860:4860::8888")))
12+
// fmt.Printf("%s", iploc.IPCountry(netip.MustParseAddr("1.1.1.1"))
1313
// }
1414
//
1515
// // Output: US
@@ -23,6 +23,7 @@ import (
2323
"net"
2424
"net/netip"
2525
"os"
26+
"reflect"
2627
"sync"
2728
"unsafe"
2829
)
@@ -53,24 +54,26 @@ func IPCountry(ip netip.Addr) (country []byte) {
5354
i = h + 1
5455
}
5556
}
56-
return ip4txt[i*2-2 : i*2]
57+
// country = ip4txt[i*2-2 : i*2]
58+
sh := (*reflect.SliceHeader)(unsafe.Pointer(&country))
59+
sh.Data = uintptr(unsafe.Add(unsafe.Pointer(&ip4txt[0]), uintptr(i*2-2)))
60+
sh.Len = 2
61+
sh.Cap = 2
62+
return
5763
}
5864

5965
ipv6once.Do(func() {
6066
ipv4only = os.Getenv("IPLOC_IPV4ONLY") != ""
61-
r, _ := gzip.NewReader(bytes.NewReader(ip6bin))
62-
ip6bin, _ = io.ReadAll(r)
67+
if !ipv4only {
68+
r, _ := gzip.NewReader(bytes.NewReader(ip6bin))
69+
ip6bin, _ = io.ReadAll(r)
70+
}
6371
})
6472

6573
if ipv4only {
6674
return
6775
}
6876

69-
ipv6once.Do(func() {
70-
r, _ := gzip.NewReader(bytes.NewReader(ip6bin))
71-
ip6bin, _ = io.ReadAll(r)
72-
})
73-
7477
b := ip.As16()
7578
high := uint64(b[0])<<56 | uint64(b[1])<<48 | uint64(b[2])<<40 | uint64(b[3])<<32 | uint64(b[4])<<24 | uint64(b[5])<<16 | uint64(b[6])<<8 | uint64(b[7])
7679
low := uint64(b[8])<<56 | uint64(b[9])<<48 | uint64(b[10])<<40 | uint64(b[11])<<32 | uint64(b[12])<<24 | uint64(b[13])<<16 | uint64(b[14])<<8 | uint64(b[15])
@@ -84,7 +87,12 @@ func IPCountry(ip netip.Addr) (country []byte) {
8487
i = h + 2
8588
}
8689
}
87-
return ip6txt[i-2 : i]
90+
// country = ip6txt[i-2 : i]
91+
sh := (*reflect.SliceHeader)(unsafe.Pointer(&country))
92+
sh.Data = uintptr(unsafe.Add(unsafe.Pointer(&ip6txt[0]), uintptr(i-2)))
93+
sh.Len = 2
94+
sh.Cap = 2
95+
return
8896
}
8997

9098
// Country return ISO 3166-1 alpha-2 country code of IP.

0 commit comments

Comments
 (0)