Skip to content

Commit

Permalink
perf: optimize OpamString.split
Browse files Browse the repository at this point in the history
this cuts the time spent on parsing `pacman -Si` significantly down

Co-authored-by: Kate <[email protected]>
  • Loading branch information
c-cube and kit-ty-kate committed Dec 5, 2024
1 parent 2c36bb3 commit 95e17c8
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/core/opamStd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -692,9 +692,25 @@ 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 i slice_start len s c =
if (i : int) < (len : int) then
if s.[i] = (c : char) then
let acc =
if (slice_start : int) < (i : int) then
String.sub s slice_start (i - slice_start) :: acc
else
acc
in
let i = i+1 in
loop acc i i len s c
else
loop acc (i+1) slice_start len s c
else if (i : int) = (slice_start : int) then
acc
else
String.sub s slice_start (len - slice_start) :: acc
in
List.rev (loop [] 0 0 (String.length s) s c)

let split_delim s c =
let tokens = Re.(split_full (compile (char c)) s) in
Expand Down Expand Up @@ -1160,9 +1176,9 @@ module OpamSys = struct
| SH_fish ->
Some (List.fold_left Filename.concat (home ".config") ["fish"; "config.fish"])
| SH_zsh ->
let zsh_home f =
let zsh_home f =
try Filename.concat (Env.get "ZDOTDIR") f
with Not_found -> home f in
with Not_found -> home f in
Some (zsh_home ".zshrc")
| SH_bash ->
let shell =
Expand Down

0 comments on commit 95e17c8

Please sign in to comment.