Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pb-rs): --skip-write #207

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion pb-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ edition = "2018"
nom = "7"
log = "0.4.4"
clap = { version = "2.33.1", optional = true }
env_logger = { version = "0.7.1", optional = true }
env_logger = { version = "0.9.0", optional = true }

[dev-dependencies]
walkdir = "2.3.1"
Expand Down
3 changes: 3 additions & 0 deletions pb-rs/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pub enum Error {
Io(io::Error),
/// Nom Error
Nom(nom::Err<nom::error::Error<String>>),
/// Nom's other failure case; giving up in the middle of a file
TrailingGarbage(String),
/// No .proto file provided
NoProto,
/// Cannot read input file
Expand Down Expand Up @@ -59,6 +61,7 @@ impl std::fmt::Display for Error {
match self {
Error::Io(e) => write!(f, "{}", e),
Error::Nom(e) => write!(f, "{}", e),
Error::TrailingGarbage(s) => write!(f, "parsing abandoned near: {:?}", s),
Error::NoProto => write!(f, "No .proto file provided"),
Error::InputFile(file) => write!(f, "Cannot read input file '{}'", file),
Error::OutputFile(file) => write!(f, "Cannot read output file '{}'", file),
Expand Down
8 changes: 8 additions & 0 deletions pb-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub struct ConfigBuilder {
owned: bool,
nostd: bool,
hashbrown: bool,
gen_write: bool,
gen_info: bool,
add_deprecated_fields: bool,
}
Expand Down Expand Up @@ -183,6 +184,12 @@ impl ConfigBuilder {
self
}

/// Generate `MessageWrite` implementations
pub fn gen_write(mut self, val: bool) -> Self {
self.gen_write = val;
self
}

/// Generate `MessageInfo` implementations
pub fn gen_info(mut self, val: bool) -> Self {
self.gen_info = val;
Expand Down Expand Up @@ -226,6 +233,7 @@ impl ConfigBuilder {
owned: self.owned,
nostd: self.nostd,
hashbrown: self.hashbrown,
gen_write: self.gen_write,
gen_info: self.gen_info,
add_deprecated_fields: self.add_deprecated_fields,
}
Expand Down
6 changes: 6 additions & 0 deletions pb-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ fn run() -> Result<(), Error> {
.long("hashrown")
.required(false)
.help("Use hashrown for HashMap implementation"),
).arg(
Arg::with_name("SKIP_WRITE")
.long("skip-write")
.required(false)
.help("Skip MessageWrite implementations")
).arg(
Arg::with_name("GEN_INFO")
.long("gen-info")
Expand Down Expand Up @@ -134,6 +139,7 @@ fn run() -> Result<(), Error> {
.custom_struct_derive(custom_struct_derive)
.nostd(matches.is_present("NOSTD"))
.hashbrown(matches.is_present("HASHBROWN"))
.gen_write(!matches.is_present("SKIP_WRITE"))
.gen_info(matches.is_present("GEN_INFO"))
.custom_repr(custom_repr)
.owned(matches.is_present("OWNED"))
Expand Down
54 changes: 49 additions & 5 deletions pb-rs/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use crate::types::{

use nom::{
branch::alt,
bytes::complete::{tag, take_until},
bytes::complete::{tag, take_until, take_while},
character::complete::{alphanumeric1, digit1, hex_digit1, line_ending, multispace1},
combinator::{map, map_res, not, opt, recognize, value},
combinator::{map, map_res, opt, recognize, value},
multi::{many0, many1, separated_list0, separated_list1},
sequence::{delimited, pair, preceded, separated_pair, terminated, tuple},
IResult,
Expand Down Expand Up @@ -60,7 +60,7 @@ fn integer(input: &str) -> IResult<&str, i32> {
fn comment(input: &str) -> IResult<&str, ()> {
value(
(),
tuple((tag("//"), many0(not(line_ending)), opt(line_ending))),
tuple((tag("//"), take_while(|c| !"\n\r".contains(c)), opt(line_ending))),
)(input)
}

Expand Down Expand Up @@ -327,7 +327,7 @@ fn message(input: &str) -> IResult<&str, Message> {
delimited(
tag("{"),
many0(delimited(many0(br), message_event, many0(br))),
tag("}"),
preceded(many0(br), tag("}")),
),
),
opt(pair(many0(br), tag(";"))),
Expand Down Expand Up @@ -370,7 +370,7 @@ fn enumerator(input: &str) -> IResult<&str, Enumerator> {
delimited(pair(tag("enum"), many1(br)), word, many0(br)),
delimited(
pair(tag("{"), many0(br)),
separated_list0(br, enum_field),
separated_list0(many0(br), enum_field),
pair(many0(br), tag("}")),
),
),
Expand Down Expand Up @@ -422,6 +422,18 @@ mod test {

use std::path::Path;

fn assert_complete<T>(parse: nom::IResult<&str, T>) -> T {
let (rem, obj) = parse.expect("valid parse");
assert_eq!(rem, "", "expected no trailing data");
obj
}

fn assert_desc(msg: &str) -> FileDescriptor {
let (rem, obj) = file_descriptor(msg).expect("valid parse");
assert_eq!("", rem, "expected no trailing data");
obj
}

#[test]
fn test_message() {
let msg = r#"message ReferenceData
Expand All @@ -444,6 +456,13 @@ mod test {
}
}

#[test]
fn empty_message() {
let mess = assert_complete(message("message Vec { }"));
assert_eq!("Vec", mess.name);
assert_eq!(0, mess.fields.len());
}

#[test]
fn test_enum() {
let msg = r#"enum PairingStatus {
Expand All @@ -459,6 +478,11 @@ mod test {
}
}

#[test]
fn comments() {
assert_complete(comment("// foo\n"));
}

#[test]
fn test_ignore() {
let msg = r#"option optimize_for = SPEED;"#;
Expand Down Expand Up @@ -487,6 +511,14 @@ mod test {
);
}

#[test]
fn leading_comment() {
let msg = r#"//foo
message Bar {}"#;
let desc = assert_desc(msg);
assert_eq!(1, desc.messages.len());
}

#[test]
fn test_package() {
let msg = r#"
Expand Down Expand Up @@ -587,6 +619,18 @@ mod test {
}
}

#[test]
fn enum_comments() {
let msg = r#"enum Turn {
UP = 1;
// for what?
// for what, you ask?
DOWN = 2;
}"#;
let en = assert_complete(enumerator(msg));
assert_eq!(2, en.fields.len());
}

#[test]
fn test_rpc_service() {
let msg = r#"
Expand Down
11 changes: 9 additions & 2 deletions pb-rs/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,9 @@ impl Message {
writeln!(w)?;
self.write_impl_message_read(w, desc, config)?;
writeln!(w)?;
self.write_impl_message_write(w, desc, config)?;
if config.gen_write {
self.write_impl_message_write(w, desc, config)?;
}

if config.gen_info {
self.write_impl_message_info(w, desc, config)?;
Expand Down Expand Up @@ -1813,6 +1815,7 @@ pub struct Config {
pub owned: bool,
pub nostd: bool,
pub hashbrown: bool,
pub gen_write: bool,
pub gen_info: bool,
pub add_deprecated_fields: bool,
}
Expand Down Expand Up @@ -1926,7 +1929,11 @@ impl FileDescriptor {
/// Opens a proto file, reads it and returns raw parsed data
pub fn read_proto(in_file: &Path, import_search_path: &[PathBuf]) -> Result<FileDescriptor> {
let file = std::fs::read_to_string(in_file)?;
let (_, mut desc) = file_descriptor(&file).map_err(|e| Error::Nom(e))?;
let (rem, mut desc) = file_descriptor(&file).map_err(|e| Error::Nom(e))?;
let rem = rem.trim();
if !rem.is_empty() {
return Err(Error::TrailingGarbage(rem.chars().take(50).collect()));
}
for mut m in &mut desc.messages {
if m.path.as_os_str().is_empty() {
m.path = in_file.to_path_buf();
Expand Down
6 changes: 5 additions & 1 deletion perftest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ edition = "2021"
[dependencies]
protobuf = "2.25.2"
quick-protobuf = { path = "../quick-protobuf" }
rand = "0.5.5"
rand = "0.8"
rand_xoshiro = "0.6"
prost = "0.9.0"
bytes = "1"

Expand All @@ -18,3 +19,6 @@ protobuf-codegen-pure = "2.25.2"
pb-rs = { path = "../pb-rs" }
prost-build = "0.9.0"
[features]

[profile.release]
lto = true
1 change: 1 addition & 0 deletions perftest/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ fn main() {
owned: false,
hashbrown: false,
nostd: false,
gen_write: true,
gen_info: false,
add_deprecated_fields: false,
};
Expand Down
26 changes: 14 additions & 12 deletions perftest/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use rand::{rngs::SmallRng, Rng, SeedableRng};
use std::default::Default;
use std::fmt::Debug;
use std::fs::File;
Expand All @@ -12,6 +11,8 @@ use perftest_data_quick::PerftestData as QuickPerftestData;

use prost::Message as ProstMessage;
use protobuf::Message;
use rand::{Rng, SeedableRng};
use rand_xoshiro::Xoshiro256PlusPlus;
use quick_protobuf::{BytesReader, MessageRead, MessageWrite, Reader, Writer};

mod perftest_data;
Expand All @@ -20,8 +21,9 @@ mod perftest_data_quick {
include!(concat!(env!("OUT_DIR"), "/perftest_data_quick.rs"));
}

const SEED: [u8; 16] = [
const SEED: [u8; 32] = [
10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160,
15, 25, 35, 45, 55, 65, 75, 85, 95, 105, 115, 125, 135, 145, 155, 165,
];

fn measure<R: Debug + PartialEq, F: FnMut() -> R>(iter: u128, mut f: F, check: Option<&R>) -> u128 {
Expand All @@ -41,12 +43,12 @@ impl TestRunner {
fn run_test<M: Clone + Message + Default + PartialEq>(&self, data: &[M]) -> [u128; 4] {
let mut a = [0; 4];

let mut rng = SmallRng::from_seed(SEED);
let mut rng = Xoshiro256PlusPlus::from_seed(SEED);
let mut random_data = Vec::new();

let mut total_size = 0;
while total_size < self.data_size {
let item = data[rng.gen_range(0, data.len())].clone();
let item = data[rng.gen_range(0..data.len())].clone();
total_size += item.compute_size();
random_data.push(item);
}
Expand Down Expand Up @@ -117,12 +119,12 @@ impl TestRunner {
{
let mut b = [0; 4];

let mut rng = SmallRng::from_seed(SEED);
let mut rng = Xoshiro256PlusPlus::from_seed(SEED);
let mut random_data: Vec<M> = Vec::new();

let mut total_size = 0;
while total_size < self.data_size {
let ref item = data[rng.gen_range(0, data.len())];
let ref item = data[rng.gen_range(0..data.len())];
random_data.push(item.clone());
total_size += item.get_size() as u32;
}
Expand Down Expand Up @@ -178,12 +180,12 @@ impl TestRunner {
fn quick_run_test_strings(&self, data: &[perftest_data_quick::TestStrings]) -> [u128; 4] {
let mut b = [0; 4];

let mut rng = SmallRng::from_seed(SEED);
let mut rng = Xoshiro256PlusPlus::from_seed(SEED);
let mut random_data = Vec::new();

let mut total_size = 0;
while total_size < self.data_size {
let ref item = data[rng.gen_range(0, data.len())];
let ref item = data[rng.gen_range(0..data.len())];
random_data.push(item.clone());
total_size += item.get_size() as u32;
}
Expand Down Expand Up @@ -230,12 +232,12 @@ impl TestRunner {
fn quick_run_test_bytes(&self, data: &[perftest_data_quick::TestBytes]) -> [u128; 4] {
let mut b = [0; 4];

let mut rng = SmallRng::from_seed(SEED);
let mut rng = Xoshiro256PlusPlus::from_seed(SEED);
let mut random_data = Vec::new();

let mut total_size = 0;
while total_size < self.data_size {
let ref item = data[rng.gen_range(0, data.len())];
let ref item = data[rng.gen_range(0..data.len())];
random_data.push(item.clone());
total_size += item.get_size() as u32;
}
Expand Down Expand Up @@ -285,12 +287,12 @@ impl TestRunner {
) -> [u128; 4] {
let mut c = [0; 4];

let mut rng = SmallRng::from_seed(SEED);
let mut rng = Xoshiro256PlusPlus::from_seed(SEED);
let mut random_data: Vec<M> = Vec::new();

let mut total_size = 0;
while total_size < self.data_size {
let ref item = data[rng.gen_range(0, data.len())];
let ref item = data[rng.gen_range(0..data.len())];
random_data.push(item.clone());
total_size += item.encoded_len() as u32;
}
Expand Down