Skip to content

Commit

Permalink
Avoid copies by using an offset internally
Browse files Browse the repository at this point in the history
  • Loading branch information
robur-team committed Apr 9, 2024
1 parent b6c4ce3 commit 5ab9523
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
18 changes: 9 additions & 9 deletions src/engine.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1637,14 +1637,13 @@ let incoming ?(is_not_taken = fun _ip -> false) state control_crypto buf =
Error `Udp_ignore
| _, r -> (r : (_, error) result :> (_, [ error | `Udp_ignore ]) result)
in
let rec multi buf (state, (out : string list), payloads, act_opt) =
match Packet.decode_key_op state.session.protocol buf with
let rec multi buf off (state, (out : string list), payloads, act_opt) =
match Packet.decode_key_op state.session.protocol buf off with
| (Error (`Unknown_operation _) | Error `Partial) as e -> ignore_udp_error e
| Error `Tcp_partial ->
(* we don't need to check protocol as [`Tcp_partial] is only ever returned for tcp *)
Ok ({ state with linger = buf }, out, payloads, act_opt)
| Ok (op, key, received, linger) ->
let state = { state with linger } in
Ok ({ state with linger = Octets.sub buf ~off ~len:(String.length buf - off) }, out, payloads, act_opt)
| Ok (op, key, received, linger_off) ->
let* state, out, payloads, act_opt =
match find_channel state key op with
| None -> ignore_udp_error (Error (`No_channel key))
Expand Down Expand Up @@ -1725,11 +1724,12 @@ let incoming ?(is_not_taken = fun _ip -> false) state control_crypto buf =
(state, out, payloads, act))))
in
(* Invariant: [linger] is always empty for UDP *)
if String.length linger = 0 then Ok (state, out, payloads, act_opt)
else multi linger (state, out, payloads, act_opt)
if String.length buf = linger_off then Ok (state, out, payloads, act_opt)
else multi buf linger_off (state, out, payloads, act_opt)
in
let buf = Octets.append state.linger buf in
let r = multi buf (state, [], [], None) in
let state = { state with linger = "" } in
let r = multi buf 0 (state, [], [], None) in
let+ s', out, payloads, act_opt = udp_ignore r in
Log.debug (fun m -> m "out state is %a" State.pp s');
Log.debug (fun m -> m "%u outgoing packets" (List.length out));
Expand Down Expand Up @@ -1980,7 +1980,7 @@ let handle_static_client t s keys ev =
let rec process_one acc linger =
if String.length linger = 0 then Ok ({ t with linger = "" }, acc)
else
match Packet.decode_protocol t.session.protocol linger with
match Packet.decode_protocol t.session.protocol linger 0 with
| Error `Partial -> Error `Partial
| Error `Tcp_partial ->
(* we don't need to check protocol as [`Tcp_partial] is only ever returned for tcp *)
Expand Down
22 changes: 11 additions & 11 deletions src/packet.ml
Original file line number Diff line number Diff line change
Expand Up @@ -212,28 +212,28 @@ let to_be_signed_control op (header, sequence_number, payload) =
Octets.blit_string payload 0 buf (off + 4) (String.length payload);
Bytes.unsafe_to_string buf

let decode_protocol proto buf =
let decode_protocol proto buf off =
let open Result.Syntax in
match proto with
| `Tcp ->
let* () = guard (String.length buf >= 2) `Tcp_partial in
let plen = Octets.get_uint16_be buf 0 in
let+ () = guard (String.length buf - 2 >= plen) `Tcp_partial in
(2, plen)
| `Udp -> Ok (0, String.length buf)
let* () = guard (String.length buf - off >= 2) `Tcp_partial in
let plen = Octets.get_uint16_be buf off in
let+ () = guard (String.length buf - off - 2 >= plen) `Tcp_partial in
(off + 2, plen)
| `Udp -> Ok (off, String.length buf - off)

let decode_key_op proto buf =
let decode_key_op proto buf off =
let open Result.Syntax in
let* poff, plen = decode_protocol proto buf in
let* poff, plen = decode_protocol proto buf off in
let* () = guard (plen >= 1) `Partial in
let opkey = Octets.get_uint8 buf poff in
let op, key = (opkey lsr 3, opkey land 0x07) in
let+ op = int_to_operation op in
let buf, linger =
let buf', linger_off =
( Octets.sub buf ~off:(poff + 1) ~len:(plen - 1),
Octets.sub buf ~off:(poff + plen) ~len:(String.length buf - poff - plen) )
poff + plen )
in
(op, key, buf, linger)
(op, key, buf', linger_off)

let operation = function
| `Ack _ -> Ack
Expand Down

0 comments on commit 5ab9523

Please sign in to comment.