Skip to content

Conversation

@notoriaga
Copy link
Contributor

@notoriaga notoriaga commented Sep 17, 2021

Apologies in advance for the massive diff. Most of it is renaming/moving things around. The bulk of new changes are in wire_format.rs and sbp_string.rs.

Breaking API changes:

Additions:

  • Adds preliminary support for the new string encodings.
  • Adds support for fixed size arrays (similar to what @jbangelo had here [WIP] Take advantage of const generics in the Rust parser #903). Haven't seen much of a perf change, but maybe it might help with the fragmentation issues @silverjam mentioned. Also feels more semantically correct to say that something like a velocity vector is an [f64; 3] not a Vec<f64>
  • Doc comments/some doc tests

Internal improvements:

  • Fixes generated docs to escape braces/wrap links in <link>
  • Rejiggers the auto generated tests to produce a single test binary rather than one for each
  • Removes all but one of the #[allow(..)] statements in the generated code by tweaking the code gen
  • Removes nom/thiserror/byteorder. Without any features enabled we dropped from 46 deps to 10, debug builds drop from ~22 -> ~9 seconds and release builds from ~43 -> ~11 seconds. It turns out bytes has all we need for parsing (mainly an easy way to pull LE bytes out of a buffer).
  • Removes SbpSerialize trait and replaces it with WireFormat (not sure this is the best name). It's very similar to SbpSerialize but also includes functions for reading the types. Unlike SbpSerialize this trait is not public. Well it's technically public but unnameable by downstream crates (https://rust-lang.github.io/api-guidelines/future-proofing.html). This was done because it's really an implementation detail. Plus, because WireFormat is a super trait of SbpMessage, it makes it impossible for downstream crates to implement SbpMessage which is probably a good idea.

Future things:

  • Expand on the SbpString stuff. Should add validation when parsing/writing that the string satisfies the format. Could also add functions to iterate over the parts of a multipart string for example.
  • When the docs refer to other messages like we could replace the names with links to the actual types.

All in all a lot of changes. Happy to undo any of them that people don't like/think are unnecessary

@@ -0,0 +1,210 @@
//! Functionality for parsing/writing SBP message payloads with in memory buffers
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Some actual new-ish code. A combination of SbpSerialize and all the free standing parse_<type>() functions

}
}

fn parse_unchecked(buf: &mut BytesMut) -> Self {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably a premature optimization. But, this is pretty much the example right out of the docs for MaybeUninit so I don't feel too bad about this touch of unsafe

@@ -0,0 +1,229 @@
use std::fmt;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The other chunk of actually new stuff

@notoriaga notoriaga requested review from jbangelo, john-michaelburke and silverjam and removed request for jbangelo and john-michaelburke September 27, 2021 19:28
@silverjam
Copy link
Contributor

* Adds support for fixed size arrays (similar to what @jbangelo had here [[WIP] Take advantage of const generics in the Rust parser #903](https://github.com/swift-nav/libsbp/pull/903)). Haven't seen much of a perf change, but maybe it might help with the fragmentation issues @silverjam mentioned. Also feels more semantically correct to say that something like a velocity vector is an `[f64; 3]` not a `Vec<f64>`

Semantics look a lot better, but I think we'd need further changes for this to have a real impact on (potential) heap fragmentation issues. "Further changes" would probably mean the ability to allocate the whole SBP message struct on the stack with variable sized arrays backed by an "upper maximum" static array (similar to the new v4 libsbp API for C).

I tried to make a change to ensure that we're allocating consistently size buffers here: 64cafc3 to make things easier for heap reclamation -- but it did not have any impact on the specific console issue (which probably means it's just a leak, not a fragmentation issue).

Copy link
Contributor

@silverjam silverjam left a comment

Choose a reason for hiding this comment

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

New code looks good

Copy link
Contributor

@jbangelo jbangelo left a comment

Choose a reason for hiding this comment

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

Changes look good to me.

use self::unknown::Unknown;

use crate::serialize::SbpSerialize;
mod lib {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be called a prelude? Or is that term just for common imports from an external crate?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm yeah I just took the idea from serde_json where they call it lib - https://docs.serde.rs/src/serde_json/lib.rs.html#366
Although they are doing it for a slightly different reason (still internal though)

pub trait ConcreteMessage: SbpMessage + std::convert::TryFrom<Sbp, Error = TryFromSbpError> + Clone + Sized {
/// Implemented by messages who's message name and type are known at compile time.
/// This is everything that implements [SbpMessage] except for [Sbp].
pub trait ConcreteMessage: SbpMessage + TryFrom<Sbp, Error = TryFromSbpError> {
Copy link
Contributor

Choose a reason for hiding this comment

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

If ConcreteMessage is only for use in the sbp crate, should this be marked as pub(crate)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm it can't be marked private because it a appears in Event which needs to be public. Could maybe add #[doc(hidden)]. Depends on if we think having access to message names/ids at compile time is useful. Right now this is the only way to get that

_ => panic!("Invalid message type! Expected a (((t.msg.name)))"),
};
let frame = sbp_msg.to_frame().unwrap();
let frame = sbp::to_vec(&sbp_msg).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

nit. What is the idiomatic rule of thumb for using to_asdf vs as_asdf? Is it related to the cost of the action such as a string to slice using "as" vs a vector to string use "to"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah pretty much - as_ should be cheap and not consume the original. to_ can be more expensive and still does not consume the original. Then into_ can be either I guess but it should consume the original value. So in this case I went with to_ because it allocates

s = s.replace(url, "<" + url + ">")
return s

def escape_braces(s):
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe for a future addition, it would be neat to see if we could somehow get python docstrings embedded into the rust generated docs seeing as how these are related to the crate generation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah kind of like how the top of each message file tells you which yaml file it came from

@notoriaga notoriaga merged commit 9b7ad95 into master Sep 28, 2021
@notoriaga notoriaga deleted the steve/rust-release branch September 28, 2021 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants