Skip to content

regex very slow at CT #104

@timotheecour

Description

@timotheecour

example 1

nim c -d:danger tests/tests.nim
51 seconds

tests/tests
0.04 seconds

notes

I'm not sure whether it's due to VM code executing tests at CT or whether it's the compilation time itself, but the slow compilation time is noticeable.

profiling

nim c --profileVM tests/tests.nim
shows

prof:     µs    #instr  location
    20773272      4435  /Users/timothee/git_clone/nim/nim-regex/src/regex/compiler.nim(26, 6)
    20679694     23062  /Users/timothee/git_clone/nim/nim-regex/src/regex/compiler.nim(10, 6)
    18337658    468986  /Users/timothee/git_clone/nim/nim-regex/src/regex.nim(289, 8)
     6562522      9757  /Users/timothee/git_clone/nim/nim-regex/src/regex/exptransformation.nim(500, 6)
     6457045     12889  /Users/timothee/git_clone/nim/nim-regex/src/regex/litopt.nim(232, 6)
     5824885    171468  /Users/timothee/git_clone/nim/nim-regex/src/regex/common.nim(69, 6)
     5792159    192220  /Users/timothee/git_clone/nim/nim-regex/src/regex/common.nim(58, 6)
     5709299      7983  /Users/timothee/git_clone/nim/nim-regex/src/regex/nfa.nim(315, 6)
     5682670    326774  /Users/timothee/git_clone/nim/Nim_devel/lib/pure/strutils.nim(2679, 6)
     5496339  12001524  /Users/timothee/git_clone/nim/Nim_devel/lib/pure/strutils.nim(2616, 6)
     5082979     15966  /Users/timothee/git_clone/nim/nim-regex/src/regex/exptransformation.nim(487, 6)
     4869667    736222  /Users/timothee/git_clone/nim/nim-regex/src/regex/nfa.nim(50, 6)
     4473871    573475  /Users/timothee/git_clone/nim/nim-regex/src/regex/litopt.nim(140, 6)
     3463356       876  /Users/timothee/git_clone/nim/nim-regex/src/regex.nim(473, 9)
     3461240     15184  /Users/timothee/git_clone/nim/nim-regex/src/regex/nfamacro.nim(562, 6)
     3249920     46364  /Users/timothee/git_clone/nim/nim-regex/src/regex/nodematch.nim(110, 6)
     2801514   2670768  /Users/timothee/git_clone/nim/nim-unicodedb/src/unicodedb/types.nim(20, 6)
     2593027    966009  /Users/timothee/git_clone/nim/nim-regex/src/regex/nfa.nim(161, 6)
     2459262    238399  /Users/timothee/git_clone/nim/nim-regex/src/regex/exptransformation.nim(65, 6)
     2201504       553  /Users/timothee/git_clone/nim/nim-regex/src/regex/nodematch.nim(10, 6)
     2062921   1242678  /Users/timothee/git_clone/nim/nim-regex/src/regex/nfa.nim(196, 6)
     1742430    269189  /Users/timothee/git_clone/nim/nim-regex/src/regex/parser.nim(742, 6)
     1612111    188082  /Users/timothee/git_clone/nim/nim-regex/src/regex/nfa.nim(294, 6)
     1498531   3173322  /Users/timothee/git_clone/nim/nim-unicodedb/src/unicodedb/types.nim(38, 10)
     1376718     54650  /Users/timothee/git_clone/nim/Nim_devel/lib/system.nim(647, 6)
     1033561    112395  /Users/timothee/git_clone/nim/nim-regex/src/regex/parser.nim(648, 6)
      871969    281895  /Users/timothee/git_clone/nim/nim-regex/src/regex/exptransformation.nim(446, 6)
      858437    327774  /Users/timothee/git_clone/nim/nim-regex/src/regex/litopt.nim(92, 6)
      849408    237897  /Users/timothee/git_clone/nim/nim-regex/src/regex/exptransformation.nim(416, 6)
      791580    168000  /Users/timothee/git_clone/nim/nim-regex/src/regex/nfa.nim(185, 6)

but it's to be taken with a grain of salt due to implementation of profileVM

example 2: self contained benchmark

this test shows a 20_000 X slowdown in VM

when defined case2:
  #[
  nim r -d:case2 -d:danger --benchmarkVM $timn_D/tests/nim/all/t11918.nim
  nim r -d:case2 -d:danger --benchmarkVM --hints:off $timn_D/tests/nim/all/t11918.nim
(5.57392, 459)
(0.0003050000000000006, 459)

nim --eval:'echo 5.57392/0.0003050000000000006'
18275.14754098357
  ]#
  import pkg/regex
  import std/random
  import std/times
  proc main()=
    # let n = 1000
    let n = 2000
    let m = 1
    var r = initRand(123)
    var s = newString(n)
    for i in 0..<n:
      s[i] = cast[char](r.rand(128))
    var pat = r"\w+"
    let reg = re(pat)
    var c = 0
    let t = cpuTime()
    for j in 0..<m:
      for i in 0..<s.len:
        let a = startsWith(s, reg, i)
        c += a.ord
    let t2 = cpuTime() - t
    echo (t2, c)
  static: main()
  main()

proposal

question

is there 1 or a few procs that could be run natively, so that the rest of code at CT would execute fast? In other words, is there an equivalent to hashes.hashVmImpl, hashes.hashVmImplByte in nim-regex?

ideally a proc that's:

  • not generic (so that it's easy to dlopen)
  • has easy to convert params between their value and corresponding PNode (see vmconv)
  • is used by everything else so that performance at CT improves overall

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions