Let's not trade safety for some convenience. This reduces performance by 5% at least (in the WebKit repository at 886077e077a496a6e398df52a4b7915d8cd68f76): ``` ❯ hyperfine "/Users/byron/dev/github.com/Byron/gitoxide/target/release/gix index entries ':(attr:export-ignore)' -i" "gix index entries ':(attr:export-ignore)' -i" Benchmark 1: /Users/byron/dev/github.com/Byron/gitoxide/target/release/gix index entries ':(attr:export-ignore)' -i Time (mean ± σ): 820.1 ms ± 71.7 ms [User: 759.9 ms, System: 85.9 ms] Range (min … max): 778.8 ms … 984.3 ms 10 runs Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options. Benchmark 2: gix index entries ':(attr:export-ignore)' -i Time (mean ± σ): 782.1 ms ± 1.8 ms [User: 760.1 ms, System: 43.2 ms] Range (min … max): 780.0 ms … 786.0 ms 10 runs Summary gix index entries ':(attr:export-ignore)' -i ran 1.05 ± 0.09 times faster than /Users/byron/dev/github.com/Byron/gitoxide/target/release/gix index entries ':(attr:export-ignore)' -i ```
Unified · Split
| @@ -6,7 +6,8 @@ · 5 unmodified lines | |||
| 6 | doc = ::document_features::document_features!() | 6 | doc = ::document_features::document_features!() |
| 7 | )] | 7 | )] |
| 8 | #![cfg_attr(all(doc, feature = "document-features"), feature(doc_cfg, doc_auto_cfg))] | 8 | #![cfg_attr(all(doc, feature = "document-features"), feature(doc_cfg, doc_auto_cfg))] |
| 9 | #![deny(missing_docs, rust_2018_idioms, unsafe_code)] | 9 | #![deny(missing_docs, rust_2018_idioms)] |
| 10 | #![forbid(unsafe_code)] | ||
| 10 | 11 | ||
| 11 | pub use gix_glob as glob; | 12 | pub use gix_glob as glob; |
| 12 | use kstring::{KString, KStringRef}; | 13 | use kstring::{KString, KStringRef}; |
| @@ -1,8 +1,7 @@ | |||
| 1 | use crate::{Name, NameRef}; | ||
| 1 | use bstr::{BStr, BString, ByteSlice}; | 2 | use bstr::{BStr, BString, ByteSlice}; |
| 2 | use kstring::KStringRef; | 3 | use kstring::KStringRef; |
| 3 | 4 | ||
| 4 | use crate::{Name, NameRef}; | ||
| 5 | |||
| 6 | impl<'a> NameRef<'a> { | 5 | impl<'a> NameRef<'a> { |
| 7 | /// Turn this ref into its owned counterpart. | 6 | /// Turn this ref into its owned counterpart. |
| 8 | pub fn to_owned(self) -> Name { | 7 | pub fn to_owned(self) -> Name { |
| @@ -1,10 +1,9 @@ | |||
| 1 | use std::borrow::Cow; | 1 | use std::borrow::Cow; |
| 2 | 2 | ||
| 3 | use crate::{name, AssignmentRef, Name, NameRef, StateRef}; | ||
| 3 | use bstr::{BStr, ByteSlice}; | 4 | use bstr::{BStr, ByteSlice}; |
| 4 | use kstring::KStringRef; | 5 | use kstring::KStringRef; |
| 5 | 6 | ||
| 6 | use crate::{name, AssignmentRef, Name, NameRef, StateRef}; | ||
| 7 | |||
| 8 | /// The kind of attribute that was parsed. | 7 | /// The kind of attribute that was parsed. |
| 9 | #[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)] | 8 | #[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)] |
| 10 | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | 9 | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
| @@ -1,7 +1,6 @@ | |||
| 1 | use std::collections::HashMap; | ||
| 2 | |||
| 3 | use kstring::KString; | 1 | use kstring::KString; |
| 4 | use smallvec::SmallVec; | 2 | use smallvec::SmallVec; |
| 3 | use std::collections::HashMap; | ||
| 5 | 4 | ||
| 6 | use crate::{Assignment, AssignmentRef}; | 5 | use crate::{Assignment, AssignmentRef}; |
| 7 | 6 | ||
| @@ -1,29 +1,24 @@ | |||
| 1 | use bstr::{BStr, ByteSlice}; | ||
| 2 | use kstring::{KString, KStringRef}; | ||
| 3 | |||
| 4 | use crate::{State, StateRef}; | 1 | use crate::{State, StateRef}; |
| 2 | use bstr::{BStr, BString, ByteSlice}; | ||
| 5 | 3 | ||
| 6 | /// A container to encapsulate a tightly packed and typically unallocated byte value that isn't necessarily UTF8 encoded. | 4 | /// A container to encapsulate a tightly packed and typically unallocated byte value that isn't necessarily UTF8 encoded. |
| 7 | #[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)] | 5 | #[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)] |
| 8 | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | 6 | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
| 9 | pub struct Value(KString); | 7 | // TODO: This should be some sort of 'smallbstring' - but can't use `kstring` here due to UTF8 requirement. 5% performance boost possible. |
| 8 | // What's really needed here is a representation that displays as string when serialized which helps with JSON. | ||
| 9 | // Maybe `smallvec` with display and serialization wrapper would do the trick? | ||
| 10 | pub struct Value(BString); | ||
| 10 | 11 | ||
| 11 | /// A reference container to encapsulate a tightly packed and typically unallocated byte value that isn't necessarily UTF8 encoded. | 12 | /// A reference container to encapsulate a tightly packed and typically unallocated byte value that isn't necessarily UTF8 encoded. |
| 12 | #[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)] | 13 | #[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)] |
| 13 | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | 14 | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
| 14 | pub struct ValueRef<'a>(#[cfg_attr(feature = "serde", serde(borrow))] KStringRef<'a>); | 15 | pub struct ValueRef<'a>(#[cfg_attr(feature = "serde", serde(borrow))] &'a [u8]); |
| 15 | 16 | ||
| 16 | /// Lifecycle | 17 | /// Lifecycle |
| 17 | impl<'a> ValueRef<'a> { | 18 | impl<'a> ValueRef<'a> { |
| 18 | /// Keep `input` as our value. | 19 | /// Keep `input` as our value. |
| 19 | pub fn from_bytes(input: &'a [u8]) -> Self { | 20 | pub fn from_bytes(input: &'a [u8]) -> Self { |
| 20 | Self(KStringRef::from_ref( | 21 | Self(input) |
| 21 | // SAFETY: our API makes accessing that value as `str` impossible, so illformed UTF8 is never exposed as such. | ||
| 22 | #[allow(unsafe_code)] | ||
| 23 | unsafe { | ||
| 24 | std::str::from_utf8_unchecked(input) | ||
| 25 | }, | ||
| 26 | )) | ||
| 27 | } | 22 | } |
| 28 | } | 23 | } |
| 29 | 24 | ||
| @@ -42,7 +37,7 @@ impl ValueRef<'_> { · 12 unmodified lines | |||
| 42 | 37 | ||
| 43 | impl<'a> From<&'a str> for ValueRef<'a> { | 38 | impl<'a> From<&'a str> for ValueRef<'a> { |
| 44 | fn from(v: &'a str) -> Self { | 39 | fn from(v: &'a str) -> Self { |
| 45 | ValueRef(v.into()) | 40 | ValueRef(v.as_bytes().into()) |
| 46 | } | 41 | } |
| 47 | } | 42 | } |
| 48 | 43 | ||
| @@ -54,7 +49,7 @@ impl<'a> From<ValueRef<'a>> for Value { · 5 unmodified lines | |||
| 54 | 49 | ||
| 55 | impl From<&str> for Value { | 50 | impl From<&str> for Value { |
| 56 | fn from(v: &str) -> Self { | 51 | fn from(v: &str) -> Self { |
| 57 | Value(KString::from_ref(v)) | 52 | Value(v.as_bytes().into()) |
| 58 | } | 53 | } |
| 59 | } | 54 | } |
| 60 | 55 | ||
| @@ -1,4 +1,5 @@ | |||
| 1 | use bstr::BString; | 1 | use bstr::BString; |
| 2 | use gix_attributes::state::ValueRef; | ||
| 2 | use gix_attributes::{parse, StateRef}; | 3 | use gix_attributes::{parse, StateRef}; |
| 3 | use gix_glob::pattern::Mode; | 4 | use gix_glob::pattern::Mode; |
| 4 | use gix_testtools::fixture_bytes; | 5 | use gix_testtools::fixture_bytes; |
| @@ -275,6 +276,19 @@ fn attributes_can_have_values() { · 270 unmodified lines | |||
| 275 | ); | 276 | ); |
| 276 | } | 277 | } |
| 277 | 278 | ||
| 279 | #[test] | ||
| 280 | fn attributes_can_have_illformed_utf8() { | ||
| 281 | assert_eq!( | ||
| 282 | byte_line(b"p a=one b=\xC3\x28\x41 c=d "), | ||
| 283 | ( | ||
| 284 | pattern("p", Mode::NO_SUB_DIR, None), | ||
| 285 | vec![value("a", "one"), byte_value("b", b"\xC3\x28\x41"), value("c", "d")], | ||
| 286 | 1 | ||
| 287 | ), | ||
| 288 | "illformed UTF8 is fully supported" | ||
| 289 | ); | ||
| 290 | } | ||
| 291 | |||
| 278 | #[test] | 292 | #[test] |
| 279 | fn attributes_see_state_adjustments_over_value_assignments() { | 293 | fn attributes_see_state_adjustments_over_value_assignments() { |
| 280 | assert_eq!( | 294 | assert_eq!( |
| @@ -325,6 +339,10 @@ fn value<'b>(attr: &str, value: &'b str) -> (BString, StateRef<'b>) { · 44 unmodified lines | |||
| 325 | (attr.into(), StateRef::Value(value.into())) | 339 | (attr.into(), StateRef::Value(value.into())) |
| 326 | } | 340 | } |
| 327 | 341 | ||
| 342 | fn byte_value<'b>(attr: &str, value: &'b [u8]) -> (BString, StateRef<'b>) { | ||
| 343 | (attr.into(), StateRef::Value(ValueRef::from_bytes(value))) | ||
| 344 | } | ||
| 345 | |||
| 328 | fn pattern(name: &str, flags: gix_glob::pattern::Mode, first_wildcard_pos: Option<usize>) -> parse::Kind { | 346 | fn pattern(name: &str, flags: gix_glob::pattern::Mode, first_wildcard_pos: Option<usize>) -> parse::Kind { |
| 329 | parse::Kind::Pattern(gix_glob::Pattern { | 347 | parse::Kind::Pattern(gix_glob::Pattern { |
| 330 | text: name.into(), | 348 | text: name.into(), |
| @@ -344,6 +362,17 @@ fn line(input: &str) -> ExpandedAttribute { · 13 unmodified lines | |||
| 344 | try_line(input).unwrap() | 362 | try_line(input).unwrap() |
| 345 | } | 363 | } |
| 346 | 364 | ||
| 365 | fn byte_line(input: &[u8]) -> ExpandedAttribute { | ||
| 366 | try_byte_line(input).unwrap() | ||
| 367 | } | ||
| 368 | |||
| 369 | fn try_byte_line(input: &[u8]) -> Result<ExpandedAttribute, parse::Error> { | ||
| 370 | let mut lines = gix_attributes::parse(input); | ||
| 371 | let res = expand(lines.next().unwrap())?; | ||
| 372 | assert!(lines.next().is_none(), "expected only one line"); | ||
| 373 | Ok(res) | ||
| 374 | } | ||
| 375 | |||
| 347 | fn lenient_lines(input: &str) -> Vec<ExpandedAttribute> { | 376 | fn lenient_lines(input: &str) -> Vec<ExpandedAttribute> { |
| 348 | gix_attributes::parse(input.as_bytes()) | 377 | gix_attributes::parse(input.as_bytes()) |
| 349 | .map(expand) | 378 | .map(expand) |
| @@ -270,7 +270,7 @@ fn given_attributes_are_made_available_in_given_order() -> crate::Result { · 269 unmodified lines | |||
| 270 | fn size_of_outcome() { | 270 | fn size_of_outcome() { |
| 271 | assert_eq!( | 271 | assert_eq!( |
| 272 | std::mem::size_of::<Outcome>(), | 272 | std::mem::size_of::<Outcome>(), |
| 273 | 904, | 273 | 840, |
| 274 | "it's quite big, shouldn't change without us noticing" | 274 | "it's quite big, shouldn't change without us noticing" |
| 275 | ) | 275 | ) |
| 276 | } | 276 | } |