diff options
author | Clombrong <cromblong@egregore.fun> | 2025-06-29 13:09:48 +0200 |
---|---|---|
committer | Clombrong <cromblong@egregore.fun> | 2025-06-29 13:09:48 +0200 |
commit | a7db699431f7b828385948865509c3cac3ff2c0d (patch) | |
tree | f721612c74aa215cbca1e35ac2b5f656262ff5b7 | |
parent | c3e9064c860178a525c35f02911f2e492724f43b (diff) |
feat(portal_tcp): schedule buffer flushes on free time
i can't believe this speeds up code by a factor of 2
-rw-r--r-- | portal/tcp/portal.ml | 8 |
1 files changed, 7 insertions, 1 deletions
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 |