From 999b9c7a138a5dd5277085570042fc67a893e5af Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 11 Aug 2022 19:02:19 +0300 Subject: refactor(tvix/value): replace static representation with SmolStr The only uses of the static variant were for `"name"` and `"value"`, which are both small enough to fit into a SmolStr. The size of NixString accomodates `String` anyways, so we may as well inline them. Additionally smol_str is already in the dependency graph because rnix uses it, and using it for representations of identifiers is sensible. Change-Id: I9969312256d1657d69128e54c47dc7294a18ce58 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6165 Tested-by: BuildkiteCI Reviewed-by: sterni Reviewed-by: grfn --- tvix/eval/src/value/string.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'tvix/eval/src/value/string.rs') diff --git a/tvix/eval/src/value/string.rs b/tvix/eval/src/value/string.rs index 3025668218..626cf398f2 100644 --- a/tvix/eval/src/value/string.rs +++ b/tvix/eval/src/value/string.rs @@ -1,3 +1,4 @@ +use smol_str::SmolStr; use std::hash::Hash; use std::{borrow::Cow, fmt::Display}; @@ -6,7 +7,7 @@ use std::{borrow::Cow, fmt::Display}; #[derive(Clone, Debug)] enum StringRepr { - Static(&'static str), + Smol(SmolStr), Heap(String), } @@ -33,9 +34,9 @@ impl Ord for NixString { } } -impl From<&'static str> for NixString { - fn from(s: &'static str) -> Self { - NixString(StringRepr::Static(s)) +impl From<&str> for NixString { + fn from(s: &str) -> Self { + NixString(StringRepr::Smol(SmolStr::new(s))) } } @@ -52,12 +53,12 @@ impl Hash for NixString { } impl NixString { - pub const NAME: Self = NixString(StringRepr::Static("name")); - pub const VALUE: Self = NixString(StringRepr::Static("value")); + pub const NAME: Self = NixString(StringRepr::Smol(SmolStr::new_inline("name"))); + pub const VALUE: Self = NixString(StringRepr::Smol(SmolStr::new_inline("value"))); pub fn as_str(&self) -> &str { match &self.0 { - StringRepr::Static(s) => s, + StringRepr::Smol(s) => s.as_str(), StringRepr::Heap(s) => s, } } -- cgit 1.4.1