当前位置: 首页 > news >正文

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

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

0. 背景

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

1. 性能测试

1.1 简单loop测试

a) 短字符串&正则串

local http_range = 'bytes=10-65535'
local string_re_p = '^bytes=([%d]*)%-([%d]*)$'
local ngx_re_p    = '^bytes=([\\d]*)?\\-([\\d]*)$'
local loop = 1000000local t0 =  get_t()
for i = 1, loop dolocal _, _ = string_match(http_range, string_re_p)
endlocal t1 =  get_t()
for i = 1, loop dolocal m, err = ngx_re_match(http_range, ngx_re_p, "jo")
end
local t2 =  get_t()

Result: 0.247 vs. 0.32

b) 长字符串&复杂正则串

local http_range = 'dsfds65465fwef bytes=12345757860-4465458586465 ewfsd65sd4fg65fsd'
local string_re_p = '.*bytes=([%d]*)%-([%d]*) .+'
local ngx_re_p    = '.*bytes=([\\d]*)?\\-([\\d]*) .+'
local loop = 1000000

Result: 1.16 vs. 0.526

由测试结果可以看出,对于字符串/正则规则越复杂,ngx-re的性能是有优势的

1.2. 加入jit扰动

a) 对照组:ipairs不破坏jit (短串正则)

local http_range = 'bytes=10-65535'
local string_re_p = '^bytes=([%d]*)%-([%d]*)$'
local ngx_re_p    = '^bytes=([\\d]*)?\\-([\\d]*)$'
local loop = 1000000local t0 =  get_t()
for i = 1, loop dofor k, v in ipairs({1,2}) do endlocal _, _ = string_match(http_range, string_re_p)
endlocal t1 =  get_t()
for i = 1, loop dofor k, v in ipairs({1,2}) do endlocal m, err = ngx_re_match(http_range, ngx_re_p, "jo")
end
local t2 =  get_t()

jit-on: 0.369 - 0.326
jit-off: 0.38 - 3.265

b) pairs 破坏jit (短串正则)

local http_range = 'bytes=10-65535'
local string_re_p = '^bytes=([%d]*)%-([%d]*)$'
local ngx_re_p    = '^bytes=([\\d]*)?\\-([\\d]*)$'
local loop = 1000000local t0 =  get_t()
for i = 1, loop dofor k, v in pairs({a=1,b=2}) do endlocal _, _ = string_match(http_range, string_re_p)
endlocal t1 =  get_t()
for i = 1, loop dofor k, v in pairs({a=1,b=2}) do endlocal m, err = ngx_re_match(http_range, ngx_re_p, "jo")
end
local t2 =  get_t()

jit-off: 0.395 - 3.216
jit-on: 0.394 - 1.04

c) pairs + 长复杂串

local http_range = 'dsfds65465fwef bytes=12345757860-4465458586465 ewfsd65sd4fg65fsd'
local string_re_p = '.*bytes=([%d]*)%-([%d]*) .+'
local ngx_re_p    = '.*bytes=([\\d]*)?\\-([\\d]*) .+'
local loop = 1000000

jit-on: 1.31 - 1.30
jit-off: 1.307 - 2.94

超长串 + jit-on:

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
短串正则 带ipirs 0.369 0.326 jit-hit
短串正则 带pairs 0.394 1.04  
长串正则 带pairs 2.775 1.739  
短串正则 带pairs+jit-off 0.395 3.216 jit-off
短串正则 带ipairs+jit-off 0.38 3.265 jit-off

2. 结论

由测试结果可知:
1)在一般情况下,nginx-re正则库更能适应复杂字符串与复杂正则规则的情况,一般情况下比较推荐使用
2)在极简单字符串的情况下,二者差别不大,string正则稍带优势,可以按照方便的写法来写;
3)nginx-re正则受JIT的影响更大,在关闭jit或使用pairs等情况下,可能会有拖累;

http://www.rkmt.cn/news/158980.html

相关文章:

  • 2025年杭州可靠的GEO品牌推荐,GEO优化/豆包优化排名/GEO优化AI工具排名/豆包优化/GEO优化AI搜索GEO老牌厂家哪家好 - 品牌推荐师
  • 2025综合评测:上海哪家装修公司最值得选择? - 品牌排行榜
  • 2025国内镁合金售后较好知名品牌企业有哪些,行业十大优质厂家推荐,靠谱看市场给口碑 - 品牌推荐大师1
  • 网络设备实训室如何选?十年职教实战派解析与厂家深度推荐 - 深度智识库
  • 华东配电柜壳体选型:为何重视双层保温 + “今天国际”供应链? - 品牌排行榜
  • SQL Server中验证大小字母和数字
  • 2025年12月中小客户管理系统软件实力榜:工单管理系统、商机管理系统、业务系统、财务系统、项目管理系统、费用报销系统、一体化管理系统四家企业凭安全适配与口碑出圈 - 海棠依旧大
  • Java毕设项目:基于springboot的汽车租赁管理系统(源码+文档,讲解、调试运行,定制等)
  • 2025年引纸绳行业口碑榜:可靠厂家全解析,柔性吊装带/船用缆绳/电缆网套/钢锭吊具/防割吊带,引纸绳源头厂家哪家好 - 品牌推荐师
  • 大模型面试题19:梯度消失梯度爆炸 纯白话文版
  • 语义层面的SQL注入:LLM 提示词注入攻击深度拆解
  • 2025年改性阻燃pc/abs厂家推荐榜:改性阻燃abs/透明abs758/透明abs050源头厂家精选 - 品牌推荐官
  • MBTI测试网站哪个好?体验感与准确度对比 - 品牌排行榜
  • StarRocks中CTE报错
  • 2026年生成式人工智能与教育国际学术会议(GAIE 2026)
  • 工业级3D点激光相机品牌TOP榜:高速采集+精准定位 - 品牌排行榜
  • GraniStudio:控制轴设置例程
  • 别被榜单骗了:Gemini 3 Pro 的实战避坑指南
  • GraniStudio:两轴圆弧插补例程
  • 2025 粉末冶金厂家推荐榜:十大通用型优质品牌盘点 - 品牌推荐排行榜
  • 2025 最新!8个AI论文平台测评:本科生毕业论文写作全攻略
  • Swift 并发里最容易被忽略的性能坑:你以为是并发,其实是串行
  • 不同调控元件及组合对烟草外源蛋白瞬时表达的效果分析--文献精读188
  • Deep Research完全指南:从RAG到AI自主研究的技术进化!
  • 揭秘Open-AutoGLM应用部署难题:3种高效解决方案助你快速落地
  • 冥想第一千七百四十五天(1745)
  • comres.dll文件丢失? 下载修复方法
  • ANSYS APDL角型板疲劳分析实例与命令流
  • 2025管理咨询推荐机构TOP5权威榜单:甄选企业管理咨询公司 - 工业品网
  • Python爬虫urllib3与requests模块详解