From 61755945d2267f0406973d6359ef7a4fe4a79731 Mon Sep 17 00:00:00 2001 From: Pavel Roschin Date: Thu, 22 Sep 2016 00:13:01 +0300 Subject: [PATCH 1/2] Enable JIT in PCRE to improve regular expressions performance --- lib/impure/nre.nim | 7 ++++++- lib/impure/re.nim | 9 +++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/impure/nre.nim b/lib/impure/nre.nim index 76a0f3db60..5d62cb0b94 100644 --- a/lib/impure/nre.nim +++ b/lib/impure/nre.nim @@ -432,7 +432,12 @@ proc initRegex(pattern: string, flags: int, study = true): Regex = if study: # 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: raise StudyError(msg: $errorMsg) diff --git a/lib/impure/re.nim b/lib/impure/re.nim index bf397550a1..f73f9547a7 100644 --- a/lib/impure/re.nim +++ b/lib/impure/re.nim @@ -87,7 +87,12 @@ proc re*(s: string, flags = {reExtended, reStudy}): Regex = result.h = rawCompile(s, cast[cint](flags - {reStudy})) if reStudy in flags: 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) proc matchOrFind(s: string, pattern: Regex, matches: var openArray[string], @@ -214,7 +219,7 @@ proc find*(s: string, pattern: Regex, start = 0): int = var rtarray = initRtArray[cint](3) 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) if res < 0'i32: return res return rawMatches[0] From dbef9f97c2efe38b9cb39130567f56df907785d3 Mon Sep 17 00:00:00 2001 From: Pavel Roschin Date: Mon, 10 Oct 2016 02:21:20 +0300 Subject: [PATCH 2/2] Remove TODO for PCRE JIT --- lib/impure/nre.nim | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/impure/nre.nim b/lib/impure/nre.nim index 5d62cb0b94..594c8aba8c 100644 --- a/lib/impure/nre.nim +++ b/lib/impure/nre.nim @@ -431,7 +431,6 @@ proc initRegex(pattern: string, flags: int, study = true): Regex = raise SyntaxError(msg: $errorMsg, pos: errOffset, pattern: pattern) if study: - # XXX investigate JIT var options: cint = 0 var hasJit: cint if pcre.config(pcre.CONFIG_JIT, addr hasJit) == 0: