mirror of
https://github.com/denoland/deno.git
synced 2024-12-23 15:49:44 -05:00
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.
This commit is contained in:
parent
1ee6218e48
commit
bf775e3306
2 changed files with 18 additions and 1 deletions
|
@ -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)?;
|
||||
|
||||
|
|
|
@ -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| {
|
||||
|
|
Loading…
Reference in a new issue