Skip to content

Commit

Permalink
Merge pull request #44 from kit-ty-kate/miou-unix-mini-optim
Browse files Browse the repository at this point in the history
Calculate the default value of Miou_unix.{really_read,read,write} ?len only when necessary
  • Loading branch information
dinosaure authored Sep 23, 2024
2 parents e10c048 + 1be006e commit 2309662
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/miou_unix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ let rec unsafe_read ({ fd; non_blocking } as file_descr) off len buf =
blocking_read fd; go ()

let read file_descr ?(off = 0) ?len buf =
let len = Option.value ~default:(Bytes.length buf - off) len in
let len = match len with
| None -> Bytes.length buf - off
| Some len -> len
in
if off < 0 || len < 0 || off > Bytes.length buf - len then
invalid_arg "Miou_unix.read";
unsafe_read file_descr off len buf
Expand All @@ -145,7 +148,10 @@ let rec really_read_go file_descr off len buf =
really_read_go file_descr (off + len') (len - len') buf

let really_read file_descr ?(off = 0) ?len buf =
let len = Option.value ~default:(Bytes.length buf - off) len in
let len = match len with
| None -> Bytes.length buf - off
| Some len -> len
in
if off < 0 || len < 0 || off > Bytes.length buf - len then
invalid_arg "Miou_unix.really_read";
if len > 0 then really_read_go file_descr off len buf
Expand All @@ -172,7 +178,10 @@ let rec unsafe_write ({ fd; non_blocking } as file_descr) off len str =
blocking_write fd; go ()

let write file_descr ?(off = 0) ?len str =
let len = Option.value ~default:(String.length str - off) len in
let len = match len with
| None -> String.length str - off
| Some len -> len
in
if off < 0 || len < 0 || off > String.length str - len then
invalid_arg "Miou_unix.write";
unsafe_write file_descr off len str
Expand Down

0 comments on commit 2309662

Please sign in to comment.