diff options
author | Martin Fischer <martin@push-f.com> | 2023-08-16 12:00:29 +0200 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2023-08-19 13:41:55 +0200 |
commit | 180f6d6111b966627aa00a4017b6fb9751f7386c (patch) | |
tree | 4a0b638eebf352761da3425370ba3ac8dbad73c5 /src/attr.rs | |
parent | 78c2066021deda483a4f0a1bb575543d98d27dca (diff) |
perf: only store start offsets for attribute spans
This spares us two usizes per AttrInternal<Range<usize>>.
So on a 64 bit target where a usize is 8 bytes this spares
us 16 bytes of memory per attribute (if spans are enabled,
... for Token<()> this obviously doesn't change anything).
And the DefaultEmitter now also no longer has to update the
spans on each Emitter::push_attribute_(name|value) call.
The spans are now calculated on demand by the Attribute methods,
which is fine since the assumption is that API users are only
interested in a few specific spans (rather than all spans).
Diffstat (limited to 'src/attr.rs')
-rw-r--r-- | src/attr.rs | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/src/attr.rs b/src/attr.rs index a56eb95..4c7e330 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -30,8 +30,10 @@ pub struct AttributeMap<O> { #[derive(Debug, Eq, PartialEq)] pub(crate) struct AttrInternal<O> { pub value: String, - pub name_span: Range<O>, - pub value_span: Range<O>, + /// The start offset of the attribute name. + pub name_offset: O, + /// The start offset of the attribute value. + pub value_offset: O, } /// An HTML attribute borrowed from an [`AttributeMap`]. @@ -74,14 +76,14 @@ impl<'a, O: Offset> Attribute<'a, O> { &self.map_val.value } - /// Returns the span of the attribute name. + /// Calculates the span of the attribute name and returns it. pub fn name_span(&self) -> Range<O> { - self.map_val.name_span.clone() + self.map_val.name_offset..self.map_val.name_offset + self.name.len() } - /// Returns the span of the attribute value. + /// Calculates the span of the attribute value and returns it. pub fn value_span(&self) -> Range<O> { - self.map_val.value_span.clone() + self.map_val.value_offset..self.map_val.value_offset + self.map_val.value.len() } } @@ -154,8 +156,8 @@ impl<O: Default> FromIterator<(String, String)> for AttributeMap<O> { name, AttrInternal { value, - name_span: O::default()..O::default(), - value_span: O::default()..O::default(), + name_offset: O::default(), + value_offset: O::default(), }, ) }) @@ -163,3 +165,9 @@ impl<O: Default> FromIterator<(String, String)> for AttributeMap<O> { } } } + +impl<O: Offset> AttrInternal<O> { + pub(crate) fn name_span(&self, name_len: usize) -> Range<O> { + self.name_offset..self.name_offset + name_len + } +} |