Skip to content

Commit 929d917

Browse files
authored
perf(ws): inline frequently called accessor methods (#782)
1 parent 7dc3424 commit 929d917

1 file changed

Lines changed: 29 additions & 4 deletions

File tree

src/client/websocket/mod.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ impl WebSocketRequestBuilder {
6060
/// # Returns
6161
///
6262
/// * `Self` - The modified instance with the custom WebSocket accept key.
63+
#[inline]
6364
pub fn accept_key<K>(mut self, key: K) -> Self
6465
where
6566
K: Into<Cow<'static, str>>,
@@ -90,6 +91,7 @@ impl WebSocketRequestBuilder {
9091
/// .protocols(["protocol1", "protocol2"])
9192
/// .build();
9293
/// ```
94+
#[inline]
9395
pub fn protocols<P>(mut self, protocols: P) -> Self
9496
where
9597
P: IntoIterator,
@@ -101,36 +103,42 @@ impl WebSocketRequestBuilder {
101103
}
102104

103105
/// Sets the websocket max_frame_size configuration.
106+
#[inline]
104107
pub fn max_frame_size(mut self, max_frame_size: usize) -> Self {
105108
self.config.max_frame_size = Some(max_frame_size);
106109
self
107110
}
108111

109112
/// Sets the websocket read_buffer_size configuration.
113+
#[inline]
110114
pub fn read_buffer_size(mut self, read_buffer_size: usize) -> Self {
111115
self.config.read_buffer_size = read_buffer_size;
112116
self
113117
}
114118

115119
/// Sets the websocket write_buffer_size configuration.
120+
#[inline]
116121
pub fn write_buffer_size(mut self, write_buffer_size: usize) -> Self {
117122
self.config.write_buffer_size = write_buffer_size;
118123
self
119124
}
120125

121126
/// Sets the websocket max_write_buffer_size configuration.
127+
#[inline]
122128
pub fn max_write_buffer_size(mut self, max_write_buffer_size: usize) -> Self {
123129
self.config.max_write_buffer_size = max_write_buffer_size;
124130
self
125131
}
126132

127133
/// Sets the websocket max_message_size configuration.
134+
#[inline]
128135
pub fn max_message_size(mut self, max_message_size: usize) -> Self {
129136
self.config.max_message_size = Some(max_message_size);
130137
self
131138
}
132139

133140
/// Sets the websocket accept_unmasked_frames configuration.
141+
#[inline]
134142
pub fn accept_unmasked_frames(mut self, accept_unmasked_frames: bool) -> Self {
135143
self.config.accept_unmasked_frames = accept_unmasked_frames;
136144
self
@@ -145,12 +153,14 @@ impl WebSocketRequestBuilder {
145153
/// # Returns
146154
///
147155
/// * `Self` - The modified instance with the HTTP version set to HTTP/2.
156+
#[inline]
148157
pub fn use_http2(mut self) -> Self {
149158
self.inner = self.inner.version(Version::HTTP_2);
150159
self
151160
}
152161

153162
/// Add a `Header` to this Request.
163+
#[inline]
154164
pub fn header<K, V>(mut self, key: K, value: V) -> Self
155165
where
156166
HeaderName: TryFrom<K>,
@@ -163,6 +173,7 @@ impl WebSocketRequestBuilder {
163173
}
164174

165175
/// Add a `Header` to append to the request.
176+
#[inline]
166177
pub fn header_append<K, V>(mut self, key: K, value: V) -> Self
167178
where
168179
HeaderName: TryFrom<K>,
@@ -177,18 +188,21 @@ impl WebSocketRequestBuilder {
177188
/// Add a set of Headers to the existing ones on this Request.
178189
///
179190
/// The headers will be merged in to any already set.
191+
#[inline]
180192
pub fn headers(mut self, headers: HeaderMap) -> Self {
181193
self.inner = self.inner.headers(headers);
182194
self
183195
}
184196

185197
/// Set the original headers for this request.
198+
#[inline]
186199
pub fn original_headers(mut self, original_headers: OriginalHeaders) -> Self {
187200
self.inner = self.inner.original_headers(original_headers);
188201
self
189202
}
190203

191204
/// Enable HTTP authentication.
205+
#[inline]
192206
pub fn auth<V>(mut self, value: V) -> Self
193207
where
194208
HeaderValue: TryFrom<V>,
@@ -199,6 +213,7 @@ impl WebSocketRequestBuilder {
199213
}
200214

201215
/// Enable HTTP basic authentication.
216+
#[inline]
202217
pub fn basic_auth<U, P>(mut self, username: U, password: Option<P>) -> Self
203218
where
204219
U: fmt::Display,
@@ -209,6 +224,7 @@ impl WebSocketRequestBuilder {
209224
}
210225

211226
/// Enable HTTP bearer authentication.
227+
#[inline]
212228
pub fn bearer_auth<T>(mut self, token: T) -> Self
213229
where
214230
T: fmt::Display,
@@ -218,18 +234,21 @@ impl WebSocketRequestBuilder {
218234
}
219235

220236
/// Modify the query string of the URL.
237+
#[inline]
221238
pub fn query<T: Serialize + ?Sized>(mut self, query: &T) -> Self {
222239
self.inner = self.inner.query(query);
223240
self
224241
}
225242

226243
/// Set the proxy for this request.
244+
#[inline]
227245
pub fn proxy(mut self, proxy: Proxy) -> Self {
228246
self.inner = self.inner.proxy(proxy);
229247
self
230248
}
231249

232250
/// Set the local address for this request.
251+
#[inline]
233252
pub fn local_address<V>(mut self, local_address: V) -> Self
234253
where
235254
V: Into<Option<IpAddr>>,
@@ -239,6 +258,7 @@ impl WebSocketRequestBuilder {
239258
}
240259

241260
/// Set the local addresses for this request.
261+
#[inline]
242262
pub fn local_addresses<V4, V6>(mut self, ipv4: V4, ipv6: V6) -> Self
243263
where
244264
V4: Into<Option<Ipv4Addr>>,
@@ -261,6 +281,7 @@ impl WebSocketRequestBuilder {
261281
target_os = "visionos",
262282
target_os = "watchos",
263283
))]
284+
#[inline]
264285
pub fn interface<I>(mut self, interface: I) -> Self
265286
where
266287
I: Into<std::borrow::Cow<'static, str>>,
@@ -275,6 +296,7 @@ impl WebSocketRequestBuilder {
275296
/// to use the specified HTTP context. It allows the client to mimic the behavior of different
276297
/// versions or setups, which can be useful for testing or ensuring compatibility with various
277298
/// environments.
299+
#[inline]
278300
pub fn emulation<P>(mut self, factory: P) -> RequestBuilder
279301
where
280302
P: EmulationProviderFactory,
@@ -548,11 +570,13 @@ impl WebSocket {
548570
/// Receive another message.
549571
///
550572
/// Returns `None` if the stream has closed.
573+
#[inline]
551574
pub async fn recv(&mut self) -> Option<Result<Message, Error>> {
552575
self.next().await
553576
}
554577

555578
/// Send a message.
579+
#[inline]
556580
pub async fn send(&mut self, msg: Message) -> Result<(), Error> {
557581
self.inner
558582
.send(msg.into_tungstenite())
@@ -561,6 +585,7 @@ impl WebSocket {
561585
}
562586

563587
/// Return the selected WebSocket subprotocol, if one has been chosen.
588+
#[inline]
564589
pub fn protocol(&self) -> Option<&HeaderValue> {
565590
self.protocol.as_ref()
566591
}
@@ -601,28 +626,28 @@ impl Stream for WebSocket {
601626
impl Sink<Message> for WebSocket {
602627
type Error = Error;
603628

604-
#[inline(always)]
629+
#[inline]
605630
fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
606631
Pin::new(&mut self.inner)
607632
.poll_ready(cx)
608633
.map_err(Error::upgrade)
609634
}
610635

611-
#[inline(always)]
636+
#[inline]
612637
fn start_send(mut self: Pin<&mut Self>, item: Message) -> Result<(), Self::Error> {
613638
Pin::new(&mut self.inner)
614639
.start_send(item.into_tungstenite())
615640
.map_err(Error::upgrade)
616641
}
617642

618-
#[inline(always)]
643+
#[inline]
619644
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
620645
Pin::new(&mut self.inner)
621646
.poll_flush(cx)
622647
.map_err(Error::upgrade)
623648
}
624649

625-
#[inline(always)]
650+
#[inline]
626651
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
627652
Pin::new(&mut self.inner)
628653
.poll_close(cx)

0 commit comments

Comments
 (0)