Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bench: shared prefixes bench #537

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions benchmarks/benchmark.ml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,45 @@ let split =
test ~name:"split on whitespace" re (fun re -> ignore (Re.split_full (re ()) s))
;;

let prefixes =
let make_ext =
let chars = "abcdefghiklmnopqrstuvwxyz" in
let buf = Buffer.create 4 in
let rec loop remains =
match remains with
| 0 -> Buffer.contents buf
| _ ->
let char = remains mod String.length chars in
Buffer.add_char buf chars.[char];
loop (remains / String.length chars)
in
fun n ->
Buffer.clear buf;
loop n
in
let n_extensions = 100 in
let n_base = 20 in
let base = String.make n_base 'x' ^ "." in
let extensions = List.init n_extensions ~f:make_ext in
let re () =
(* This regular expression can be heavily optimized by computing the shared prefix *)
List.init 100 ~f:(fun i ->
let ext = make_ext i in
let open Re in
seq [ rep1 any; char '.'; str ext ])
|> Re.alt
|> Re.compile
in
let extensions = Array.of_list extensions in
test ~name:"shared prefixes" re (fun re ->
let re = re () in
for i = 0 to Array.length extensions - 1 do
let extension = extensions.(i) in
let str = base ^ extension in
ignore (Re.execp re str)
done)
;;

let benchmarks =
let benches =
List.map benchmarks ~f:(fun (name, re, cases) ->
Expand Down Expand Up @@ -155,6 +194,7 @@ let benchmarks =
@ Memory.benchmarks
@ repeated_sequence
@ split
@ prefixes
;;

let () =
Expand Down
Loading