探究Openresty中ngx.re与Lua string.re两种正则的选择

[复制链接]
发表于 2025-12-26 16:26:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
本文分享自天翼云开发者社区《探究Openresty中ngx.re与Lua string.re两种正则的选择》.作者:王****淋
0. 配景

openresty中存在2套正则API,即ngx.re与 lua语言的string库,都可以实现正则匹配查找等功能,那么,这2个API有什么区别,又怎样选择呢?
1. 性能测试

1.1 简单loop测试

a) 短字符串&正则串
  1. local http_range = 'bytes=10-65535'
  2. local string_re_p = '^bytes=([%d]*)%-([%d]*)$'
  3. local ngx_re_p    = '^bytes=([\\d]*)?\\-([\\d]*)$'
  4. local loop = 1000000
  5. local t0 =  get_t()
  6. for i = 1, loop do
  7.     local _, _ = string_match(http_range, string_re_p)
  8. end
  9. local t1 =  get_t()
  10. for i = 1, loop do
  11.     local m, err = ngx_re_match(http_range, ngx_re_p, "jo")
  12. end
  13. local t2 =  get_t()
复制代码
Result: 0.247 vs. 0.32
b) 长字符串&复杂正则串
  1. local http_range = 'dsfds65465fwef bytes=12345757860-4465458586465 ewfsd65sd4fg65fsd'
  2. local string_re_p = '.*bytes=([%d]*)%-([%d]*) .+'
  3. local ngx_re_p    = '.*bytes=([\\d]*)?\\-([\\d]*) .+'
  4. local loop = 1000000
复制代码
Result: 1.16 vs. 0.526
由测试结果可以看出,对于字符串/正则规则越复杂,ngx-re的性能是有上风的
1.2. 参加jit扰动

a) 对照组:ipairs不粉碎jit (短串正则)
  1. local http_range = 'bytes=10-65535'
  2. local string_re_p = '^bytes=([%d]*)%-([%d]*)$'
  3. local ngx_re_p    = '^bytes=([\\d]*)?\\-([\\d]*)$'
  4. local loop = 1000000
  5. local t0 =  get_t()
  6. for i = 1, loop do
  7.     for k, v in ipairs({1,2}) do end
  8.     local _, _ = string_match(http_range, string_re_p)
  9. end
  10. local t1 =  get_t()
  11. for i = 1, loop do
  12.     for k, v in ipairs({1,2}) do end
  13.     local m, err = ngx_re_match(http_range, ngx_re_p, "jo")
  14. end
  15. local t2 =  get_t()
复制代码
jit-on: 0.369 - 0.326
jit-off: 0.38 - 3.265
b) pairs 粉碎jit (短串正则)
  1. local http_range = 'bytes=10-65535'
  2. local string_re_p = '^bytes=([%d]*)%-([%d]*)$'
  3. local ngx_re_p    = '^bytes=([\\d]*)?\\-([\\d]*)$'
  4. local loop = 1000000
  5. local t0 =  get_t()
  6. for i = 1, loop do
  7.     for k, v in pairs({a=1,b=2}) do end
  8.     local _, _ = string_match(http_range, string_re_p)
  9. end
  10. local t1 =  get_t()
  11. for i = 1, loop do
  12.     for k, v in pairs({a=1,b=2}) do end
  13.     local m, err = ngx_re_match(http_range, ngx_re_p, "jo")
  14. end
  15. local t2 =  get_t()
复制代码
jit-off: 0.395 - 3.216
jit-on: 0.394 - 1.04
c) pairs + 长复杂串
  1. local http_range = 'dsfds65465fwef bytes=12345757860-4465458586465 ewfsd65sd4fg65fsd'
  2. local string_re_p = '.*bytes=([%d]*)%-([%d]*) .+'
  3. local ngx_re_p    = '.*bytes=([\\d]*)?\\-([\\d]*) .+'
  4. local loop = 1000000
复制代码
jit-on: 1.31 - 1.30
jit-off: 1.307 - 2.94
超长串 + jit-on:
  1. local http_range = 'dsfds6546vsdvsdfdsfsdfsdfwaasdasdasdas5fwef bytes=12354345345345757860-4465453453453453453453453458586465 ewfsd65safdknsalk;nlkasdnflksdajfhkldashjnfkl;ashfgjklahfg;jlsasd4fg65fsd'
复制代码
结果: 2.775 - 1.739
1.3测试结果汇总

 string.matchngx.re.match备注短串正则0.247 秒0.32 秒jit-hit短串正则 带ipirs0.3690.326jit-hit短串正则 带pairs0.3941.04 长串正则 带pairs2.7751.739 短串正则 带pairs+jit-off0.3953.216jit-off短串正则 带ipairs+jit-off0.383.265jit-off2. 结论

由测试结果可知:
1)在一样寻常情况下,nginx-re正则库更能顺应复杂字符串与复杂正则规则的情况,一样寻常情况下比力保举使用
2)在极简单字符串的情况下,二者差异不大,string正则稍带上风,可以按照方便的写法来写;
3)nginx-re正则受JIT的影响更大,在关闭jit或使用pairs等情况下,大概会有拖累;
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表