diff --git a/functional-tests/regex-bench.scm b/functional-tests/regex-bench.scm new file mode 100644 index 0000000..0fd96fd --- /dev/null +++ b/functional-tests/regex-bench.scm @@ -0,0 +1,11 @@ +(import (chezscheme) + (regex) + (loops)) + +(let ((rx (compile-rx + (seq (seq (star (lit "a")) + (lit "foo")) + (plus + (lit "b")))))) + (time (upto (n 1000000) + (rx "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaafoobbbbbbb")))) diff --git a/functional-tests/regex.scm b/functional-tests/regex.scm index b7d3fce..9d12770 100644 --- a/functional-tests/regex.scm +++ b/functional-tests/regex.scm @@ -6,8 +6,7 @@ opt star plus - compile-rx - match-rx) + compile-rx) (import (chezscheme) (fmt fmt) (loops) @@ -140,7 +139,7 @@ (_ _))) code) - (define (compile-rx rx) + (define (compile-rx% rx) (let ((rx (append-instr rx (match-instr)))) (optimise-jumps! (list->vector @@ -200,48 +199,50 @@ (set! x y) (set! y tmp))))) - (define (match-rx code txt) + (define (compile-rx rx) ; (fmt #t (dsp "running ") (pretty code) nl) - (call/cc - (lambda (k) - (let ((code-len (vector-length code))) - (let ((threads (mk-thread-set code-len)) - (next-threads (mk-thread-set code-len))) + (let ((code (compile-rx% rx))) + (let ((code-len (vector-length code))) + (let ((threads (mk-thread-set code-len)) + (next-threads (mk-thread-set code-len))) - (define (compile-instr instr) - (match instr - (('match) - (lambda (_ pc) (k #t))) + (define (compile-instr instr) + (match instr + (('match) + (lambda (in-c pc k) (k #t))) - (('char c) - (lambda (in-c pc) - (when (char=? c in-c) - (add-thread! next-threads (+ 1 pc))))) + (('char c) + (lambda (in-c pc k) + (when (char=? c in-c) + (add-thread! next-threads (+ 1 pc))))) - (('jmp l) - (lambda (in-c pc) - (add-thread! threads l))) + (('jmp l) + (lambda (in-c pc k) + (add-thread! threads l))) - (('split l1 l2) - (lambda (in-c pc) - (add-thread! threads l1) - (add-thread! threads l2))))) + (('split l1 l2) + (lambda (in-c pc k) + (add-thread! threads l1) + (add-thread! threads l2))))) - ;; compile to thunks to avoid calling match in the loop. - (let ((code (vector-copy code))) - (upto (n code-len) - (vector-set! code n (compile-instr (vector-ref code n)))) + ;; compile to thunks to avoid calling match in the loop. + (let ((code (vector-copy code))) + (upto (n code-len) + (vector-set! code n (compile-instr (vector-ref code n)))) - (add-thread! threads 0) - (string-iter (in-c txt) - ; (fmt #t (dsp "processing: ") (wrt in-c) nl) - (while (pc (pop-thread! threads)) - ((vector-ref code pc) in-c pc)) - (if (no-threads? next-threads) - (k #f) - (begin - (swap threads next-threads) - (clear-thread-set! next-threads))))) - (any-matches? threads code)))))) + (lambda (txt) + (call/cc + (lambda (k) + (add-thread! threads 0) + (string-iter (in-c txt) + ; (fmt #t (dsp "processing: ") (wrt in-c) nl) + (while (pc (pop-thread! threads)) + ((vector-ref code pc) in-c pc k)) + (if (no-threads? next-threads) + (k #f) + (begin + (swap threads next-threads) + (clear-thread-set! next-threads)))) + (any-matches? threads code))))))))) )