Skip to content

Commit 7e75791

Browse files
committed
perf: change backing string type to strumba and use opaque wrapper to avoid breaking API in the future
1 parent 4f301b1 commit 7e75791

6 files changed

Lines changed: 177 additions & 11 deletions

File tree

Cargo.lock

Lines changed: 21 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

librashader-common/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ dxgi = ["windows"]
2121
vulkan = ["ash"]
2222
wgpu = ["wgpu-types"]
2323
metal = ["objc2", "objc2-metal"]
24-
serde = ["dep:serde", "serde/derive", "smartstring/serde", "halfbrown/serde"]
24+
serde = ["dep:serde", "serde/derive", "strumbra/serde", "halfbrown/serde"]
2525
[dependencies]
2626
num-traits = "0.2.15"
2727
rustc-hash = "2.0.0"
28-
halfbrown = "0.2.4"
29-
smartstring = "1.0"
28+
halfbrown = "0.4.0"
29+
strumbra = "0.6.0"
30+
3031
bitflags = { version = "2", features = ["serde"] }
3132

3233
glow = { workspace = true, optional = true }

librashader-common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ mod viewport;
3636
#[doc(hidden)]
3737
pub mod map;
3838
pub mod shader_features;
39+
pub mod string;
3940

4041
pub use viewport::Viewport;
4142

librashader-common/src/map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
pub type FastHashMap<K, V> =
55
halfbrown::SizedHashMap<K, V, core::hash::BuildHasherDefault<rustc_hash::FxHasher>, 32>;
66

7-
/// A string with small string optimizations up to 23 bytes.
8-
pub type ShortString = smartstring::SmartString<smartstring::LazyCompact>;
7+
/// Common string type for parameters and uniform names with small string optimizations up to 23 bytes.
8+
pub type ShortString = crate::string::ParamString;

librashader-common/src/string.rs

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
use std::borrow::Borrow;
2+
use std::cmp::Ordering;
3+
use std::fmt::{Display, Formatter};
4+
use std::ops::Deref;
5+
use strumbra::SharedString;
6+
7+
#[repr(transparent)]
8+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9+
#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Hash)]
10+
pub struct ParamString(SharedString);
11+
12+
impl Deref for ParamString {
13+
type Target = str;
14+
15+
#[inline]
16+
fn deref(&self) -> &Self::Target {
17+
self.0.as_ref()
18+
}
19+
}
20+
21+
impl From<&str> for ParamString {
22+
#[inline]
23+
fn from(s: &str) -> Self {
24+
ParamString(SharedString::try_from(s)
25+
.expect("ParamString with more than 4294967295 characters. Parameter too large."))
26+
}
27+
}
28+
29+
impl From<&String> for ParamString {
30+
#[inline]
31+
fn from(s: &String) -> Self {
32+
ParamString(SharedString::try_from(s)
33+
.expect("ParamString with more than 4294967295 characters. Parameter too large."))
34+
}
35+
}
36+
37+
38+
impl From<String> for ParamString {
39+
#[inline]
40+
fn from(s: String) -> Self {
41+
ParamString(SharedString::try_from(s)
42+
.expect("ParamString with more than 4294967295 characters. Parameter too large."))
43+
}
44+
}
45+
46+
impl Display for ParamString {
47+
#[inline]
48+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
49+
self.0.fmt(f)
50+
}
51+
}
52+
53+
impl AsRef<str> for ParamString {
54+
#[inline]
55+
fn as_ref(&self) -> &str {
56+
self.0.as_ref()
57+
}
58+
}
59+
60+
impl Borrow<str> for ParamString {
61+
fn borrow(&self) -> &str {
62+
self.0.as_ref()
63+
}
64+
}
65+
66+
impl PartialEq<&str> for ParamString {
67+
fn eq(&self, other: &&str) -> bool {
68+
self.0.as_str() == *other
69+
}
70+
}
71+
72+
impl PartialEq<str> for ParamString {
73+
fn eq(&self, other: &str) -> bool {
74+
self.0.as_str() == other
75+
}
76+
}
77+
78+
impl PartialEq<String> for ParamString {
79+
fn eq(&self, other: &String) -> bool {
80+
self.0.as_str() == other
81+
}
82+
}
83+
84+
impl PartialEq<ParamString> for &str {
85+
fn eq(&self, other: &ParamString) -> bool {
86+
*self == other.as_ref()
87+
}
88+
}
89+
90+
impl PartialEq<ParamString> for String {
91+
fn eq(&self, other: &ParamString) -> bool {
92+
*self == other.as_ref()
93+
}
94+
}
95+
96+
impl PartialEq<ParamString> for &String {
97+
fn eq(&self, other: &ParamString) -> bool {
98+
*self == other.as_ref()
99+
}
100+
}
101+
102+
103+
impl Eq for ParamString {}
104+
105+
impl PartialOrd<String> for ParamString {
106+
fn partial_cmp(&self, other: &String) -> Option<Ordering> {
107+
self.0.partial_cmp(other)
108+
}
109+
}
110+
111+
impl PartialOrd<str> for ParamString {
112+
fn partial_cmp(&self, other: &str) -> Option<Ordering> {
113+
self.0.partial_cmp(other)
114+
}
115+
}
116+
117+
impl PartialOrd<&str> for ParamString {
118+
fn partial_cmp(&self, other: &&str) -> Option<Ordering> {
119+
self.0.partial_cmp(*other)
120+
}
121+
}
122+
123+
124+
impl ParamString {
125+
/// Pushes a new string onto the `ParamString`.
126+
///
127+
/// This could incur up to two allocations.
128+
pub fn push_str(&mut self, s: &str) {
129+
let new= format!("{self}{s}");
130+
self.0 = SharedString::try_from(new)
131+
.expect("ParamString with more than 4294967295 characters.");
132+
}
133+
134+
// Extracts a string slice containing the entire `ParamString`.
135+
pub fn as_str(&self) -> &str {
136+
self.0.as_str()
137+
}
138+
}
139+
140+
#[cfg(test)]
141+
mod test {
142+
use super::*;
143+
#[test]
144+
pub fn test_push_str() {
145+
let mut string = ParamString::from("Hello");
146+
string.push_str(" World");
147+
assert_eq!("Hello World", string);
148+
}
149+
}

librashader-runtime/src/binding.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,6 @@ where
370370
_ => None,
371371
})
372372
{
373-
let id = id.as_str();
374-
375373
let default = parameter_defaults.get(id).map_or(0f32, |f| f.initial);
376374

377375
let value = *runtime_parameters.get(id).unwrap_or(&default);

0 commit comments

Comments
 (0)