Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: reuse Request in a session #249

Merged
merged 5 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ohkami/src/header/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ impl<const N: usize, Value> IndexMap<N, Value> {
}
}

#[cfg(any(feature="rt_tokio",feature="rt_async-std"))]
#[inline]
pub(crate) fn clear(&mut self) {
for idx in &mut self.index {*idx = Self::NULL}
self.values.clear();
}

#[inline(always)]
pub(crate) unsafe fn get(&self, index: usize) -> Option<&Value> {
match *self.index.get_unchecked(index) {
Expand Down
9 changes: 9 additions & 0 deletions ohkami/src/request/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,15 @@ impl Headers {
Self::init()
}

#[cfg(any(feature="rt_tokio",feature="rt_async-std"))]
#[inline]
pub(crate) fn clear(&mut self) {
self.standard.clear();
if let Some(map) = &mut self.custom {
map.clear()
}
}

#[inline] pub(crate) fn get_raw(&self, name: Header) -> Option<&CowSlice> {
unsafe {self.standard.get(name as usize)}
}
Expand Down
6 changes: 6 additions & 0 deletions ohkami/src/request/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ impl Store {
pub(super) const fn init() -> Self {
Self(None)
}
#[cfg(any(feature="rt_tokio",feature="rt_async-std"))]
pub(super) fn clear(&mut self) {
if let Some(map) = &mut self.0 {
map.clear()
}
}

#[inline] pub fn insert<Data: Send + Sync + 'static>(&mut self, value: Data) {
self.0.get_or_insert_with(|| Box::new(HashMap::default()))
Expand Down
14 changes: 14 additions & 0 deletions ohkami/src/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ impl Request {
store: Store::init(),
}
}
#[cfg(any(feature="rt_tokio",feature="rt_async-std"))]
#[inline]
pub(crate) fn clear(&mut self) {
if self.__buf__[0] != 0 {
for b in &mut *self.__buf__ {
match b {0 => break, _ => *b = 0}
}
self.path = Path::uninit();
self.query = None;
self.headers.clear();
self.payload = None;
self.store.clear();
} /* else: just after `init`ed or `clear`ed */
}

#[cfg(any(feature="rt_tokio",feature="rt_async-std"))]
#[inline]
Expand Down
2 changes: 1 addition & 1 deletion ohkami/src/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl Upgrade {
impl Response {
#[cfg_attr(not(feature="sse"), inline)]
pub(crate) async fn send(mut self,
conn: &mut (impl AsyncWriter + Unpin + 'static)
conn: &mut (impl AsyncWriter + Unpin)
) -> Upgrade {
self.complete();

Expand Down
30 changes: 9 additions & 21 deletions ohkami/src/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,45 +64,33 @@ impl Session {
}

match timeout_in(Duration::from_secs(env::OHKAMI_KEEPALIVE_TIMEOUT()), async {
let mut req = Request::init();
let mut req = unsafe {Pin::new_unchecked(&mut req)};
loop {
let mut req = Request::init();
let mut req = unsafe {Pin::new_unchecked(&mut req)};
req.clear();
match req.as_mut().read(&mut self.connection).await {
Ok(Some(())) => {
let close = matches!(req.headers.Connection(), Some("close" | "Close"));

let res = match catch_unwind(AssertUnwindSafe(|| self.router.handle(req.get_mut()))) {
let res = match catch_unwind(AssertUnwindSafe({
let req = req.as_mut();
|| self.router.handle(req.get_mut())
})) {
Ok(future) => future.await,
Err(panic) => panicking(panic),
};
let upgrade = res.send(&mut self.connection).await;
if !upgrade.is_none() {
break upgrade
}

if !upgrade.is_none() {break upgrade}
if close {break Upgrade::None}
}
Ok(None) => break Upgrade::None,
Err(res) => {res.send(&mut self.connection).await;},
};
}
}
}).await {
Some(Upgrade::None) | None => {
crate::DEBUG!("about to shutdown connection");

if let Some(err) = {
#[cfg(feature="rt_tokio")] {use crate::__rt__::AsyncWriter;
self.connection.shutdown().await
}
#[cfg(feature="rt_async-std")] {
self.connection.shutdown(std::net::Shutdown::Both)
}
}.err() {
match err.kind() {
std::io::ErrorKind::NotConnected => (),
_ => panic!("Failed to shutdown stream: {err}")
}
}
}

#[cfg(all(feature="ws", any(feature="rt_tokio",feature="rt_async-std")))]
Expand Down
Loading