Skip to content

Commit ff1c7ac

Browse files
committed
Speedup OpamSystem.read by using Stdlib.In_channel.input_all when available
1 parent ebff21b commit ff1c7ac

6 files changed

+58
-14
lines changed

master_changes.md

+1
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,4 @@ users)
180180

181181
## opam-core
182182
* `OpamStd.Env`: add `env_string_list` for parsing string list environment variables (comma separated) [#5682 @desumn]
183+
* `OpamSystem.read`: Speedup by using `Stdlib.In_channel.input_all` when available [#5896 @kit-ty-kate]

src/core/dune

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
(write-file opamVersionInfo.ml
2828
"let version = \"%{read-strings:version}\""))
2929

30+
(rule
31+
(enabled_if (>= %{ocaml_version} "4.14"))
32+
(action (copy opamCompatInChannel.real.ml opamCompatInChannel.ml)))
33+
(rule
34+
(enabled_if (< %{ocaml_version} "4.14"))
35+
(action (copy opamCompatInChannel.compat.ml opamCompatInChannel.ml)))
36+
3037
(rule
3138
(targets version)
3239
(deps ../../shell/get_version.ml ../../configure.ac)
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
(**************************************************************************)
2+
(* *)
3+
(* Copyright 2024 Kate Deplaix *)
4+
(* *)
5+
(* All rights reserved. This file is distributed under the terms of the *)
6+
(* GNU Lesser General Public License version 2.1, with the special *)
7+
(* exception on linking described in the file LICENSE. *)
8+
(* *)
9+
(**************************************************************************)
10+
11+
let input_all ic =
12+
let n = 32768 in
13+
let s = Bytes.create n in
14+
let b = Buffer.create 1024 in
15+
let rec iter ic b s =
16+
let nread =
17+
try input ic s 0 n
18+
with End_of_file -> 0 in
19+
if nread > 0 then (
20+
Buffer.add_subbytes b s 0 nread;
21+
iter ic b s
22+
) in
23+
iter ic b s;
24+
Buffer.contents b

src/core/opamCompatInChannel.mli

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(**************************************************************************)
2+
(* *)
3+
(* Copyright 2024 Kate Deplaix *)
4+
(* *)
5+
(* All rights reserved. This file is distributed under the terms of the *)
6+
(* GNU Lesser General Public License version 2.1, with the special *)
7+
(* exception on linking described in the file LICENSE. *)
8+
(* *)
9+
(**************************************************************************)
10+
11+
(** Compatibility module for Stdlib.In_channel which was added in OCaml 4.14 *)
12+
13+
(** Emulates or aliases [Stdlib.In_channel.input_all] *)
14+
val input_all : Stdlib.in_channel -> string

src/core/opamCompatInChannel.real.ml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(**************************************************************************)
2+
(* *)
3+
(* Copyright 2024 Kate Deplaix *)
4+
(* *)
5+
(* All rights reserved. This file is distributed under the terms of the *)
6+
(* GNU Lesser General Public License version 2.1, with the special *)
7+
(* exception on linking described in the file LICENSE. *)
8+
(* *)
9+
(**************************************************************************)
10+
11+
include Stdlib.In_channel

src/core/opamSystem.ml

+1-14
Original file line numberDiff line numberDiff line change
@@ -221,20 +221,7 @@ let remove_file file =
221221
internal_error "Cannot remove %s (%s)." file (Printexc.to_string e)
222222
)
223223

224-
let string_of_channel ic =
225-
let n = 32768 in
226-
let s = Bytes.create n in
227-
let b = Buffer.create 1024 in
228-
let rec iter ic b s =
229-
let nread =
230-
try input ic s 0 n
231-
with End_of_file -> 0 in
232-
if nread > 0 then (
233-
Buffer.add_subbytes b s 0 nread;
234-
iter ic b s
235-
) in
236-
iter ic b s;
237-
Buffer.contents b
224+
let string_of_channel = OpamCompatInChannel.input_all
238225

239226
let read file =
240227
let ic =

0 commit comments

Comments
 (0)