From e2fba6e6f4f1d016e84274bb3a66410fb1534aad Mon Sep 17 00:00:00 2001 From: Sayan Nandan Date: Sun, 2 Aug 2020 22:38:05 +0530 Subject: [PATCH] Elide bound checks --- CONTRIBUTING.md | 1 + Cargo.toml | 7 ++++++- buildandbin.sh | 4 ++-- server/src/protocol/deserializer.rs | 21 +++++++++++++-------- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d0e8eac1..b821a394 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -18,6 +18,7 @@ You can see a list of contributors **[here](./CONTRIBUTORS.md)** * `FIXME(@)` : Use this when you have made an implementation that can be improved in the future, such as improved efficiency * `HACK(@)` : Use this when the code you are using a temporary workaround * `TODO(@)` : Use this when you have kept something incomplete +* `UNSAFE(@` : Use this to explain why the `unsafe` block used is safe ### Formatting diff --git a/Cargo.toml b/Cargo.toml index c3572043..ccd38b09 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,4 +3,9 @@ members = [ "cli", "server", "corelib" -] \ No newline at end of file +] + +[profile.release] +opt-level = 3 +codegen-units = 1 +lto = "fat" diff --git a/buildandbin.sh b/buildandbin.sh index 645f589a..37c48ab9 100755 --- a/buildandbin.sh +++ b/buildandbin.sh @@ -1,5 +1,5 @@ # This is a simple script which creates a release build and # moves the release builds into my $HOME/bin folder cargo build --release -cp -f target/release/tdb target/release/tsh $HOME/bin -echo 'Done!' \ No newline at end of file +cp -f target/release/tdb target/release/tsh target/release/tdb-bench $HOME/bin +echo 'Done!' diff --git a/server/src/protocol/deserializer.rs b/server/src/protocol/deserializer.rs index 32a6783d..13c67ede 100644 --- a/server/src/protocol/deserializer.rs +++ b/server/src/protocol/deserializer.rs @@ -48,7 +48,7 @@ pub struct Navigator<'a> { } impl<'a> Navigator<'a> { /// Create a new `Navigator` instance - pub fn new(buffer: &'a mut BytesMut) -> Self { + pub fn new<'b: 'a>(buffer: &'b BytesMut) -> Self { Navigator { cursor: Cursor::new(&buffer[..]), } @@ -136,17 +136,19 @@ impl Metaline { } // The first byte is always a `*` or `$` depending on the // type of query - let actiontype = match mline[0] { + // UNSAFE(@ohsayan): This is safe because we already know the size + let actiontype = match unsafe { mline.get_unchecked(0) } { b'$' => ActionType::Pipeline, b'*' => ActionType::Simple, _ => return None, }; // Get the frame sizes: the first index is the content size // and the second index is the metalayout size - if let Some(sizes) = get_frame_sizes(&mline[1..]) { + // UNSAFE(@ohsayan): This is safe because we already know the size + if let Some(sizes) = get_frame_sizes(unsafe { &mline.get_unchecked(1..) }) { return Some(Metaline { - content_size: sizes[0], - metalayout_size: sizes[1], + content_size: unsafe { *sizes.get_unchecked(0) }, + metalayout_size: unsafe { *sizes.get_unchecked(1) }, actiontype, }); } @@ -249,19 +251,22 @@ fn extract_sizes_splitoff(buf: &[u8], splitoff: u8, sizehint: usize) -> Option s.into(), None => return None, };