Skip to content

Commit 0b19b71

Browse files
committed
inline functions
Former-commit-id: 4b0dea2
1 parent 98e71c4 commit 0b19b71

9 files changed

+66
-86
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Phus Lu
3+
Copyright (c) 2020 Phus Lu
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# geoip - fast geoip country library
1+
# geoip - fastest geoip country library
22

33
[![godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/phuslu/geoip) [![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://raw.githubusercontent.com/phuslu/geoip/master/LICENSE) [![goreport](https://goreportcard.com/badge/github.com/phuslu/geoip)](https://goreportcard.com/report/github.com/phuslu/geoip) [![coverage](https://img.shields.io/badge/coverage-100%25-brightgreen)](https://gocover.io/github.com/phuslu/geoip)
44

@@ -8,21 +8,22 @@
88
package main
99

1010
import (
11+
"fmt"
1112
"net"
1213
"github.com/phuslu/geoip"
1314
)
1415

1516
func main() {
16-
println(string(geoip.Country(net.ParseIP("2001:4860:4860::8888"))))
17+
fmt.Printf("%s", geoip.Country(net.ParseIP("2001:4860:4860::8888")))
1718
}
1819

1920
// Output: US
2021
```
2122

2223
### Benchmarks
2324
```
24-
BenchmarkGeoIpCountryForIPv4-8 35.2 ns/op 0 B/op 0 allocs/op
25-
BenchmarkGeoIpCountryForIPv6-8 43.6 ns/op 0 B/op 0 allocs/op
25+
BenchmarkGeoIpCountryForIPv4-8 20.8 ns/op 0 B/op 0 allocs/op
26+
BenchmarkGeoIpCountryForIPv6-8 38.2 ns/op 0 B/op 0 allocs/op
2627
```
2728

2829
### Acknowledgment

geoip.go

+28-21
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Package geoip provides fastest GeoIP Country library for Go.
22
//
3-
// eg.
4-
//
5-
// country := geoip.Country(net.ParseIP("1.1.1.1"))
6-
// fmt.Printf("%s\n", country)
3+
// fmt.Printf("%s\n", geoip.Country(net.ParseIP("1.1.1.1")))
74
//
85
// // Output: US
96
package geoip
@@ -13,31 +10,41 @@ import (
1310
"net"
1411
)
1512

16-
// Country find iso3166 country code of IP.
13+
// Country find ISO 3166-1 alpha-2 country code of IP.
1714
func Country(ip net.IP) (country []byte) {
1815
if ip == nil {
1916
return
2017
}
18+
2119
if ip4 := ip.To4(); ip4 != nil {
22-
country = country4(binary.BigEndian.Uint32(ip4))
20+
// ipv4
21+
n := binary.BigEndian.Uint32(ip4)
22+
i, j := 0, len(ips)
23+
for i < j {
24+
h := (i + j) >> 1
25+
if ips[h] > n {
26+
j = h
27+
} else {
28+
i = h + 1
29+
}
30+
}
31+
country = geo[i<<1-2 : i<<1]
2332
} else {
24-
country = country6(binary.BigEndian.Uint64(ip), binary.BigEndian.Uint64(ip[8:]))
25-
}
26-
return
27-
}
28-
29-
func country4(n uint32) (country []byte) {
30-
i, j := 0, len(ips)
31-
for i < j {
32-
h := int(uint(i+j) >> 1)
33-
if ips[h] <= n {
34-
i = h + 1
35-
} else {
36-
j = h
33+
// ipv6
34+
high := binary.BigEndian.Uint64(ip)
35+
low := binary.BigEndian.Uint64(ip[8:])
36+
i, j := 0, len(ips6)
37+
for i < j {
38+
h := (i + j) >> 1 & ^0xf
39+
n := ip6uint(h)
40+
if n > high || (n == high && ip6uint(h+8) > low) {
41+
j = h
42+
} else {
43+
i = h + 16
44+
}
3745
}
46+
country = geo6[i>>3-2 : i>>3]
3847
}
3948

40-
country = geo[i*2-2 : i*2]
41-
4249
return
4350
}

geoip6.go

-25
This file was deleted.

geoip6_safe.go

-32
This file was deleted.

geoip_db.go.REMOVED.git-id

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8f11b974f898b3938be68810f1d244d6603ff4dc
1+
b25db6cfc8619db0925d4a5b0bd7bfd10a4108c1

geoip_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ func TestGeoIpCountry(t *testing.T) {
5454
}
5555

5656
func BenchmarkGeoIpCountryForIPv4(b *testing.B) {
57-
ip := net.ParseIP("8.8.8.8")
57+
ip := net.IP{8, 8, 8, 8}
5858

5959
b.ReportAllocs()
6060
b.ResetTimer()
6161
for i := 0; i < b.N; i++ {
62-
Country(net.IP(ip))
62+
Country(ip)
6363
}
6464
}
6565

safe.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// +build !386
2+
// +build !amd64
3+
// +build !amd64p32
4+
// +build !arm64
5+
// +build !ppc64le
6+
// +build !mipsle
7+
// +build !mips64le
8+
// +build !mips64p32le
9+
10+
package geoip
11+
12+
import (
13+
"encoding/binary"
14+
)
15+
16+
func ip6uint(i int) uint64 {
17+
return binary.LittleEndian.Uint64(ips6[i:])
18+
}

unsafe.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// +build 386 amd64 amd64p32 arm64 ppc64le mipsle mips64le mips64p32le
2+
3+
package geoip
4+
5+
import (
6+
"unsafe"
7+
)
8+
9+
func ip6uint(i int) uint64 {
10+
return *(*uint64)(unsafe.Pointer(&ips6[i]))
11+
}

0 commit comments

Comments
 (0)