Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

monomorphic-iovec: only support bigstrings as buffers #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion async/faraday_async.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ let writev_of_fd fd =
in
fun iovecs ->
let iovecs = Array.of_list_map iovecs ~f:(fun iovec ->
let { Faraday.buffer; off = pos; len } = iovec in
let { Faraday.IOVec.buffer; off = pos; len } = iovec in
Unix.IOVec.of_bigstring ~pos ~len buffer)
in
if Fd.supports_nonblock fd then
Expand Down
4 changes: 2 additions & 2 deletions async/faraday_async.mli
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ open Faraday
val serialize
: Faraday.t
-> yield : (t -> unit Deferred.t)
-> writev : (bigstring iovec list -> [ `Ok of int | `Closed ] Deferred.t)
-> writev : (IOVec.t list -> [ `Ok of int | `Closed ] Deferred.t)
-> unit Deferred.t

val writev_of_fd
: Fd.t
-> bigstring iovec list -> [ `Ok of int | `Closed ] Deferred.t
-> IOVec.t list -> [ `Ok of int | `Closed ] Deferred.t
60 changes: 30 additions & 30 deletions lib/faraday.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,30 @@

type bigstring = Bigstringaf.t

type 'a iovec =
{ buffer : 'a
; off : int
; len : int }
module IOVec = struct
type t =
{ buffer : bigstring
; off : int
; len : int }

let create buffer ~off ~len =
{ buffer; off; len }

let length t =
t.len

let shift { buffer; off; len } n =
assert (n < len);
{ buffer; off = off + n; len = len - n }

let lengthv ts =
let rec loop ts acc =
match ts with
| [] -> acc
| iovec::ts -> loop ts (length iovec + acc)
in
loop ts 0
end

exception Dequeue_empty

Expand Down Expand Up @@ -120,34 +140,14 @@ end = struct
!result
end

module IOVec = struct
let create buffer ~off ~len =
{ buffer; off; len }

let length t =
t.len

let shift { buffer; off; len } n =
assert (n < len);
{ buffer; off = off + n; len = len - n }

let lengthv ts =
let rec loop ts acc =
match ts with
| [] -> acc
| iovec::ts -> loop ts (length iovec + acc)
in
loop ts 0
end

module Buffers = Deque(struct
type t = bigstring iovec
type t = IOVec.t
let sentinel =
let deadbeef = "\222\173\190\239" in
let len = String.length deadbeef in
let buffer = Bigstringaf.create len in
String.iteri (Bigstringaf.unsafe_set buffer) deadbeef;
{ buffer; off = 0; len }
{ IOVec.buffer; off = 0; len }
end)
module Flushes = Deque(struct
type t = int * (unit -> unit)
Expand All @@ -167,7 +167,7 @@ type t =
}

type operation = [
| `Writev of bigstring iovec list
| `Writev of IOVec.t list
| `Yield
| `Close
]
Expand Down Expand Up @@ -368,7 +368,7 @@ let yield t =

let rec shift_buffers t written =
try
let { len; _ } as iovec = Buffers.dequeue_exn t.scheduled in
let { IOVec.len; _ } as iovec = Buffers.dequeue_exn t.scheduled in
if len <= written then begin
shift_buffers t (written - len)
end else
Expand Down Expand Up @@ -443,7 +443,7 @@ let serialize_to_string t =
let bytes = Bytes.create len in
let pos = ref 0 in
List.iter (function
| { buffer; off; len } ->
| { IOVec.buffer; off; len } ->
Bigstringaf.unsafe_blit_to_bytes buffer ~src_off:off bytes ~dst_off:!pos ~len;
pos := !pos + len)
iovecs;
Expand All @@ -461,7 +461,7 @@ let serialize_to_bigstring t =
let bs = Bigstringaf.create len in
let pos = ref 0 in
List.iter (function
| { buffer; off; len } ->
| { IOVec.buffer; off; len } ->
Bigstringaf.unsafe_blit buffer ~src_off:off bs ~dst_off:!pos ~len;
pos := !pos + len)
iovecs;
Expand Down
16 changes: 10 additions & 6 deletions lib/faraday.mli
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,19 @@ val drain : t -> int
consider the Async and Lwt support that this library includes before
attempting to use this these operations directly. *)

type 'a iovec =
{ buffer : 'a
; off : int
; len : int }
module IOVec : sig
(** A view into {!iovec.buffer} starting at {!iovec.off} and with length
{!iovec.len}. *)

type t =
{ buffer : bigstring
; off : int
; len : int }
end


type operation = [
| `Writev of bigstring iovec list
| `Writev of IOVec.t list
| `Yield
| `Close ]
(** The type of operations that the serialier may wish to perform.
Expand All @@ -282,7 +286,7 @@ val operation : t -> operation
function. See the documentation for the {!type:operation} type for details
on how callers should handle these operations. *)

val serialize : t -> (bigstring iovec list -> [`Ok of int | `Closed]) -> [`Yield | `Close]
val serialize : t -> (IOVec.t list -> [`Ok of int | `Closed]) -> [`Yield | `Close]
(** [serialize t writev] sufaces the next operation of [t] to the caller,
handling a [`Writev] operation with [writev] function and performing an
additional bookkeeping on the caller's behalf. In the event that [writev]
Expand Down
8 changes: 4 additions & 4 deletions lib_test/test_faraday.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ open Faraday

module Operation = struct
type t =
[ `Writev of Bigstringaf.t iovec list
[ `Writev of IOVec.t list
| `Yield
| `Close ]

Expand All @@ -13,7 +13,7 @@ module Operation = struct
| `Writev iovecs ->
let writev_len = List.length iovecs in
Format.pp_print_string fmt "Writev [";
List.iteri (fun i { off; len; buffer } ->
List.iteri (fun i { IOVec.off; len; buffer } ->
Format.fprintf fmt "%S" (Bigstringaf.substring ~off ~len buffer);
if i < writev_len - 1 then Format.pp_print_string fmt ", ")
iovecs;
Expand All @@ -25,7 +25,7 @@ module Operation = struct
| `Yield, `Yield -> true
| `Close, `Close -> true
| `Writev xs, `Writev ys ->
let to_string { off; len; buffer } = Bigstringaf.substring ~off ~len buffer in
let to_string { IOVec.off; len; buffer } = Bigstringaf.substring ~off ~len buffer in
let xs = List.map to_string xs in
let ys = List.map to_string ys in
xs = ys
Expand All @@ -36,7 +36,7 @@ module Operation = struct
`Writev
(List.map (fun s ->
let len = String.length s in
{ off = 0; len; buffer = Bigstringaf.of_string ~off:0 ~len s })
{ IOVec.off = 0; len; buffer = Bigstringaf.of_string ~off:0 ~len s })
ss)
;;
end
Expand Down
2 changes: 1 addition & 1 deletion lwt/faraday_lwt.mli
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ open Faraday
val serialize
: t
-> yield : (t -> unit Lwt.t)
-> writev : (bigstring iovec list -> [ `Ok of int | `Closed ] Lwt.t)
-> writev : (IOVec.t list -> [ `Ok of int | `Closed ] Lwt.t)
-> unit Lwt.t
2 changes: 1 addition & 1 deletion lwt_unix/faraday_lwt_unix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ open Lwt.Infix
let writev_of_fd fd =
fun iovecs ->
let lwt_iovecs = Lwt_unix.IO_vectors.create () in
iovecs |> List.iter (fun {Faraday.buffer; off; len} ->
iovecs |> List.iter (fun { Faraday.IOVec.buffer; off; len} ->
Lwt_unix.IO_vectors.append_bigarray lwt_iovecs buffer off len);

Lwt.catch
Expand Down
2 changes: 1 addition & 1 deletion lwt_unix/faraday_lwt_unix.mli
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ include module type of Faraday_lwt

val writev_of_fd
: Lwt_unix.file_descr
-> bigstring iovec list -> [ `Ok of int | `Closed ] Lwt.t
-> IOVec.t list -> [ `Ok of int | `Closed ] Lwt.t