Skip to content

Commit 78e8fc7

Browse files
committed
perf(layer): inline layer creation for faster client build
1 parent 5b68106 commit 78e8fc7

5 files changed

Lines changed: 19 additions & 17 deletions

File tree

src/client/layer/cookie/layer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct CookieServiceLayer {
1717

1818
impl CookieServiceLayer {
1919
/// Create a new [`CookieServiceLayer`].
20+
#[inline(always)]
2021
pub const fn new(cookie_store: Option<Arc<dyn CookieStore + 'static>>) -> Self {
2122
Self { cookie_store }
2223
}

src/client/layer/decoder/layer.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub struct DecompressionLayer {
1919

2020
impl DecompressionLayer {
2121
/// Creates a new `DecompressionLayer` with the specified `AcceptEncoding`.
22+
#[inline(always)]
2223
pub const fn new(accept: AcceptEncoding) -> Self {
2324
Self { accept }
2425
}
@@ -27,6 +28,7 @@ impl DecompressionLayer {
2728
impl<S> Layer<S> for DecompressionLayer {
2829
type Service = Decompression<S>;
2930

31+
#[inline(always)]
3032
fn layer(&self, service: S) -> Self::Service {
3133
let decoder = decompression::Decompression::new(service);
3234
let decoder = Decompression::<S>::accept_in_place(decoder, &self.accept);

src/client/layer/redirect/mod.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub struct FollowRedirectLayer<P> {
2323

2424
impl<P> FollowRedirectLayer<P> {
2525
/// Create a new [`FollowRedirectLayer`] with the given redirection [`Policy`].
26+
#[inline(always)]
2627
pub const fn with_policy(policy: P) -> Self {
2728
FollowRedirectLayer { policy }
2829
}
@@ -35,6 +36,7 @@ where
3536
{
3637
type Service = FollowRedirect<S, P>;
3738

39+
#[inline(always)]
3840
fn layer(&self, inner: S) -> Self::Service {
3941
FollowRedirect::with_policy(inner, self.policy.clone())
4042
}
@@ -52,6 +54,7 @@ where
5254
P: Clone,
5355
{
5456
/// Create a new [`FollowRedirect`] with the given redirection [`Policy`].
57+
#[inline(always)]
5558
pub const fn with_policy(inner: S, policy: P) -> Self {
5659
FollowRedirect { inner, policy }
5760
}
@@ -138,22 +141,12 @@ where
138141
match self {
139142
BodyRepr::Some(_) | BodyRepr::Empty => {}
140143
BodyRepr::None => {
141-
if let Some(body) = clone_body(policy, body) {
142-
*self = BodyRepr::Some(body);
144+
if body.size_hint().exact() == Some(0) {
145+
*self = BodyRepr::Some(B::default());
146+
} else if let Some(cloned) = policy.clone_body(body) {
147+
*self = BodyRepr::Some(cloned);
143148
}
144149
}
145150
}
146151
}
147152
}
148-
149-
fn clone_body<P, B, E>(policy: &P, body: &B) -> Option<B>
150-
where
151-
P: Policy<B, E>,
152-
B: Body + Default,
153-
{
154-
if body.size_hint().exact() == Some(0) {
155-
Some(B::default())
156-
} else {
157-
policy.clone_body(body)
158-
}
159-
}

src/client/layer/timeout/future.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ pin_project! {
6969
/// Response future for [`ResponseBodyTimeout`].
7070
pub struct ResponseBodyTimeoutFuture<Fut> {
7171
#[pin]
72-
pub(crate) inner: Fut,
73-
pub(crate) total_timeout: Option<Duration>,
74-
pub(crate) read_timeout: Option<Duration>,
72+
pub(super) inner: Fut,
73+
pub(super) total_timeout: Option<Duration>,
74+
pub(super) read_timeout: Option<Duration>,
7575
}
7676
}
7777

src/client/layer/timeout/layer.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub struct TimeoutLayer {
2525

2626
impl TimeoutLayer {
2727
/// Create a new [`TimeoutLayer`].
28+
#[inline(always)]
2829
pub const fn new(options: TimeoutOptions) -> Self {
2930
TimeoutLayer {
3031
timeout: RequestConfig::new(Some(options)),
@@ -35,6 +36,7 @@ impl TimeoutLayer {
3536
impl<S> Layer<S> for TimeoutLayer {
3637
type Service = Timeout<S>;
3738

39+
#[inline(always)]
3840
fn layer(&self, service: S) -> Self::Service {
3941
Timeout {
4042
inner: service,
@@ -64,6 +66,7 @@ where
6466
self.inner.poll_ready(cx)
6567
}
6668

69+
#[inline(always)]
6770
fn call(&mut self, req: Request<ReqBody>) -> Self::Future {
6871
let (total_timeout, read_timeout) = resolve_timeout_config(&self.timeout, req.extensions());
6972
ResponseFuture {
@@ -83,6 +86,7 @@ pub struct ResponseBodyTimeoutLayer {
8386

8487
impl ResponseBodyTimeoutLayer {
8588
/// Creates a new [`ResponseBodyTimeoutLayer`].
89+
#[inline(always)]
8690
pub const fn new(options: TimeoutOptions) -> Self {
8791
Self {
8892
timeout: RequestConfig::new(Some(options)),
@@ -93,6 +97,7 @@ impl ResponseBodyTimeoutLayer {
9397
impl<S> Layer<S> for ResponseBodyTimeoutLayer {
9498
type Service = ResponseBodyTimeout<S>;
9599

100+
#[inline(always)]
96101
fn layer(&self, inner: S) -> Self::Service {
97102
ResponseBodyTimeout {
98103
inner,
@@ -122,6 +127,7 @@ where
122127
self.inner.poll_ready(cx)
123128
}
124129

130+
#[inline(always)]
125131
fn call(&mut self, req: Request<ReqBody>) -> Self::Future {
126132
let (total_timeout, read_timeout) = resolve_timeout_config(&self.timeout, req.extensions());
127133
ResponseBodyTimeoutFuture {

0 commit comments

Comments
 (0)