From cf351c65c079f7982ea268dec3f26c325b938165 Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Wed, 7 Aug 2024 16:31:02 +0100 Subject: [PATCH] Reduce allocations in opamVersionCompare Test-case: let () let x0 = Gc.stat () in assert (OpamVersionCompare.compare "1.2.3" "1.02.3" == 0); let x1 = Gc.stat () in Printf.printf "minor_words: %.0f\n" (x1.minor_words -. x0.minor_words) Before: minor_words: 156 After: minor_words: 84 --- master_changes.md | 1 + src/core/opamVersionCompare.ml | 8 +++----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/master_changes.md b/master_changes.md index bae8e0d2f2b..ca710438b6a 100644 --- a/master_changes.md +++ b/master_changes.md @@ -121,6 +121,7 @@ users) ## Internal * Stop using polymorphic comparison when comparing `OpamTypes.switch_selections` [#6102 @kit-ty-kate] * Remove the meta opam packages opam and opam-admin [#6115 @kit-ty-kate] + * Reduce allocations in OpamVersionCompare [#6144 @talex5] ## Internal: Windows diff --git a/src/core/opamVersionCompare.ml b/src/core/opamVersionCompare.ml index 9fefb43c24c..9aa47372935 100644 --- a/src/core/opamVersionCompare.ml +++ b/src/core/opamVersionCompare.ml @@ -20,11 +20,9 @@ let is_digit = function (* [skip_while_from i f w m] yields the index of the leftmost character * in the string [s], starting from [i], and ending at [m], that does * not satisfy the predicate [f], or [length w] if no such index exists. *) -let skip_while_from i f w m = - let rec loop i = - if i = m then i - else if f w.[i] then loop (i + 1) else i - in loop i +let rec skip_while_from i f w m = + if i = m then i + else if f w.[i] then skip_while_from (i + 1) f w m else i ;; (* splits a version into (epoch,rest), without the separating ':'. The