Skip to content

Commit 06a22ab

Browse files
authored
fix: optimize bytes write action (#24)
1 parent 396653f commit 06a22ab

File tree

12 files changed

+49
-46
lines changed

12 files changed

+49
-46
lines changed

rsocket/src/extension/composite.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,13 @@ impl Writeable for CompositeMetadataEntry {
191191
let mime_type_len = s.len() as u8;
192192
assert!(mime_type_len > 0, "invalid length of MimeType!");
193193
bf.put_u8(mime_type_len - 1);
194-
bf.put_slice(s.as_ref());
194+
bf.extend_from_slice(s.as_ref());
195195
}
196196
};
197197
let metadata_len = self.metadata.len();
198198
u24::from(metadata_len).write_to(bf);
199199
if metadata_len > 0 {
200-
bf.put(self.metadata.bytes());
200+
bf.extend_from_slice(&self.metadata);
201201
}
202202
}
203203

rsocket/src/frame/error.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use super::utils::too_short;
21
use super::{Body, Frame};
2+
use crate::error::RSocketError;
33
use crate::utils::Writeable;
44
use bytes::{Buf, BufMut, Bytes, BytesMut};
55
use std::fmt;
@@ -46,11 +46,11 @@ impl ErrorBuilder {
4646
impl Error {
4747
pub(crate) fn decode(flag: u16, bf: &mut BytesMut) -> crate::Result<Error> {
4848
if bf.len() < 4 {
49-
too_short(4)
49+
Err(RSocketError::InCompleteFrame.into())
5050
} else {
5151
let code = bf.get_u32();
5252
let data: Option<Bytes> = if !bf.is_empty() {
53-
Some(Bytes::from(bf.to_vec()))
53+
Some(bf.split().freeze())
5454
} else {
5555
None
5656
};
@@ -85,7 +85,7 @@ impl Writeable for Error {
8585
fn write_to(&self, bf: &mut BytesMut) {
8686
bf.put_u32(self.code);
8787
match &self.data {
88-
Some(v) => bf.put(v.bytes()),
88+
Some(v) => bf.extend_from_slice(v),
8989
None => (),
9090
}
9191
}

rsocket/src/frame/keepalive.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use super::utils::too_short;
21
use super::{Body, Frame};
2+
use crate::error::RSocketError;
33
use crate::utils::Writeable;
44
use bytes::{Buf, BufMut, Bytes, BytesMut};
55

@@ -45,13 +45,13 @@ impl KeepaliveBuilder {
4545
impl Keepalive {
4646
pub(crate) fn decode(flag: u16, bf: &mut BytesMut) -> crate::Result<Keepalive> {
4747
if bf.len() < 8 {
48-
return too_short(8);
48+
return Err(RSocketError::InCompleteFrame.into());
4949
}
5050
let position = bf.get_u64();
5151
let data = if bf.is_empty() {
5252
None
5353
} else {
54-
Some(Bytes::from(bf.to_vec()))
54+
Some(bf.split().freeze())
5555
};
5656
Ok(Keepalive {
5757
last_received_position: position,
@@ -83,7 +83,7 @@ impl Writeable for Keepalive {
8383
fn write_to(&self, bf: &mut BytesMut) {
8484
bf.put_u64(self.last_received_position);
8585
match &self.data {
86-
Some(v) => bf.put(v.bytes()),
86+
Some(v) => bf.extend_from_slice(v),
8787
None => (),
8888
}
8989
}

rsocket/src/frame/lease.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use super::utils::too_short;
21
use super::{Body, Frame};
2+
use crate::error::RSocketError;
33
use crate::utils::Writeable;
44
use bytes::{Buf, BufMut, Bytes, BytesMut};
55

@@ -53,12 +53,12 @@ impl LeaseBuilder {
5353
impl Lease {
5454
pub(crate) fn decode(flag: u16, bf: &mut BytesMut) -> crate::Result<Lease> {
5555
if bf.len() < 8 {
56-
return too_short(8);
56+
return Err(RSocketError::InCompleteFrame.into());
5757
}
5858
let ttl = bf.get_u32();
5959
let n = bf.get_u32();
6060
let m = if flag & Frame::FLAG_METADATA != 0 {
61-
Some(Bytes::from(bf.to_vec()))
61+
Some(bf.split().freeze())
6262
} else {
6363
None
6464
};
@@ -94,7 +94,7 @@ impl Writeable for Lease {
9494
bf.put_u32(self.ttl);
9595
bf.put_u32(self.number_of_requests);
9696
match &self.metadata {
97-
Some(v) => bf.put(v.bytes()),
97+
Some(v) => bf.extend_from_slice(v),
9898
None => (),
9999
}
100100
}

rsocket/src/frame/metadata_push.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ impl MetadataPushBuiler {
3434

3535
impl MetadataPush {
3636
pub(crate) fn decode(flag: u16, bf: &mut BytesMut) -> crate::Result<MetadataPush> {
37-
let m = Bytes::from(bf.to_vec());
38-
Ok(MetadataPush { metadata: Some(m) })
37+
Ok(MetadataPush {
38+
metadata: Some(bf.split().freeze()),
39+
})
3940
}
4041

4142
pub fn builder(stream_id: u32, flag: u16) -> MetadataPushBuiler {
@@ -57,7 +58,7 @@ impl MetadataPush {
5758
impl Writeable for MetadataPush {
5859
fn write_to(&self, bf: &mut BytesMut) {
5960
match &self.metadata {
60-
Some(v) => bf.put(v.bytes()),
61+
Some(v) => bf.extend_from_slice(v),
6162
None => (),
6263
}
6364
}

rsocket/src/frame/request_channel.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::utils;
22
use super::{Body, Frame, REQUEST_MAX};
3+
use crate::error::RSocketError;
34
use crate::utils::Writeable;
45
use bytes::{Buf, BufMut, Bytes, BytesMut};
56

@@ -68,7 +69,7 @@ impl RequestChannelBuilder {
6869
impl RequestChannel {
6970
pub(crate) fn decode(flag: u16, bf: &mut BytesMut) -> crate::Result<RequestChannel> {
7071
if bf.len() < 4 {
71-
utils::too_short(4)
72+
Err(RSocketError::InCompleteFrame.into())
7273
} else {
7374
let initial_request_n = bf.get_u32();
7475
utils::read_payload(flag, bf).map(move |(metadata, data)| RequestChannel {

rsocket/src/frame/request_n.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::{utils, Body, Frame, REQUEST_MAX};
2+
use crate::error::RSocketError;
23
use crate::utils::Writeable;
34
use bytes::{Buf, BufMut, Bytes, BytesMut};
45

@@ -35,7 +36,7 @@ impl RequestNBuilder {
3536
impl RequestN {
3637
pub(crate) fn decode(flag: u16, bf: &mut BytesMut) -> crate::Result<RequestN> {
3738
if bf.len() < 4 {
38-
utils::too_short(4)
39+
Err(RSocketError::InCompleteFrame.into())
3940
} else {
4041
let n = bf.get_u32();
4142
Ok(RequestN { n })

rsocket/src/frame/request_stream.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::{utils, Body, Frame, REQUEST_MAX};
2+
use crate::error::RSocketError;
23
use crate::utils::Writeable;
34
use bytes::{Buf, BufMut, Bytes, BytesMut};
45

@@ -55,7 +56,7 @@ impl RequestStreamBuilder {
5556
impl RequestStream {
5657
pub(crate) fn decode(flag: u16, bf: &mut BytesMut) -> crate::Result<RequestStream> {
5758
if bf.len() < 4 {
58-
utils::too_short(4)
59+
Err(RSocketError::InCompleteFrame.into())
5960
} else {
6061
let initial_request_n = bf.get_u32();
6162
utils::read_payload(flag, bf).map(move |(metadata, data)| RequestStream {

rsocket/src/frame/resume.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use super::utils::too_short;
21
use super::{Body, Frame, Version};
2+
use crate::error::RSocketError;
33
use crate::utils::Writeable;
44
use bytes::{Buf, BufMut, Bytes, BytesMut};
55

@@ -29,21 +29,21 @@ impl Resume {
2929

3030
pub(crate) fn decode(flag: u16, b: &mut BytesMut) -> crate::Result<Resume> {
3131
if b.len() < 6 {
32-
return too_short(6);
32+
return Err(RSocketError::InCompleteFrame.into());
3333
}
3434
let major = b.get_u16();
3535
let minor = b.get_u16();
3636
let token_size = b.get_u16() as usize;
3737
let token = if token_size > 0 {
3838
if b.len() < token_size {
39-
return too_short(token_size);
39+
return Err(RSocketError::InCompleteFrame.into());
4040
}
4141
Some(b.split_to(token_size).freeze())
4242
} else {
4343
None
4444
};
4545
if b.len() < 16 {
46-
return too_short(16);
46+
return Err(RSocketError::InCompleteFrame.into());
4747
}
4848
let last_received_server_position = b.get_u64();
4949
let first_available_client_position = b.get_u64();
@@ -114,7 +114,7 @@ impl Writeable for Resume {
114114
self.version.write_to(bf);
115115
if let Some(b) = self.get_token() {
116116
bf.put_u16(b.len() as u16);
117-
bf.put(b.bytes());
117+
bf.extend_from_slice(b);
118118
}
119119
bf.put_u64(self.get_last_received_server_position());
120120
bf.put_u64(self.get_first_available_client_position());

rsocket/src/frame/resume_ok.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use super::utils::too_short;
21
use super::{Body, Frame};
2+
use crate::error::RSocketError;
33
use crate::utils::Writeable;
44
use bytes::{Buf, BufMut, Bytes, BytesMut};
55

@@ -35,7 +35,7 @@ impl ResumeOKBuilder {
3535
impl ResumeOK {
3636
pub(crate) fn decode(flag: u16, bf: &mut BytesMut) -> crate::Result<ResumeOK> {
3737
if bf.len() < 8 {
38-
too_short(8)
38+
Err(RSocketError::InCompleteFrame.into())
3939
} else {
4040
Ok(ResumeOK {
4141
position: bf.get_u64(),

0 commit comments

Comments
 (0)