Skip to content

Commit

Permalink
Do a little less copying
Browse files Browse the repository at this point in the history
When decoding operation return offset and length of the package. We
avoid copying the packet only to copy it again without the first byte.
  • Loading branch information
reynir committed Apr 8, 2024
1 parent d59bfc2 commit fc52916
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/engine.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1991,7 +1991,12 @@ let handle_static_client t s keys ev =
| Error `Tcp_partial ->
(* we don't need to check protocol as [`Tcp_partial] is only ever returned for tcp *)
Ok ({ t with linger }, acc)
| Ok (cs, linger) ->
| Ok (poff, plen) ->
let cs, linger =
( String.sub linger poff plen,
String.sub linger (poff + plen)
(String.length linger - poff - plen) )
in
let bad_mac hmac = `Bad_mac (t, hmac, (0, `Data cs)) in
let* d =
incoming_data ~add_timestamp bad_mac keys hmac_algorithm
Expand Down
17 changes: 10 additions & 7 deletions src/packet.ml
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,21 @@ let decode_protocol proto buf =
let* () = guard (String.length buf >= 2) `Tcp_partial in
let plen = String.get_uint16_be buf 0 in
let+ () = guard (String.length buf - 2 >= plen) `Tcp_partial in
( String.sub buf 2 plen,
String.sub buf (plen + 2) (String.length buf - plen - 2) )
| `Udp -> Ok (buf, "")
(2, plen)
| `Udp -> Ok (0, String.length buf)

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

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

0 comments on commit fc52916

Please sign in to comment.