From a7db699431f7b828385948865509c3cac3ff2c0d Mon Sep 17 00:00:00 2001 From: Clombrong Date: Sun, 29 Jun 2025 13:09:48 +0200 Subject: feat(portal_tcp): schedule buffer flushes on free time i can't believe this speeds up code by a factor of 2 --- portal/tcp/portal.ml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'portal') diff --git a/portal/tcp/portal.ml b/portal/tcp/portal.ml index 207bd91..f01e6ad 100644 --- a/portal/tcp/portal.ml +++ b/portal/tcp/portal.ml @@ -104,8 +104,14 @@ let socket_to_stream (sock : socket) = let chomp c = Lwt_bytes.set send_buffer !send_pos c; incr send_pos; - if !send_pos >= 1024 || c = '>' + if !send_pos >= 1024 then flush_buffer () + else if c = '>' + then + (* flush_buffer is idempotent, so we schedule to do it after other computations + happened. This means it won't take busy time, and it will flush "after", e.g. + when it's full of more interesting stuff. *) + Lwt.async (fun () -> Lwt.pause () >>= flush_buffer) |> Lwt.return else Lwt.return_unit in let close_sock = match sock with -- cgit v1.2.3