Skip to content

Commit

Permalink
Speed-up file access by not using lseek
Browse files Browse the repository at this point in the history
This is related to performance improvments mentionned in ocaml#412
  • Loading branch information
samoht committed Jan 23, 2013
1 parent 7551244 commit b72eba2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
17 changes: 1 addition & 16 deletions src/core/opamFile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1442,23 +1442,8 @@ module Make (F : F) = struct
let dummy_file = OpamFilename.of_string "<dummy>"
let string_of_channel ic =
let n = 32768 in
let s = String.create n in
let b = Buffer.create 1024 in
let rec iter ic b s =
let nread =
try input ic s 0 n
with End_of_file -> 0 in
if nread > 0 then (
Buffer.add_substring b s 0 nread;
iter ic b s
) in
iter ic b s;
Buffer.contents b
let read_from_channel ic =
try F.of_string dummy_file (string_of_channel ic)
try F.of_string dummy_file (OpamSystem.string_of_channel ic)
with OpamFormat.Bad_format msg ->
OpamGlobals.error_and_exit "%s" msg
Expand Down
20 changes: 17 additions & 3 deletions src/core/opamSystem.ml
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,25 @@ let remove_file file =
try Unix.unlink file
with Unix.Unix_error _ -> ()


let string_of_channel ic =
let n = 32768 in
let s = String.create n in
let b = Buffer.create 1024 in
let rec iter ic b s =
let nread =
try input ic s 0 n
with End_of_file -> 0 in
if nread > 0 then (
Buffer.add_substring b s 0 nread;
iter ic b s
) in
iter ic b s;
Buffer.contents b

let read file =
let ic = open_in_bin file in
let n = in_channel_length ic in
let s = String.create n in
really_input ic s 0 n;
let s = string_of_channel ic in
close_in ic;
s

Expand Down
3 changes: 3 additions & 0 deletions src/core/opamSystem.mli
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ val link: string -> string -> unit
expanded and relative paths become absolute. *)
val real_path: string -> string

(** Return the contents of a channel. *)
val string_of_channel: in_channel -> string

(** [read filename] returns the contents of [filename] *)
val read: string -> string

Expand Down

0 comments on commit b72eba2

Please sign in to comment.