Skip to content

Commit 92ccc92

Browse files
committed
Avoid having to scan the list just to get the number of items
Signed-off-by: Frediano Ziglio <[email protected]>
1 parent d6d05df commit 92ccc92

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

ocaml/forkexecd/test/fe_test.ml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,17 @@ let fd_list () =
7979
let path = "/proc/self/fd" in
8080
(* get rid of the fd used to read the directory *)
8181
Array.fold_right
82-
(fun fd_num l ->
83-
try (fd_num, Unix.readlink (Filename.concat path fd_num)) :: l
84-
with _ -> l
82+
(fun fd_num (l, n) ->
83+
try
84+
let pair = (fd_num, Unix.readlink (Filename.concat path fd_num)) in
85+
(pair :: l, n + 1)
86+
with _ -> (l, n)
8587
)
86-
(Sys.readdir path) []
88+
(Sys.readdir path) ([], 0)
8789

88-
let fd_count () = List.length (fd_list ())
90+
let fd_count () =
91+
let _, n = fd_list () in
92+
n
8993

9094
let irrelevant_strings = ["irrelevant"; "not"; "important"]
9195

@@ -259,7 +263,7 @@ let slave = function
259263
List.filter (fun x -> not (List.mem x irrelevant_strings)) rest
260264
in
261265
(* Check that these fds are present *)
262-
let pairs = fd_list () in
266+
let pairs, _ = fd_list () in
263267
(* Filter any of stdin,stdout,stderr which have been mapped to /dev/null *)
264268
let filtered =
265269
List.filter

0 commit comments

Comments
 (0)