-
Notifications
You must be signed in to change notification settings - Fork 3
/
lookup.c
74 lines (58 loc) · 1.82 KB
/
lookup.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// This file is generated
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sc/dict_int_chr.h" //https://github.com/S-YOU/sc
#include "data.h" //generate from MaxMind csv with gen.py
unsigned char *direct; //first 24 bit direct mapping, cover 75% ranges
DictIntChr map; //remaining ranges as hashtable
static inline const char *lookup(unsigned int ip) {
if (ip < 0x01000000 || ip >= 0xe0000000) return countries[0];
unsigned char range_ip = direct[ip >> 8];
if (range_ip < 255) return countries[range_ip]; //75% match here
//search between bit 32 down to 25, and lookup in hashtable each
DictIntChrItem e;
unsigned mask = 0xffffffff;
while (mask > 0xffffff00) {
e = DictIntChrSearch(map, ip);
if (e) {
return countries[e->val];
}
mask <<= 1;
ip &= mask;
}
return countries[0];
}
//time python gen.py; time clang -O3 sc/dict_int_chr.c lookup.c; time ./a.out
int main() {
unsigned i;
double start = clock();
direct = calloc(0xe00000, 1);
if (DictIntChrInit(&map, 0x100000, 1)) goto error;
for (i = 0; i < 0x98a7; i++) {
direct[r1[i][0]] = r1[i][1];
}
for (i = 0; i < 0x19817; i++) {
memset(direct + rx[i][0], rx[i][1], rx[i][2]);
}
for (i = 0; i < 0xd042; i++) {
DictIntChrInsert(map, maps[i][0], maps[i][1]);
}
//calloc/init/memset time: 14.371ms
printf("calloc/init/memset time: %.3fms\n", (clock() - start) / 1000); start = clock();
// printf("lookup: [%s]\n",lookup(3743093650));
char x = 0;
for (i = 0; i < 0xffffffff; i++) {
x += *lookup(i);
}
direct[0] = x; //to prevent optimization from compiler
//benchmark time: 8296.153ms
printf("benchmark time: %.3fms\n", (clock() - start) / 1000); start = clock();
error:
DictIntChrDestroy(map);
free(direct);
//destroy time: 1.375ms
printf("destroy time: %.3fms\n", (clock() - start) / 1000);
return 0;
}