From 6a51a35dd63aa9833f5a4bcf31900acb7bbf64d6 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Wed, 27 Mar 2024 08:39:30 +0100 Subject: [PATCH] perf(bin/server): increase msg size and don't allocate msg per resp (#1772) * perf(bin/server): increase msg size and don't allocate msg per resp Previously `neqo-server` would respond to a request by repeatedly sending a static 440 byte message (Major-General's Song). Instead of sending 440 bytes, increase the batch size to 4096 bytes. This also matches the `neqo-client` receive buffer size. https://github.com/mozilla/neqo/blob/76630a5ebb6c6b94de6a40cf3f439b9a846f6ab7/neqo-bin/src/bin/client/http3.rs#L165 Previously `ResponseData::repeat` would convert the provided `buf: &[u8]` to ` Vec`, i.e. re-allocate the buf. Instead keep a reference to the original buf, thus removing the allocation. * Remove unnecessary into() --- neqo-bin/src/bin/server/main.rs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/neqo-bin/src/bin/server/main.rs b/neqo-bin/src/bin/server/main.rs index 753794d6f6..62eb19e78c 100644 --- a/neqo-bin/src/bin/server/main.rs +++ b/neqo-bin/src/bin/server/main.rs @@ -5,6 +5,7 @@ // except according to those terms. use std::{ + borrow::Cow, cell::RefCell, cmp::min, collections::HashMap, @@ -196,7 +197,7 @@ trait HttpServer: Display { } struct ResponseData { - data: Vec, + data: Cow<'static, [u8]>, offset: usize, remaining: usize, } @@ -211,7 +212,7 @@ impl From> for ResponseData { fn from(data: Vec) -> Self { let remaining = data.len(); Self { - data, + data: Cow::Owned(data), offset: 0, remaining, } @@ -219,9 +220,9 @@ impl From> for ResponseData { } impl ResponseData { - fn repeat(buf: &[u8], total: usize) -> Self { + fn repeat(buf: &'static [u8], total: usize) -> Self { Self { - data: buf.to_owned(), + data: Cow::Borrowed(buf), offset: 0, remaining: total, } @@ -260,14 +261,7 @@ struct SimpleServer { } impl SimpleServer { - const MESSAGE: &'static [u8] = b"I am the very model of a modern Major-General,\n\ - I've information vegetable, animal, and mineral,\n\ - I know the kings of England, and I quote the fights historical\n\ - From Marathon to Waterloo, in order categorical;\n\ - I'm very well acquainted, too, with matters mathematical,\n\ - I understand equations, both the simple and quadratical,\n\ - About binomial theorem, I'm teeming with a lot o' news,\n\ - With many cheerful facts about the square of the hypotenuse.\n"; + const MESSAGE: &'static [u8] = &[0; 4096]; pub fn new( args: &Args,