1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use core::fmt;
use core::cmp;
use core::ops::Deref;
use core::hash::{Hash, Hasher};
use crate::runtime::gc::{Gc, GcTrace};
use crate::runtime::errors::{ExecResult, ErrorKind};
pub mod intern;
pub mod inline;
pub use intern::{StringSymbol, StringInterner, STRING_TABLE};
use intern::StringTable;
#[derive(Debug, Clone, Copy)]
pub enum StringValue {
Intern(StringSymbol),
Inline(InlineStr),
Gc(GCStr),
}
pub type InlineStr = inline::InlineStr<22>;
#[derive(Debug, Clone, Copy)]
pub struct GCStr(Gc<str>);
impl Deref for GCStr {
type Target = Gc<str>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
unsafe impl GcTrace for str {
fn trace(&self) { }
fn size_hint(&self) -> usize {
self.len()
}
}
const INTERN_THRESHOLD: usize = 40;
impl From<&str> for StringValue {
fn from(string: &str) -> Self {
if let Ok(inline) = InlineStr::try_new(string) {
return Self::Inline(inline);
}
if string.len() <= INTERN_THRESHOLD {
return Self::Intern(StringSymbol::intern(string))
}
Self::Gc(GCStr(Gc::from_box(string.to_string().into_boxed_str())))
}
}
impl From<StringSymbol> for StringValue {
fn from(symbol: StringSymbol) -> Self {
StringValue::Intern(symbol)
}
}
impl From<InlineStr> for StringValue {
fn from(inline: InlineStr) -> Self {
StringValue::Inline(inline)
}
}
impl From<GCStr> for StringValue {
fn from(gc_str: GCStr) -> Self {
StringValue::Gc(gc_str)
}
}
impl StringValue {
pub fn trace(&self) {
if let Self::Gc(gc_str) = self {
gc_str.mark_trace()
}
}
pub fn write(&self, buf: &mut impl fmt::Write) -> fmt::Result {
match self {
Self::Intern(symbol) => symbol.write(buf),
Self::Inline(inline) => buf.write_str(&*inline),
Self::Gc(gc_str) => buf.write_str(&**gc_str),
}
}
pub fn as_intern(&self) -> StringSymbol {
match self {
Self::Intern(symbol) => *symbol,
Self::Inline(inline) => StringSymbol::intern(&*inline),
Self::Gc(gc_str) => StringSymbol::intern(&**gc_str),
}
}
pub fn intern(&mut self) {
let symbol = self.as_intern();
*self = Self::Intern(symbol)
}
fn is_intern(&self) -> bool {
matches!(self, Self::Intern(..))
}
fn resolve_str<'s, 'h>(&'s self, string_table: &'h StringTable) -> &'s str where 'h: 's {
match self {
Self::Intern(symbol) => string_table.resolve(symbol),
Self::Inline(inline) => &*inline,
Self::Gc(gc_str) => &**gc_str,
}
}
fn try_str(&self) -> Option<&str> {
match self {
Self::Inline(inline) => Some(&*inline),
Self::Gc(gc_str) => Some(&**gc_str),
_ => None,
}
}
pub fn concat(&self, other: &StringValue) -> ExecResult<StringValue> {
let mut buf = String::new();
self.write(&mut buf)
.map_err(|error| ErrorKind::Other(format!("{}", error)))?;
other.write(&mut buf)
.map_err(|error| ErrorKind::Other(format!("{}", error)))?;
Ok(StringValue::from(buf.as_str()))
}
}
impl Hash for StringValue {
fn hash<H: Hasher>(&self, state: &mut H) {
STRING_TABLE.with(|string_table| match self {
Self::Intern(symbol) =>
string_table.borrow().lookup_hash(symbol).hash(state),
Self::Inline(inline) =>
string_table.borrow().hash_str(&*inline).hash(state),
Self::Gc(gc_str) =>
string_table.borrow().hash_str(&**gc_str).hash(state),
})
}
}
impl PartialEq for StringValue {
fn eq(&self, other: &StringValue) -> bool {
match (self, other) {
(Self::Intern(a), Self::Intern(b)) => a == b,
(Self::Intern(symbol), strval) | (strval, Self::Intern(symbol)) => STRING_TABLE.with(|string_table| {
string_table.borrow().resolve(symbol) == strval.try_str().unwrap()
}),
(a, b) => a.try_str().unwrap() == b.try_str().unwrap()
}
}
}
impl Eq for StringValue { }
impl PartialOrd for StringValue {
fn partial_cmp(&self, other: &StringValue) -> Option<cmp::Ordering> {
let a = self.try_str();
let b = other.try_str();
if a.and(b).is_some() {
return a.unwrap().partial_cmp(b.unwrap())
}
STRING_TABLE.with(|string_table| {
let string_table = string_table.borrow();
self.resolve_str(&string_table)
.partial_cmp(other.resolve_str(&string_table))
})
}
}
impl Ord for StringValue {
fn cmp(&self, other: &Self) -> cmp::Ordering {
let a = self.try_str();
let b = other.try_str();
if a.and(b).is_some() {
return a.unwrap().cmp(b.unwrap())
}
STRING_TABLE.with(|string_table| {
let string_table = string_table.borrow();
self.resolve_str(&string_table)
.cmp(other.resolve_str(&string_table))
})
}
}
impl fmt::Display for StringValue {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
self.write(fmt)
}
}