From f0f2b2d3e12220d141de0c71872ffd79b9367cdc Mon Sep 17 00:00:00 2001 From: Dev380 <49997896+Dev380@users.noreply.github.com> Date: Fri, 26 Jun 2026 19:05:10 -0400 Subject: [PATCH] perf: use SmolStr from smol_str instead of String --- Cargo.toml | 1 + crates/mti/Cargo.toml | 1 + crates/mti/src/magic_type_id.rs | 13 +++++++------ crates/typeid-prefix/Cargo.toml | 1 + crates/typeid-prefix/src/type_id_prefix.rs | 7 ++++--- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5e41a7a..0593730 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ uuid = { version = "1.10", features = ["v1", "v3", "v4", "v5", "v6", "v7"] } serde = { version = "1.0", features = ["derive"] } tracing = { version = "0.1" } arbitrary = { version = "1.3.2", features = ["derive"] } +smol_str = { version = "0.3.6" } # Dev dependencies proptest = { version = "1.5.0", features = ["proptest-macro"] } diff --git a/crates/mti/Cargo.toml b/crates/mti/Cargo.toml index 141bdcc..b379be5 100644 --- a/crates/mti/Cargo.toml +++ b/crates/mti/Cargo.toml @@ -14,6 +14,7 @@ categories = ["data-structures", "development-tools", "encoding", "parser-implem [dependencies] typeid_prefix = { workspace = true } typeid_suffix = { workspace = true } +smol_str = { workspace = true } serde = { workspace = true, optional = true } tracing = { workspace = true, optional = true } diff --git a/crates/mti/src/magic_type_id.rs b/crates/mti/src/magic_type_id.rs index 6d89d38..2ee5445 100644 --- a/crates/mti/src/magic_type_id.rs +++ b/crates/mti/src/magic_type_id.rs @@ -5,6 +5,7 @@ use std::fmt::{Display, Formatter}; use std::hash::{Hash, Hasher}; use std::ops::Deref; use std::str::FromStr; +use smol_str::{SmolStr, ToSmolStr, format_smolstr}; use typeid_prefix::{TypeIdPrefix, ValidationError}; use typeid_suffix::prelude::*; @@ -81,7 +82,7 @@ use tracing::{debug, instrument, trace}; pub struct MagicTypeId { prefix: TypeIdPrefix, suffix: TypeIdSuffix, - string_repr: String, + string_repr: SmolStr, } impl Ord for MagicTypeId { @@ -115,13 +116,13 @@ impl PartialOrd for str { // Implement PartialOrd for String impl PartialOrd for MagicTypeId { fn partial_cmp(&self, other: &String) -> Option { - self.string_repr.partial_cmp(other) + self.string_repr.as_str().partial_cmp(other.as_str()) } } impl PartialOrd for String { fn partial_cmp(&self, other: &MagicTypeId) -> Option { - self.partial_cmp(&other.string_repr) + self.as_str().partial_cmp(other.string_repr.as_str()) } } @@ -178,11 +179,11 @@ impl MagicTypeId { let string_repr = if prefix.is_empty() { #[cfg(feature = "instrument")] trace!("Creating MagicTypeId with empty prefix"); - suffix.to_string() + suffix.to_smolstr() } else { #[cfg(feature = "instrument")] trace!("Creating MagicTypeId with prefix and suffix"); - format!("{prefix}_{suffix}") + format_smolstr!("{prefix}_{suffix}") }; #[cfg(feature = "instrument")] debug!("Created MagicTypeId: {}", string_repr); @@ -253,7 +254,7 @@ impl MagicTypeId { /// assert_eq!(type_id.as_str(), "user_01h455vb4pex5vsknk084sn02q"); /// ``` #[must_use] - pub const fn as_str(&self) -> &str { + pub fn as_str(&self) -> &str { self.string_repr.as_str() } } diff --git a/crates/typeid-prefix/Cargo.toml b/crates/typeid-prefix/Cargo.toml index 24dddad..2ba1620 100644 --- a/crates/typeid-prefix/Cargo.toml +++ b/crates/typeid-prefix/Cargo.toml @@ -18,6 +18,7 @@ arbitrary = ["dep:arbitrary"] serde = ["dep:serde"] [dependencies] +smol_str = { workspace = true } arbitrary = { workspace = true, optional = true } tracing = { workspace = true, optional = true } serde = { workspace = true, optional = true } diff --git a/crates/typeid-prefix/src/type_id_prefix.rs b/crates/typeid-prefix/src/type_id_prefix.rs index f4ac6aa..944d38f 100644 --- a/crates/typeid-prefix/src/type_id_prefix.rs +++ b/crates/typeid-prefix/src/type_id_prefix.rs @@ -5,6 +5,7 @@ use std::str::FromStr; use crate::ValidationError; +use smol_str::{SmolStr, ToSmolStr}; #[cfg(feature = "serde")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -30,7 +31,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer}; /// ``` #[derive(Default, Debug, Clone, PartialEq, Eq, Hash)] #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] -pub struct TypeIdPrefix(String); +pub struct TypeIdPrefix(SmolStr); #[cfg(feature = "serde")] impl Serialize for TypeIdPrefix { @@ -237,7 +238,7 @@ impl TypeIdPrefix { return Err(ValidationError::ContainsInvalidCharacters); } - Ok(Self(input.to_string())) + Ok(Self(input.to_smolstr())) } pub(crate) fn clean_inner(input: &str) -> String { @@ -273,7 +274,7 @@ impl TypeIdPrefix { /// assert_eq!(prefix.as_str(), "valid_prefix"); /// ``` #[must_use] - pub const fn as_str(&self) -> &str { + pub fn as_str(&self) -> &str { self.0.as_str() } }