Skip to content

Commit

Permalink
feat: reuse buffer on the receiver end
Browse files Browse the repository at this point in the history
  • Loading branch information
leostera committed Dec 21, 2023
1 parent f831d19 commit f419c15
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 15 deletions.
5 changes: 2 additions & 3 deletions riot/lib/net/socket.ml
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ let rec accept ?(timeout = Infinity) (socket : Socket.listen_socket) =

let controlling_process _socket ~new_owner:_ = Ok ()

let rec receive ?(timeout = Infinity) ~len socket =
let buf = Bigstringaf.create len in
let rec receive ?(timeout = Infinity) ~buf socket =
match Io.read ~fd:socket ~buf with
| exception Fd.(Already_closed _) -> Error `Closed
| `Abort reason -> Error (`Unix_error reason)
| `Retry -> syscall "read" `r socket @@ receive ~timeout ~len
| `Retry -> syscall "read" `r socket @@ receive ~timeout ~buf
| `Read 0 -> Error `Closed
| `Read _len -> Ok buf

Expand Down
2 changes: 1 addition & 1 deletion riot/lib/net/socket.mli
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ val controlling_process : 'a -> new_owner:'b -> (unit, 'c) result

val receive :
?timeout:timeout ->
len:int ->
buf:Bigstringaf.t ->
Fd.t ->
(Bigstringaf.t, [> `Unix_error of Unix.error | `Closed ]) result

Expand Down
2 changes: 1 addition & 1 deletion riot/riot.mli
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ module Net : sig

val receive :
?timeout:timeout ->
len:int ->
buf:Bigstringaf.t ->
stream_socket ->
(Bigstringaf.t, [> `Closed | `Timeout ]) result

Expand Down
4 changes: 0 additions & 4 deletions riot/runtime/net/io_vec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,3 @@ external _unsafe_writev : Unix.file_descr -> iovec array -> int -> int = "riot_u
let writev fd bigstring =
let iovecs = [| Io_vector.of_bigstring bigstring |] in
_unsafe_writev fd iovecs 1




6 changes: 3 additions & 3 deletions riot/runtime/net/unix_readv.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
CAMLprim value riot_unix_readv(value fd, value iovs, value count) {
CAMLparam3(fd, iovs, count);

int len = Int_val(count);
size_t len = Int_val(count);
struct iovec c_iovs[len];

for (int i = 0; i < len; i++) {
Expand All @@ -18,8 +18,8 @@ CAMLprim value riot_unix_readv(value fd, value iovs, value count) {
c_iovs[i].iov_len = Int_val(Field(iov, 1));
}

int ret = readv(Int_val(fd), c_iovs, len);
size_t ret = readv(Int_val(fd), c_iovs, len);
if (ret == -1) caml_failwith("readv failed");

CAMLreturn(Val_int(ret));
CAMLreturn(Val_long(ret));
}
6 changes: 3 additions & 3 deletions riot/runtime/net/unix_writev.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
CAMLprim value riot_unix_writev(value fd, value iovs, value count) {
CAMLparam3(fd, iovs, count);

int len = Int_val(count);
size_t len = Int_val(count);
struct iovec c_iovs[len];

for (int i = 0; i < len; i++) {
Expand All @@ -18,8 +18,8 @@ CAMLprim value riot_unix_writev(value fd, value iovs, value count) {
c_iovs[i].iov_len = Int_val(Field(iov, 1));
}

int ret = writev(Int_val(fd), c_iovs, len);
size_t ret = writev(Int_val(fd), c_iovs, len);
if (ret == -1) caml_failwith("writev failed");

CAMLreturn(Val_int(ret));
CAMLreturn(Val_long(ret));
}

0 comments on commit f419c15

Please sign in to comment.