根据这个最佳实践 https://moonbingbing.gitbooks.io/openresty-best-practices/content/lua/re.html,建议使用 ngx.re 替代 lua 内置正则,想来也是,既然 openresty 自己实现了,应该比 lua 好才对, 但是简单测试之后发现,ngx.re 比 lua 正则还差,函运行时间差了大概 5 倍,下面是简单循环调用了 100 W 次的结果:
ngx.re.gmatch-i: 3.2929999828339s
string.gmatch: 0.48600006103516s
测试数据:
ngx.re.gmatch("a:100.00,b:200.00,c:300.00", "(\\w+):([+-]?\\d+.\\d+)", "ioj")
string.gmatch("a:100.00,b:200.00,c:300.00", "(\\w+):([+-]?\\d+.\\d+)", "i")
另外,通过 --with-debug 查看日志,发现打印 pcre JIT compiling result: 0,如果根据 https://github.com/openresty/lua-nginx-module#ngxrematch 应该是 1 才是启用了 pcre jit,但通过前后日志好像已经命中了缓存了:
2017/04/19 16:47:23 [debug] 31321#0: *3 lua access handler, uri:"/bench_misc" c:1
2017/04/19 16:47:23 [debug] 31321#0: *3 post access phase: 11
2017/04/19 16:47:23 [debug] 31321#0: *3 lua content handler, uri:"/bench_misc" c:1
2017/04/19 16:47:23 [debug] 31321#0: *3 posix_memalign: 0000000001BCD8F0:4096 @16
2017/04/19 16:47:23 [debug] 31321#0: *3 lua reset ctx
2017/04/19 16:47:23 [debug] 31321#0: *3 lua creating new thread
2017/04/19 16:47:23 [debug] 31321#0: *3 http cleanup add: 0000000001B78E90
2017/04/19 16:47:23 [debug] 31321#0: *3 lua run thread, top:0 c:1
2017/04/19 16:47:23 [debug] 31321#0: *3 lua regex cache miss for match regex "(\w+):([+-]?\d+.\d+)" with options "oji"
2017/04/19 16:47:23 [debug] 31321#0: *3 lua compiling gmatch regex "(\w+):([+-]?\d+.\d+)" with options "oji" (compile once: 1) (dfa mode: 0) (jit mode: 1)
2017/04/19 16:47:23 [debug] 31321#0: *3 pcre JIT compiling result: 0
2017/04/19 16:47:23 [debug] 31321#0: *3 lua saving compiled regex (2 captures) into the cache (entries 0)
2017/04/19 16:47:23 [debug] 31321#0: *3 lua regex cache hit for match regex "(\w+):([+-]?\d+.\d+)" with options "oji"
2017/04/19 16:47:23 [debug] 31321#0: *3 lua regex cache hit for match regex "(\w+):([+-]?\d+.\d+)" with options "oji"
2017/04/19 16:47:23 [debug] 31321#0: *3 lua regex cache hit for match regex "(\w+):([+-]?\d+.\d+)" with options "oji"
2017/04/19 16:47:23 [debug] 31321#0: *3 lua allocate new chainlink and new buf of size 22, cl:0000000001B78EC8
大家有什么建议?