summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2023-07-20 07:30:17 -0600
committerGitHub <noreply@github.com>2023-07-20 07:30:17 -0600
commitbf775e3306faeb2a18525ba38f9c3e60e1e5ed16 (patch)
tree9c2ec1c7b50d079abfed3bd8deab2f0f11aff8c7 /ext
parent1ee6218e488b277eeb5b80f87dbea97ebaf5dcb2 (diff)
refactor(ext/http): Use const thread-local initializer for slightly better perf (#19881)
Benchmarking shows numbers are pretty close, however this is recommended for the best possible thread-local performance and may improve in future Rust compiler revisions.
Diffstat (limited to 'ext')
-rw-r--r--ext/http/http_next.rs5
-rw-r--r--ext/http/slab.rs14
2 files changed, 18 insertions, 1 deletions
diff --git a/ext/http/http_next.rs b/ext/http/http_next.rs
index e95839005..5635fbd4b 100644
--- a/ext/http/http_next.rs
+++ b/ext/http/http_next.rs
@@ -12,6 +12,7 @@ use crate::response_body::ResponseBytesInner;
use crate::response_body::V8StreamHttpResponseBody;
use crate::slab::slab_drop;
use crate::slab::slab_get;
+use crate::slab::slab_init;
use crate::slab::slab_insert;
use crate::slab::SlabId;
use crate::websocket_upgrade::WebSocketUpgrade;
@@ -836,6 +837,8 @@ pub fn op_http_serve<HTTP>(
where
HTTP: HttpPropertyExtractor,
{
+ slab_init();
+
let listener =
HTTP::get_listener_for_rid(&mut state.borrow_mut(), listener_rid)?;
@@ -886,6 +889,8 @@ pub fn op_http_serve_on<HTTP>(
where
HTTP: HttpPropertyExtractor,
{
+ slab_init();
+
let connection =
HTTP::get_connection_for_rid(&mut state.borrow_mut(), connection_rid)?;
diff --git a/ext/http/slab.rs b/ext/http/slab.rs
index 9f7c1f3e9..dbe1a6635 100644
--- a/ext/http/slab.rs
+++ b/ext/http/slab.rs
@@ -32,7 +32,7 @@ pub struct HttpSlabRecord {
}
thread_local! {
- static SLAB: RefCell<Slab<HttpSlabRecord>> = RefCell::new(Slab::with_capacity(1024));
+ static SLAB: RefCell<Slab<HttpSlabRecord>> = const { RefCell::new(Slab::new()) };
}
macro_rules! http_trace {
@@ -56,6 +56,18 @@ pub struct SlabEntry(
RefMut<'static, Slab<HttpSlabRecord>>,
);
+const SLAB_CAPACITY: usize = 1024;
+
+pub fn slab_init() {
+ SLAB.with(|slab: &RefCell<Slab<HttpSlabRecord>>| {
+ // Note that there might already be an active HTTP server, so this may just
+ // end up adding room for an additional SLAB_CAPACITY items. All HTTP servers
+ // on a single thread share the same slab.
+ let mut slab = slab.borrow_mut();
+ slab.reserve(SLAB_CAPACITY);
+ })
+}
+
pub fn slab_get(index: SlabId) -> SlabEntry {
http_trace!(index, "slab_get");
let mut lock: RefMut<'static, Slab<HttpSlabRecord>> = SLAB.with(|x| {