Enable JIT in PCRE to improve regular expressions performance
This commit is contained in:
parent
4b0ba5e3f1
commit
61755945d2
2 changed files with 13 additions and 3 deletions
|
|
@ -432,7 +432,12 @@ proc initRegex(pattern: string, flags: int, study = true): Regex =
|
||||||
|
|
||||||
if study:
|
if study:
|
||||||
# XXX investigate JIT
|
# XXX investigate JIT
|
||||||
result.pcreExtra = pcre.study(result.pcreObj, 0x0, addr errorMsg)
|
var options: cint = 0
|
||||||
|
var hasJit: cint
|
||||||
|
if pcre.config(pcre.CONFIG_JIT, addr hasJit) == 0:
|
||||||
|
if hasJit == 1'i32:
|
||||||
|
options = pcre.STUDY_JIT_COMPILE
|
||||||
|
result.pcreExtra = pcre.study(result.pcreObj, options, addr errorMsg)
|
||||||
if errorMsg != nil:
|
if errorMsg != nil:
|
||||||
raise StudyError(msg: $errorMsg)
|
raise StudyError(msg: $errorMsg)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -87,7 +87,12 @@ proc re*(s: string, flags = {reExtended, reStudy}): Regex =
|
||||||
result.h = rawCompile(s, cast[cint](flags - {reStudy}))
|
result.h = rawCompile(s, cast[cint](flags - {reStudy}))
|
||||||
if reStudy in flags:
|
if reStudy in flags:
|
||||||
var msg: cstring
|
var msg: cstring
|
||||||
result.e = pcre.study(result.h, 0, addr msg)
|
var options: cint = 0
|
||||||
|
var hasJit: cint
|
||||||
|
if pcre.config(pcre.CONFIG_JIT, addr hasJit) == 0:
|
||||||
|
if hasJit == 1'i32:
|
||||||
|
options = pcre.STUDY_JIT_COMPILE
|
||||||
|
result.e = pcre.study(result.h, options, addr msg)
|
||||||
if not isNil(msg): raiseInvalidRegex($msg)
|
if not isNil(msg): raiseInvalidRegex($msg)
|
||||||
|
|
||||||
proc matchOrFind(s: string, pattern: Regex, matches: var openArray[string],
|
proc matchOrFind(s: string, pattern: Regex, matches: var openArray[string],
|
||||||
|
|
@ -214,7 +219,7 @@ proc find*(s: string, pattern: Regex, start = 0): int =
|
||||||
var
|
var
|
||||||
rtarray = initRtArray[cint](3)
|
rtarray = initRtArray[cint](3)
|
||||||
rawMatches = rtarray.getRawData
|
rawMatches = rtarray.getRawData
|
||||||
res = pcre.exec(pattern.h, nil, s, len(s).cint, start.cint, 0'i32,
|
res = pcre.exec(pattern.h, pattern.e, s, len(s).cint, start.cint, 0'i32,
|
||||||
cast[ptr cint](rawMatches), 3)
|
cast[ptr cint](rawMatches), 3)
|
||||||
if res < 0'i32: return res
|
if res < 0'i32: return res
|
||||||
return rawMatches[0]
|
return rawMatches[0]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue