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

perf: optimize OpamString.split #6210

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ users)
## Test

## Benchmarks
* Add benchmarks for `opam show` [#6212 @kit-ty-kate]

## Reftests
### Tests
Expand Down
24 changes: 21 additions & 3 deletions src/core/opamStd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -692,9 +692,27 @@ module OpamString = struct
let rcut_at = cut_at_aux String.rindex

let split s c =
(* old compat version (Re 1.2.0)
{[Re_str.split (Re_str.regexp (Printf.sprintf "[%c]+" c)) s]} *)
Re.(split (compile (rep1 (char c)))) s
let rec loop acc in_run slice_start len i s c =
if (i : int) < (len : int) then
if (String.get s i : char) = (c : char) then
if not in_run then
if (i : int) > (slice_start : int) then
loop (String.sub s slice_start (i - slice_start) :: acc)
true slice_start len (i + 1) s c
else
loop acc true slice_start len (i + 1) s c
else
loop acc in_run slice_start len (i + 1) s c
else if in_run then
loop acc false i len (i + 1) s c
else
loop acc in_run slice_start len (i + 1) s c
else if not in_run && (slice_start : int) < (len : int) then
String.sub s slice_start (len - slice_start) :: acc
else
acc
in
List.rev (loop [] false 0 (String.length s) 0 s c)

let split_delim s c =
let tokens = Re.(split_full (compile (char c)) s) in
Expand Down
42 changes: 42 additions & 0 deletions tests/bench/bench.ml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@ let () =
launch (fmt "%s switch create six --empty" bin);
time_cmd ~exit:0 (fmt "%s list --installed --short --safe --color=never ocp-indent ocp-index merlin" bin)
in
launch (fmt "%s switch create seven --empty" bin);
launch (fmt "%s install -y --fake dune" bin);
let time_show_installed =
Gc.compact ();
time_cmd ~exit:0 (fmt "%s show dune" bin)
in
let time_show_with_depexts =
Gc.compact ();
time_cmd ~exit:0 (fmt "%s show conf-pkg-config" bin)
in
let time_show_raw =
Gc.compact ();
time_cmd ~exit:0 (fmt "%s show --raw conf-pkg-config" bin)
in
let time_show_precise =
Gc.compact ();
time_cmd ~exit:0 (fmt "%s show --raw conf-pkg-config.2" bin)
in
let json = fmt {|{
"results": [
{
Expand Down Expand Up @@ -148,6 +166,26 @@ let () =
"name": "opam list --installed on non-installed packages",
"value": %f,
"units": "secs"
},
{
"name": "opam show of an installed package",
"value": %f,
"units": "secs"
},
{
"name": "opam show with depexts",
"value": %f,
"units": "secs"
},
{
"name": "opam show --raw pkgname",
"value": %f,
"units": "secs"
},
{
"name": "opam show --raw pkgname.version",
"value": %f,
"units": "secs"
}
]
},
Expand All @@ -172,6 +210,10 @@ let () =
time_install_check_installed
time_install_check_not_installed
time_list_installed_noninstalled_packages
time_show_installed
time_show_with_depexts
time_show_raw
time_show_precise
bin_size
in
print_endline json
Loading