久久ER99热精品一区二区-久久精品99国产精品日本-久久精品免费一区二区三区-久久综合九色综合欧美狠狠

博客專欄

EEPW首頁 > 博客 > 探究Openresty中ngx.re與Lua string.re兩種正則的選擇

探究Openresty中ngx.re與Lua string.re兩種正則的選擇

發(fā)布人:天翼云開發(fā)者 時間:2025-12-26 來源:工程師 發(fā)布文章

本文分享自天翼云開發(fā)者社區(qū)《探究Openresty中ngx.re與Lua string.re兩種正則的選擇》.作者:王****淋

0. 背景

openresty中存在2套正則API,即ngx.re與 lua語言的string庫,都可以實現(xiàn)正則匹配查找等功能,那么,這2個API有什么區(qū)別,又如何選擇呢?

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 do
    local _, _ = string_match(http_range, string_re_p)endlocal t1 =  get_t()for i = 1, loop do
    local m, err = ngx_re_match(http_range, ngx_re_p, "jo")endlocal 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

由測試結(jié)果可以看出,對于字符串/正則規(guī)則越復雜,ngx-re的性能是有優(yōu)勢的

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 do
    for k, v in ipairs({1,2}) do end
    local _, _ = string_match(http_range, string_re_p)endlocal t1 =  get_t()for i = 1, loop do
    for k, v in ipairs({1,2}) do end
    local m, err = ngx_re_match(http_range, ngx_re_p, "jo")endlocal 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 do
    for k, v in pairs({a=1,b=2}) do end
    local _, _ = string_match(http_range, string_re_p)endlocal t1 =  get_t()for i = 1, loop do
    for k, v in pairs({a=1,b=2}) do end
    local m, err = ngx_re_match(http_range, ngx_re_p, "jo")endlocal 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'

結(jié)果: 2.775 - 1.739

1.3測試結(jié)果匯總
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-off
2. 結(jié)論

由測試結(jié)果可知:
1)在一般情況下,nginx-re正則庫更能適應復雜字符串與復雜正則規(guī)則的情況,一般情況下比較推薦使用
2)在極簡單字符串的情況下,二者差別不大,string正則稍帶優(yōu)勢,可以按照方便的寫法來寫;
3)nginx-re正則受JIT的影響更大,在關(guān)閉jit或使用pairs等情況下,可能會有拖累;


*博客內(nèi)容為網(wǎng)友個人發(fā)布,僅代表博主個人觀點,如有侵權(quán)請聯(lián)系工作人員刪除。


關(guān)鍵詞: CDN

相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉