Fields

Struct Fields 

Source
pub struct Fields(/* private fields */);
Expand description

A record’s fields, sorted by name.

This was a BTreeMap, and a record is the wrong size for one: three to eight entries, built once and read many times. A B-tree pays a node allocation and a pointer chase per level to buy an asymptotic advantage that never arrives at that size, and profiling awfy/havlak.beck put a fifth of the process inside its search, its insert and the memcmp underneath them.

Sorted by name and searched linearly: one allocation for the whole record, the names lie next to each other in cache, and get compares lengths before bytes because it wants equality rather than order. Iteration is in name order, so the value order, the state digest and the wire format (crate::repr) are all bit-for-bit what the BTreeMap gave — which is what makes this a representation change and not a semantic one.

Implementations§

Source§

impl Fields

Source

pub fn new() -> Fields

Source

pub fn with_capacity(n: usize) -> Fields

Source

pub fn get(&self, name: &str) -> Option<&Value>

Source

pub fn insert(&mut self, name: Arc<str>, value: Value) -> Option<Value>

Set name, keeping the order by name. Answers the value that was there.

The search is by equality and not by order, which is the whole difference: == on two strs compares their lengths first and reaches memcmp only for a pair that could match, where a binary search has to order every probe it makes. A record has three to eight fields, so a scan makes at most as many comparisons as a binary search and nearly all of them are an integer test. Only a field that is genuinely new pays for the ordered insert, and with — which is what calls this in a loop — never has one.

Source

pub fn from_pairs(pairs: Vec<(Arc<str>, Value)>) -> Fields

Build from fields in any order, sorting once.

This is how a record literal is built, and it is a separate entry point from insert in a loop because the two cost differently: sort_unstable_by on a handful of elements is an insertion sort, which makes n - 1 comparisons and moves nothing when the fields already arrive in order — as a record literal’s usually do.

Source

pub fn from_sorted(pairs: Vec<(Arc<str>, Value)>) -> Fields

Build from fields the caller has already put in order.

The caller is crate::fields, which decided the order once at compile time — a record literal’s field names are written in the source, so sorting them once per record built is work with a known answer. Nothing else should use this: the order is the Map iteration, the state digest and the patch stream, so getting it wrong is a wire-format bug rather than a slow lookup.

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn iter(&self) -> Iter<'_, (Arc<str>, Value)>

Source

pub fn values(&self) -> impl Iterator<Item = &Value>

Trait Implementations§

Source§

impl Clone for Fields

Source§

fn clone(&self) -> Fields

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Fields

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Fields

Source§

fn default() -> Fields

Returns the “default value” for a type. Read more
Source§

impl FromIterator<(Arc<str>, Value)> for Fields

Source§

fn from_iter<I: IntoIterator<Item = (Arc<str>, Value)>>(it: I) -> Fields

Creates a value from an iterator. Read more
Source§

impl<'a> IntoIterator for &'a Fields

Source§

type Item = &'a (Arc<str>, Value)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, (Arc<str>, Value)>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl Ord for Fields

Source§

fn cmp(&self, other: &Fields) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Fields

Source§

fn eq(&self, other: &Fields) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Fields

Source§

fn partial_cmp(&self, other: &Fields) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for Fields

Source§

impl StructuralPartialEq for Fields

Auto Trait Implementations§

§

impl Freeze for Fields

§

impl RefUnwindSafe for Fields

§

impl Send for Fields

§

impl Sync for Fields

§

impl Unpin for Fields

§

impl UnwindSafe for Fields

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.