Skip to content

Commit

Permalink
refactor: use heapopt::Vec wherever possible
Browse files Browse the repository at this point in the history
  • Loading branch information
pamburus committed Dec 28, 2024
1 parent e5a3d0f commit 9874a51
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 131 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ enumset-ext = { path = "./crate/enumset-ext" }
env_logger = "0"
flate2 = "1"
heapless = "0"
heapopt = { path = "./crate/heapopt" }
hex = "0"
htp = { git = "https://github.com/pamburus/htp.git" }
humantime = "2"
Expand Down
96 changes: 19 additions & 77 deletions src/fmtx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,65 +24,7 @@ where

// ---

#[derive(Default)]
pub struct OptimizedBuf<T, const N: usize> {
pub head: heapless::Vec<T, N>,
pub tail: Vec<T>,
}

impl<T, const N: usize> OptimizedBuf<T, N>
where
T: Clone,
{
#[inline]
pub fn new() -> Self {
Self {
head: heapless::Vec::new(),
tail: Vec::new(),
}
}

#[inline]
pub fn len(&self) -> usize {
self.head.len() + self.tail.len()
}

#[inline]
pub fn clear(&mut self) {
self.head.clear();
self.tail.clear();
}

#[inline]
pub fn truncate(&mut self, len: usize) {
if len <= self.head.len() {
self.head.truncate(len);
self.tail.clear();
} else {
self.tail.truncate(len - self.head.len());
}
}

#[inline]
pub fn push(&mut self, value: T) {
if self.head.len() < N {
self.head.push(value).ok();
} else {
self.tail.push(value);
}
}

#[inline]
pub fn extend_from_slice(&mut self, values: &[T]) {
if self.head.len() + values.len() <= N {
self.head.extend_from_slice(values).ok();
} else {
let n = N - self.head.len();
self.head.extend_from_slice(&values[..n]).ok();
self.tail.extend_from_slice(&values[n..]);
}
}
}
pub type OptimizedBuf<T, const N: usize> = heapopt::Vec<T, N>;

impl<T, const N: usize> Push<T> for OptimizedBuf<T, N>
where
Expand Down Expand Up @@ -495,8 +437,8 @@ mod tests {
assert_eq!(buf.len(), 4);
buf.push(5);
assert_eq!(buf.len(), 5);
assert_eq!(buf.head.as_slice(), &[1, 2, 3, 4]);
assert_eq!(buf.tail.as_slice(), &[5]);
assert_eq!(buf.as_slices().0, &[1, 2, 3, 4]);
assert_eq!(buf.as_slices().1, &[5]);
}

#[test]
Expand All @@ -511,8 +453,8 @@ mod tests {
assert_eq!(buf.len(), 3);
buf.extend_from_slice(&[4, 5, 6]);
assert_eq!(buf.len(), 6);
assert_eq!(buf.head.as_slice(), &[1, 2, 3, 4]);
assert_eq!(buf.tail.as_slice(), &[5, 6]);
assert_eq!(buf.as_slices().0, &[1, 2, 3, 4]);
assert_eq!(buf.as_slices().1, &[5, 6]);
}

#[test]
Expand All @@ -523,32 +465,32 @@ mod tests {
assert_eq!(buf.len(), 7);
buf.truncate(8);
assert_eq!(buf.len(), 7);
assert_eq!(buf.head.as_slice(), &[1, 2, 3, 4]);
assert_eq!(buf.tail.as_slice(), &[5, 6, 7]);
assert_eq!(buf.as_slices().0, &[1, 2, 3, 4]);
assert_eq!(buf.as_slices().1, &[5, 6, 7]);
buf.truncate(7);
assert_eq!(buf.len(), 7);
assert_eq!(buf.head.as_slice(), &[1, 2, 3, 4]);
assert_eq!(buf.tail.as_slice(), &[5, 6, 7]);
assert_eq!(buf.as_slices().0, &[1, 2, 3, 4]);
assert_eq!(buf.as_slices().1, &[5, 6, 7]);
buf.truncate(6);
assert_eq!(buf.len(), 6);
assert_eq!(buf.head.as_slice(), &[1, 2, 3, 4]);
assert_eq!(buf.tail.as_slice(), &[5, 6]);
assert_eq!(buf.as_slices().0, &[1, 2, 3, 4]);
assert_eq!(buf.as_slices().1, &[5, 6]);
buf.truncate(4);
assert_eq!(buf.len(), 4);
assert_eq!(buf.head.as_slice(), &[1, 2, 3, 4]);
assert_eq!(buf.tail.len(), 0);
assert_eq!(buf.as_slices().0, &[1, 2, 3, 4]);
assert_eq!(buf.as_slices().1.len(), 0);
buf.truncate(4);
buf.extend_from_slice(&[8, 9]);
assert_eq!(buf.len(), 6);
assert_eq!(buf.head.as_slice(), &[1, 2, 3, 4]);
assert_eq!(buf.tail.as_slice(), &[8, 9]);
assert_eq!(buf.as_slices().0, &[1, 2, 3, 4]);
assert_eq!(buf.as_slices().1, &[8, 9]);
buf.truncate(3);
assert_eq!(buf.len(), 3);
assert_eq!(buf.head.as_slice(), &[1, 2, 3]);
assert_eq!(buf.tail.len(), 0);
assert_eq!(buf.as_slices().0, &[1, 2, 3]);
assert_eq!(buf.as_slices().1.len(), 0);
buf.truncate(0);
assert_eq!(buf.len(), 0);
assert_eq!(buf.head.len(), 0);
assert_eq!(buf.tail.len(), 0);
assert_eq!(buf.as_slices().0.len(), 0);
assert_eq!(buf.as_slices().1.len(), 0);
}
}
14 changes: 4 additions & 10 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ impl KeyPrefix {

#[inline]
fn format<B: Push<u8>>(&self, buf: &mut B) {
buf.extend_from_slice(&self.value.head);
buf.extend_from_slice(&self.value.tail);
buf.extend_from_slice(&self.value.as_slices().0);
buf.extend_from_slice(&self.value.as_slices().1);
}

#[inline]
Expand Down Expand Up @@ -985,10 +985,7 @@ mod tests {
impl<'a> RecordExt<'a> for Record<'a> {
fn from_fields(fields: &[(&'a str, RawValue<'a>)]) -> Record<'a> {
Record {
fields: RecordFields {
head: heapless::Vec::from_slice(fields).unwrap(),
..Default::default()
},
fields: RecordFields::from_slice(fields),
..Default::default()
}
}
Expand All @@ -1003,10 +1000,7 @@ mod tests {
level: Some(Level::Debug),
logger: Some("tl"),
caller: Some(Caller::Text("tc")),
fields: RecordFields {
head: heapless::Vec::from_slice(&[("k_a", RawValue::from(RawObject::Json(&ka)))]).unwrap(),
..Default::default()
},
fields: RecordFields::from_slice(&[("k_a", RawValue::from(RawObject::Json(&ka)))]),
..Default::default()
};

Expand Down
55 changes: 11 additions & 44 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ pub struct Record<'a> {
impl<'a> Record<'a> {
#[inline(always)]
pub fn fields(&self) -> impl Iterator<Item = &(&'a str, RawValue<'a>)> {
self.fields.head.iter().chain(self.fields.tail.iter())
self.fields.iter()
}

#[inline(always)]
Expand All @@ -342,24 +342,13 @@ impl<'a> Record<'a> {
level: None,
logger: None,
caller: None,
fields: RecordFields {
head: heapless::Vec::new(),
tail: if capacity > RECORD_EXTRA_CAPACITY {
Vec::with_capacity(capacity - RECORD_EXTRA_CAPACITY)
} else {
Vec::new()
},
},
fields: RecordFields::with_capacity(capacity),
predefined: heapless::Vec::new(),
}
}
}

#[derive(Default)]
pub struct RecordFields<'a> {
pub(crate) head: heapless::Vec<(&'a str, RawValue<'a>), RECORD_EXTRA_CAPACITY>,
pub(crate) tail: Vec<(&'a str, RawValue<'a>)>,
}
pub type RecordFields<'a> = heapopt::Vec<(&'a str, RawValue<'a>), RECORD_EXTRA_CAPACITY>;

// ---

Expand Down Expand Up @@ -660,10 +649,7 @@ impl ParserSettingsBlock {
return;
}
}
match to.fields.head.push((key, value)) {
Ok(_) => {}
Err(value) => to.fields.tail.push(value),
}
to.fields.push((key, value));
}

#[inline(always)]
Expand Down Expand Up @@ -899,17 +885,7 @@ impl<'de: 'a, 'a> Deserialize<'de> for RawRecord<'a> {

// ---

pub struct RawRecordFields<'a> {
head: heapless::Vec<(&'a str, RawValue<'a>), RAW_RECORD_FIELDS_CAPACITY>,
tail: Vec<(&'a str, RawValue<'a>)>,
}

impl<'a> RawRecordFields<'a> {
#[inline]
pub fn iter(&self) -> impl Iterator<Item = &(&'a str, RawValue<'a>)> {
self.head.iter().chain(self.tail.iter())
}
}
pub type RawRecordFields<'a> = heapopt::Vec<(&'a str, RawValue<'a>), RAW_RECORD_FIELDS_CAPACITY>;

// ---

Expand Down Expand Up @@ -1108,23 +1084,14 @@ where
}

fn visit_map<M: MapAccess<'de>>(self, mut access: M) -> std::result::Result<Self::Value, M::Error> {
let mut head = heapless::Vec::new();
let count = access.size_hint().unwrap_or(0);
let mut tail = match count > RAW_RECORD_FIELDS_CAPACITY {
false => Vec::new(),
true => Vec::with_capacity(count - RAW_RECORD_FIELDS_CAPACITY),
};
let mut fields = heapopt::Vec::with_capacity(access.size_hint().unwrap_or(0));

while let Some(key) = access.next_key::<&'a str>()? {
let value: &RV = access.next_value()?;
match head.push((key, value.into())) {
Ok(_) => {}
Err(value) => tail.push(value),
}
fields.push((key, value.into()));
}

Ok(RawRecord {
fields: RawRecordFields { head, tail },
})
Ok(RawRecord { fields })
}
}

Expand Down Expand Up @@ -1766,8 +1733,8 @@ mod tests {

let rec = stream.next().unwrap().unwrap();
assert_eq!(rec.prefix, b"");
assert_eq!(rec.record.fields.head.len(), 0);
assert_eq!(rec.record.fields.tail.len(), 0);
assert_eq!(rec.record.fields.as_slices().0.len(), 0);
assert_eq!(rec.record.fields.as_slices().1.len(), 0);
}

#[test]
Expand Down

0 comments on commit 9874a51

Please sign in to comment.