History

Remove `kstring` in favor of `BString` (#1460)

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 ```

Sebastian Thiel committed 2 years ago

Commit 3cb216e1 parent a807dd17 files · +44 −22Browse files

Unified · Split

7 files
  1. gix-attributes/src/lib.rs +2 −1
  2. gix-attributes/src/name.rs +1 −2
  3. gix-attributes/src/parse.rs +1 −2
  4. gix-attributes/src/search/mod.rs +1 −2
  5. gix-attributes/src/state.rs +9 −14
  6. gix-attributes/tests/parse/mod.rs +29 −0
  7. gix-attributes/tests/search/mod.rs +1 −1
gix-attributes/src/lib.rs+2−1
@@ -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
11pub use gix_glob as glob;12pub use gix_glob as glob;
12use kstring::{KString, KStringRef};13use kstring::{KString, KStringRef};
gix-attributes/src/name.rs+1−2
@@ -1,8 +1,7 @@
1use crate::{Name, NameRef};
1use bstr::{BStr, BString, ByteSlice};2use bstr::{BStr, BString, ByteSlice};
2use kstring::KStringRef;3use kstring::KStringRef;
3 4
4use crate::{Name, NameRef};
5
6impl<'a> NameRef<'a> {5impl<'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 {
gix-attributes/src/parse.rs+1−2
@@ -1,10 +1,9 @@
1use std::borrow::Cow;1use std::borrow::Cow;
2 2
3use crate::{name, AssignmentRef, Name, NameRef, StateRef};
3use bstr::{BStr, ByteSlice};4use bstr::{BStr, ByteSlice};
4use kstring::KStringRef;5use kstring::KStringRef;
5 6
6use 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))]
gix-attributes/src/search/mod.rs+1−2
@@ -1,7 +1,6 @@
1use std::collections::HashMap;
2
3use kstring::KString;1use kstring::KString;
4use smallvec::SmallVec;2use smallvec::SmallVec;
3use std::collections::HashMap;
5 4
6use crate::{Assignment, AssignmentRef};5use crate::{Assignment, AssignmentRef};
7 6
gix-attributes/src/state.rs+9−14
@@ -1,29 +1,24 @@
1use bstr::{BStr, ByteSlice};
2use kstring::{KString, KStringRef};
3
4use crate::{State, StateRef};1use crate::{State, StateRef};
2use 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))]
9pub 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?
10pub 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))]
14pub struct ValueRef<'a>(#[cfg_attr(feature = "serde", serde(borrow))] KStringRef<'a>);15pub struct ValueRef<'a>(#[cfg_attr(feature = "serde", serde(borrow))] &'a [u8]);
15 16
16/// Lifecycle17/// Lifecycle
17impl<'a> ValueRef<'a> {18impl<'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
43impl<'a> From<&'a str> for ValueRef<'a> {38impl<'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
55impl From<&str> for Value {50impl 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
gix-attributes/tests/parse/mod.rs+29−0
@@ -1,4 +1,5 @@
1use bstr::BString;1use bstr::BString;
2use gix_attributes::state::ValueRef;
2use gix_attributes::{parse, StateRef};3use gix_attributes::{parse, StateRef};
3use gix_glob::pattern::Mode;4use gix_glob::pattern::Mode;
4use gix_testtools::fixture_bytes;5use gix_testtools::fixture_bytes;
@@ -275,6 +276,19 @@ fn attributes_can_have_values() { · 270 unmodified lines
275 );276 );
276}277}
277 278
279#[test]
280fn 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]
279fn attributes_see_state_adjustments_over_value_assignments() {293fn 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
342fn byte_value<'b>(attr: &str, value: &'b [u8]) -> (BString, StateRef<'b>) {
343 (attr.into(), StateRef::Value(ValueRef::from_bytes(value)))
344}
345
328fn pattern(name: &str, flags: gix_glob::pattern::Mode, first_wildcard_pos: Option<usize>) -> parse::Kind {346fn 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
365fn byte_line(input: &[u8]) -> ExpandedAttribute {
366 try_byte_line(input).unwrap()
367}
368
369fn 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
347fn lenient_lines(input: &str) -> Vec<ExpandedAttribute> {376fn 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)
gix-attributes/tests/search/mod.rs+1−1
@@ -270,7 +270,7 @@ fn given_attributes_are_made_available_in_given_order() -> crate::Result { · 269 unmodified lines
270fn size_of_outcome() {270fn 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}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t