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 17, 2024
1 parent cdb49a5 commit 30827b0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ users)
* `OpamFile.Repos_config.t`: change the type to not allow repositories without an URL [#6249 @kit-ty-kate]

## opam-core
* `OpamStd.List.split`: Improve performance [#6210 @kit-ty-kate]
* `OpamStd.Sys.{get_terminal_columns,uname,getconf,guess_shell_compat}`: Harden the process calls to account for failures [#6230 @kit-ty-kate - fix #6215]
* `OpamStd.Sys.getconf`: was removed, replaced by `get_long_bit` [#6217 @kit-ty-kate]
* `OpamStd.Sys.get_long_bit`: was added, which returns the output of the `getconf LONG_BIT` command [#6217 @kit-ty-kate]
Expand Down
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 30827b0

Please sign in to comment.