Skip to content

Commit

Permalink
monomorphic-iovec: only support bigstrings as buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
seliopou committed Aug 11, 2019
1 parent 8bd31ee commit 147a5b2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 39 deletions.
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

0 comments on commit 147a5b2

Please sign in to comment.