Skip to content

Commit 638e458

Browse files
cknittclaude
andcommitted
Check package-relative external paths without allocating
String.starts_with allocates a closure for its local loop on every call, and is_package_relative_path calls it twice for every external, which the benchmarks showed as +163 words on Stdlib_Symbol. It compares the first characters directly now. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kbf3gKyiYMf54wPRxFdUWe Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 2d20578 commit 638e458

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

‎compiler/ml/external_ffi_types.ml‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,14 @@ let valid_ident (s : string) =
123123
true
124124
with E -> false
125125

126+
(* Starts with "./" or "../". Checked for every external, so it compares
127+
characters instead of calling String.starts_with, which allocates a
128+
closure per call. *)
126129
let is_package_relative_path (x : string) =
127-
String.starts_with ~prefix:"./" x || String.starts_with ~prefix:"../" x
130+
let n = String.length x in
131+
n >= 2
132+
&& x.[0] = '.'
133+
&& (x.[1] = '/' || (n >= 3 && x.[1] = '.' && x.[2] = '/'))
128134

129135
let valid_global_name ?loc txt =
130136
if not (valid_ident txt) then

0 commit comments

Comments
 (0)