Skip to content
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
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ members = [
"rust/sbp",
"rust/sbp2json"
]

[profile.release]
lto = true
codegen-units = 1
4 changes: 1 addition & 3 deletions generator/sbpg/targets/resources/sbp2json-cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ features = ["json"]
env_logger = "0.8"
serde_json = "1.0"
structopt = "0.3"
mimalloc = { version = "0.1", default-features = false }

[dependencies.dencode]
version = "0.3.0"
default-features = false

[target.'cfg(all(not(windows), not(target_env = "musl")))'.dependencies]
jemallocator = "0.3"

[dev-dependencies]
sha2 = "0.8"
hex = "0.4"
Expand Down
4 changes: 2 additions & 2 deletions rust/sbp/src/json/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
de::Frame,
json::{Json2JsonInput, JsonError, JsonInput},
messages::Sbp,
MAX_PAYLOAD_LEN,
BUFLEN,
};

/// Deserialize the IO stream into an iterator of messages.
Expand Down Expand Up @@ -40,7 +40,7 @@ struct JsonDecoder {
impl JsonDecoder {
fn new() -> Self {
JsonDecoder {
payload_buf: Vec::with_capacity(MAX_PAYLOAD_LEN),
payload_buf: Vec::with_capacity(BUFLEN),
}
}

Expand Down
24 changes: 12 additions & 12 deletions rust/sbp/src/json/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ use crate::{
de::Frame,
json::{CommonJson, HaskellishFloatFormatter, Json2JsonInput, Json2JsonOutput},
messages::Sbp,
SbpMessage, CRC_LEN, HEADER_LEN, MAX_PAYLOAD_LEN, PREAMBLE,
SbpMessage, BUFLEN, CRC_LEN, HEADER_LEN, PREAMBLE,
};

const BASE64_MAX_PAYLOAD_LEN: usize = MAX_PAYLOAD_LEN / 3 * 4 + 4;
const BASE64_BUFLEN: usize = BUFLEN * 4;

/// Serialize the given message as JSON into the IO stream.
pub fn to_writer<W, M>(mut writer: W, msg: &M) -> Result<(), JsonError>
where
W: io::Write,
M: SbpMessage + Serialize,
{
let mut frame = BytesMut::new();
let mut payload = String::new();
let mut buf = BytesMut::new();
let mut frame = BytesMut::with_capacity(BUFLEN);
let mut payload = String::with_capacity(BUFLEN);
let mut buf = BytesMut::with_capacity(BUFLEN);
to_buffer(
&mut frame,
&mut payload,
Expand All @@ -41,9 +41,9 @@ pub fn to_vec<M>(msg: &M) -> Result<Vec<u8>, JsonError>
where
M: SbpMessage + Serialize,
{
let mut frame = BytesMut::new();
let mut payload = String::new();
let mut buf = BytesMut::new();
let mut frame = BytesMut::with_capacity(BUFLEN);
let mut payload = String::with_capacity(BUFLEN);
let mut buf = BytesMut::with_capacity(BUFLEN);
to_buffer(
&mut frame,
&mut payload,
Expand Down Expand Up @@ -114,8 +114,8 @@ struct JsonEncoderInner<F> {
impl<F: Formatter + Clone> JsonEncoderInner<F> {
fn new(formatter: F) -> Self {
JsonEncoderInner {
frame_buf: BytesMut::with_capacity(MAX_PAYLOAD_LEN),
payload_buf: String::with_capacity(BASE64_MAX_PAYLOAD_LEN),
frame_buf: BytesMut::with_capacity(BUFLEN),
payload_buf: String::with_capacity(BASE64_BUFLEN),
formatter,
}
}
Expand Down Expand Up @@ -181,8 +181,8 @@ struct Json2JsonEncoderInner<F> {
impl<F: Formatter + Clone> Json2JsonEncoderInner<F> {
fn new(formatter: F) -> Self {
Json2JsonEncoderInner {
frame_buf: BytesMut::with_capacity(MAX_PAYLOAD_LEN),
payload_buf: String::with_capacity(BASE64_MAX_PAYLOAD_LEN),
frame_buf: BytesMut::with_capacity(BUFLEN),
payload_buf: String::with_capacity(BASE64_BUFLEN),
formatter,
}
}
Expand Down
3 changes: 3 additions & 0 deletions rust/sbp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ pub const PREAMBLE: u8 = 0x55;
/// Length of the header section.
pub const HEADER_LEN: usize = 1 /*preamble*/ + 2 /*msg_type*/ + 2 /*sender_id*/ + 1 /*len*/;

/// Internal buffer length.
pub(crate) const BUFLEN: usize = 128;

/// Max length of the variable-sized payload field.
pub const MAX_PAYLOAD_LEN: usize = 255;

Expand Down
6 changes: 3 additions & 3 deletions rust/sbp/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use dencode::{Encoder, FramedWrite, IterSinkExt};

use crate::wire_format::WireFormat;
use crate::{Sbp, SbpMessage};
use crate::{MAX_PAYLOAD_LEN, PREAMBLE};
use crate::{BUFLEN, MAX_PAYLOAD_LEN, PREAMBLE};

/// Serialize the given message into the IO stream.
///
Expand Down Expand Up @@ -36,7 +36,7 @@ where
W: io::Write,
M: SbpMessage,
{
let mut buf = BytesMut::with_capacity(msg.len());
let mut buf = BytesMut::with_capacity(BUFLEN);
to_buffer(&mut buf, msg)?;
writer.write_all(&buf)?;
Ok(())
Expand Down Expand Up @@ -65,7 +65,7 @@ where
/// }
/// ```
pub fn to_vec<M: SbpMessage>(msg: &M) -> Result<Vec<u8>, Error> {
let mut buf = BytesMut::with_capacity(msg.len());
let mut buf = BytesMut::with_capacity(BUFLEN);
to_buffer(&mut buf, msg)?;
Ok(buf.to_vec())
}
Expand Down
4 changes: 3 additions & 1 deletion rust/sbp/src/wire_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use std::{

use bytes::{Buf, BufMut};

use crate::BUFLEN;

pub trait WireFormat: Sized {
/// Minimum number of bytes this type will take in the frame.
const MIN_LEN: usize = mem::size_of::<Self>();
Expand Down Expand Up @@ -51,7 +53,7 @@ where
}

fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
let mut v = Vec::new();
let mut v = Vec::with_capacity(BUFLEN);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would something like this work?

let mut v = Vec::with_capacity(BUFLEN / mem::size_of::<T>());

it should over-allocate less often. but I think the buffer sizes would vary more

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this which didn't seem to have any impact:

diff --git a/rust/sbp/src/wire_format.rs b/rust/sbp/src/wire_format.rs
index afdd09fd..3d3639d3 100644
--- a/rust/sbp/src/wire_format.rs
+++ b/rust/sbp/src/wire_format.rs
@@ -53,7 +53,7 @@ where
     }

     fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
-        let mut v = Vec::with_capacity(BUFLEN);
+        let mut v = Vec::with_capacity(64*(1+(mem::size_of::<T>() / 64)));
         while buf.remaining() >= T::MIN_LEN {
             v.push(T::parse_unchecked(buf));
         }

Benchmark:

Benchmark #1: sbp2json <test_data/benchmark.sbp | json2sbp >/dev/null
  Time (mean ± σ):     610.7 ms ±  18.9 ms    [User: 936.4 ms, System: 161.0 ms]
  Range (min … max):   561.0 ms … 684.1 ms    50 runs

Copy link
Contributor Author

@silverjam silverjam Oct 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Smaller internal buffer size of 64 bytes was a bit slower:

Benchmark #1: sbp2json <test_data/benchmark.sbp | json2sbp >/dev/null
  Time (mean ± σ):     629.7 ms ±  18.2 ms    [User: 979.7 ms, System: 151.9 ms]
  Range (min … max):   584.6 ms … 678.3 ms    50 runs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buffer size of 256:

Benchmark #1: sbp2json <test_data/benchmark.sbp | json2sbp >/dev/null
  Time (mean ± σ):     624.5 ms ±  18.7 ms    [User: 962.8 ms, System: 159.4 ms]
  Range (min … max):   587.7 ms … 698.3 ms    50 runs

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm interesting, I guess T is typically small anyway

while buf.remaining() >= T::MIN_LEN {
v.push(T::parse_unchecked(buf));
}
Expand Down
4 changes: 1 addition & 3 deletions rust/sbp2json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ features = ["json"]
env_logger = "0.8"
serde_json = "1.0"
structopt = "0.3"
mimalloc = { version = "0.1", default-features = false }

[dependencies.dencode]
version = "0.3.0"
default-features = false

[target.'cfg(all(not(windows), not(target_env = "musl")))'.dependencies]
jemallocator = "0.3"

[dev-dependencies]
sha2 = "0.8"
hex = "0.4"
Expand Down
3 changes: 1 addition & 2 deletions rust/sbp2json/src/bin/json2json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use structopt::StructOpt;

use converters::{json2json, Result};

#[cfg(all(not(windows), not(target_env = "musl")))]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

/// Convert "compact" SBP JSON data to an "exploded" form
///
Expand Down
3 changes: 1 addition & 2 deletions rust/sbp2json/src/bin/json2sbp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use structopt::StructOpt;

use converters::{json2sbp, Result};

#[cfg(all(not(windows), not(target_env = "musl")))]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

/// Convert SBP JSON data to binary SBP.
///
Expand Down
3 changes: 1 addition & 2 deletions rust/sbp2json/src/bin/sbp2json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use structopt::StructOpt;

use converters::{sbp2json, Result};

#[cfg(all(not(windows), not(target_env = "musl")))]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

/// Convert binary SBP data to JSON.
///
Expand Down
8 changes: 4 additions & 4 deletions test_data/benchmark_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@

# How much faster Rust should be than other implementations
RATIOS_SBP2JSON = {
"haskell": 2.06,
"python": 26.0,
"haskell": 2.19,
"python": 17.72,
}

RATIOS_JSON2SBP = {
"haskell": 2.18,
"haskell": 2.75,
}

RATIOS_JSON2JSON = {
"haskell": 2.68,
"haskell": 3.45,
}

FAILED = [False]
Expand Down