Skip to content

Commit

Permalink
Fix excessive stack sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
algesten committed Jan 27, 2025
1 parent 94f3e0e commit bda1c68
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
27 changes: 26 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,9 @@ pub(crate) mod test {
use std::io;

use assert_no_alloc::AllocDisabler;
use config::Config;
use config::{Config, ConfigBuilder};
use once_cell::sync::Lazy;
use typestate::AgentScope;

use super::*;

Expand Down Expand Up @@ -988,6 +989,30 @@ pub(crate) mod test {
res.body_mut().read_to_string().unwrap();
}

#[test]
fn ensure_reasonable_stack_sizes() {
macro_rules! ensure {
($type:ty, $size:tt) => {
let sz = std::mem::size_of::<$type>();
// println!("{}: {}", stringify!($type), sz);
assert!(
sz <= $size,
"Stack size of {} is too big {} > {}",
stringify!($type),
sz,
$size
);
};
}

ensure!(RequestBuilder<WithoutBody>, 400); // 288
ensure!(Agent, 100); // 32
ensure!(Config, 400); // 320
ensure!(ConfigBuilder<AgentScope>, 400); // 320
ensure!(Response<Body>, 800); // 760
ensure!(Body, 700); // 648
}

// This doesn't need to run, just compile.
fn _ensure_send_sync() {
fn is_send(_t: impl Send) {}
Expand Down
6 changes: 3 additions & 3 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ pub(crate) struct BodyHandler {
connection: Option<Connection>,
timings: CallTimings,
remote_closed: bool,
redirect: Option<Flow<Redirect>>,
redirect: Option<Box<Flow<Redirect>>>,
}

impl BodyHandler {
Expand Down Expand Up @@ -651,7 +651,7 @@ impl BodyHandler {
let must_close_connection = match flow.proceed().unwrap() {
RecvBodyResult::Redirect(flow) => {
let c = flow.must_close_connection();
self.redirect = Some(flow);
self.redirect = Some(Box::new(flow));
c
}
RecvBodyResult::Cleanup(v) => v.must_close_connection(),
Expand All @@ -674,7 +674,7 @@ impl BodyHandler {

// Unwrap is OK, because we are only consuming the redirect body if
// such a body was signalled by the remote.
let redirect = self.redirect.take();
let redirect = self.redirect.take().map(|b| *b);
Ok(redirect.expect("remote to have signaled redirect"))
}
}
Expand Down
15 changes: 5 additions & 10 deletions src/timings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::fmt;
use std::sync::Arc;
use ureq_proto::ArrayVec;

use crate::config::Timeouts;
use crate::transport::time::{Duration, Instant};
Expand Down Expand Up @@ -84,35 +83,31 @@ impl Timeout {

#[derive(Debug)]
pub(crate) struct CallTimings {
timeouts: Timeouts,
timeouts: Box<Timeouts>,
current_time: CurrentTime,
times: ArrayVec<(Timeout, Instant), 8>,
times: Vec<(Timeout, Instant)>,
}

impl Default for CallTimings {
fn default() -> Self {
Self {
timeouts: Default::default(),
current_time: Default::default(),
times: empty_times(),
times: vec![],
}
}
}

fn empty_times() -> ArrayVec<(Timeout, Instant), 8> {
ArrayVec::from_fn(|_| (Timeout::Global, Instant::AlreadyHappened))
}

impl CallTimings {
pub(crate) fn new(timeouts: Timeouts, current_time: CurrentTime) -> Self {
let mut times = empty_times();
let mut times = Vec::with_capacity(8);

let now = current_time.now();
times.push((Timeout::Global, now));
times.push((Timeout::PerCall, now));

CallTimings {
timeouts,
timeouts: Box::new(timeouts),
current_time,
times,
}
Expand Down

0 comments on commit bda1c68

Please sign in to comment.