Skip to content

Commit bea4dc3

Browse files
authored
optimize: only supported to store integer index. (#18)
* optimize: only supported to store integer index.
1 parent 0749048 commit bea4dc3

File tree

5 files changed

+52
-23
lines changed

5 files changed

+52
-23
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ INST_LUADIR ?= $(INST_PREFIX)/share/lua/5.1
44
INSTALL ?= install
55
UNAME ?= $(shell uname)
66

7-
CFLAGS := -O2 -g -Wall -fpic -std=c99
7+
CFLAGS := -O2 -g -Wall -fpic -std=c99 -Wno-pointer-to-int-cast -Wno-int-to-pointer-cast
88

99
C_SO_NAME := librestyradixtree.so
1010
LDFLAGS := -shared

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Table of Contents
1919
* [dispatch](#dispatch)
2020
* [Install](#install)
2121
* [DEV ENV](#dev-env)
22+
* [Benchmark](#benchmark)
2223

2324
Status
2425
======
@@ -148,3 +149,34 @@ DEV ENV
148149
```
149150
make dev
150151
```
152+
153+
Benchmark
154+
=========
155+
156+
This is a test example and the result, on my laptop, a single core CPU can match 1 million times in 1.4 seconds (based on 100,000 routes).
157+
158+
```shell
159+
$ cat test.lua
160+
local radix = require("resty.radixtree")
161+
162+
local routes = {}
163+
for i = 1, 1000 * 100 do
164+
routes[i] = {paths = {"/" .. ngx.md5(i) .. "/*"}, metadata = i}
165+
end
166+
167+
local rx = radix.new(routes)
168+
169+
local res
170+
local uri = "/" .. ngx.md5(300) .. "/a"
171+
for _ = 1, 1000 * 1000 do
172+
res = rx:match(uri)
173+
end
174+
175+
ngx.say(res)
176+
177+
$ time resty test.lua
178+
800
179+
resty test.lua 1.31s user 0.07s system 100% cpu 1.378 total
180+
```
181+
182+
[Back to TOC](#table-of-contents)

lib/resty/radixtree.lua

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,11 @@ ffi_cdef[[
7171
void *radix_tree_new();
7272
int radix_tree_destroy(void *t);
7373
int radix_tree_insert(void *t, const unsigned char *buf, size_t len,
74-
void *data);
74+
int idx);
7575
void *radix_tree_find(void *t, const unsigned char *buf, size_t len);
7676
void *radix_tree_search(void *t, void *it, const unsigned char *buf,
7777
size_t len);
78-
void *radix_tree_pcre(void *it, const unsigned char *buf, size_t len);
79-
void *radix_tree_next(void *it, const unsigned char *buf, size_t len);
78+
int radix_tree_pcre(void *it, const unsigned char *buf, size_t len);
8079
int radix_tree_stop(void *it);
8180
8281
void *radix_tree_new_it();
@@ -156,9 +155,8 @@ local function insert_route(self, opts)
156155
self.match_data_index = self.match_data_index + 1
157156
self.match_data[self.match_data_index] = {opts}
158157

159-
local dataptr = ffi_cast('void *', self.match_data_index)
160-
radix.radix_tree_insert(self.tree, path, #path, dataptr)
161-
log_info("insert route path: ", path, " dataprt: ", tostring(dataptr))
158+
radix.radix_tree_insert(self.tree, path, #path, self.match_data_index)
159+
log_info("insert route path: ", path, " dataprt: ", self.match_data_index)
162160
return true
163161
end
164162

@@ -467,15 +465,12 @@ local function match_route(self, path, opts)
467465
end
468466

469467
while true do
470-
local data_idx = radix.radix_tree_pcre(it, path, #path)
471-
log_info("path: ", path, " data_idx: ", tostring(data_idx))
472-
if data_idx == nil then
468+
local idx = radix.radix_tree_pcre(it, path, #path)
469+
if idx <= 0 then
473470
break
474471
end
475472

476-
local idx = tonumber(ffi_cast('intptr_t', data_idx))
477473
routes = self.match_data[idx]
478-
-- log_info("route: ", require("cjson").encode(routes))
479474
if routes then
480475
local route = _match_from_routes(routes, path, opts)
481476
if route then

src/easy_rax.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ radix_tree_destroy(void *t)
2525

2626

2727
int
28-
radix_tree_insert(void *t, const unsigned char *buf, size_t len, void *data)
28+
radix_tree_insert(void *t, const unsigned char *buf, size_t len, int idx)
2929
{
30+
void *data = (void *)idx;
31+
3032
if (t == NULL) {
3133
return -1;
3234
}
@@ -80,29 +82,29 @@ radix_tree_search(void *t, void *it, const unsigned char *buf, size_t len)
8082
}
8183

8284

83-
void *
85+
int
8486
radix_tree_next(void *it, const unsigned char *buf, size_t len)
8587
{
8688
raxIterator *iter = it;
8789

8890
int res = raxNext(iter);
8991
if (!res) {
90-
return NULL;
92+
return -1;
9193
}
9294

9395
// fprintf(stderr, "it key len: %lu buf len: %lu, key: %.*s\n",
9496
// iter->key_len, len, (int)iter->key_len, iter->key);
9597

9698
if (iter->key_len > len ||
9799
memcmp(buf, iter->key, iter->key_len) != 0) {
98-
return NULL;
100+
return -1;
99101
}
100102

101-
return iter->data;
103+
return (int)iter->data;
102104
}
103105

104106

105-
void *
107+
int
106108
radix_tree_pcre(void *it, const unsigned char *buf, size_t len)
107109
{
108110
raxIterator *iter = it;
@@ -111,7 +113,7 @@ radix_tree_pcre(void *it, const unsigned char *buf, size_t len)
111113
while (1) {
112114
res = raxPrev(iter);
113115
if (!res) {
114-
return NULL;
116+
return -1;
115117
}
116118

117119
if (iter->key_len > len ||
@@ -122,7 +124,7 @@ radix_tree_pcre(void *it, const unsigned char *buf, size_t len)
122124
break;
123125
}
124126

125-
return iter->data;
127+
return (int)iter->data;
126128
}
127129

128130

src/easy_rax.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ extern "C" {
3232
void *radix_tree_new();
3333
int radix_tree_destroy(void *t);
3434
int radix_tree_insert(void *t, const unsigned char *buf, size_t len,
35-
void *data);
35+
int idx);
3636
void *radix_tree_find(void *t, const unsigned char *buf, size_t len);
3737
void *radix_tree_search(void *t, void *it, const unsigned char *buf, size_t len);
38-
void *radix_tree_pcre(void *it, const unsigned char *buf, size_t len);
39-
void *radix_tree_next(void *it, const unsigned char *buf, size_t len);
38+
int radix_tree_pcre(void *it, const unsigned char *buf, size_t len);
39+
int radix_tree_next(void *it, const unsigned char *buf, size_t len);
4040
int radix_tree_stop(void *it);
4141

4242
void *radix_tree_new_it();

0 commit comments

Comments
 (0)