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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use crate::file_header::{
write_file_header, FILE_MAGIC_STRINGTABLE_DATA, FILE_MAGIC_STRINGTABLE_INDEX,
};
use crate::serialization::{Addr, SerializationSink};
use byteorder::{BigEndian, ByteOrder, LittleEndian};
use std::sync::Arc;
#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash)]
#[repr(C)]
pub struct StringId(u32);
impl StringId {
pub const INVALID: StringId = StringId(INVALID_STRING_ID);
#[inline]
pub fn new(id: u32) -> StringId {
assert!(id <= MAX_STRING_ID);
StringId(id)
}
#[inline]
pub fn new_virtual(id: u32) -> StringId {
assert!(id <= MAX_USER_VIRTUAL_STRING_ID);
StringId(id)
}
#[inline]
pub fn is_virtual(self) -> bool {
self.0 <= METADATA_STRING_ID
}
#[inline]
pub fn as_u32(self) -> u32 {
self.0
}
#[inline]
pub fn from_addr(addr: Addr) -> StringId {
let id = addr.0 + FIRST_REGULAR_STRING_ID;
StringId::new(id)
}
#[inline]
pub fn to_addr(self) -> Addr {
assert!(self.0 >= FIRST_REGULAR_STRING_ID);
Addr(self.0 - FIRST_REGULAR_STRING_ID)
}
}
pub const TERMINATOR: u8 = 0xFF;
pub const MAX_STRING_ID: u32 = 0x3FFF_FFFF;
pub const STRING_ID_MASK: u32 = 0x3FFF_FFFF;
const MAX_USER_VIRTUAL_STRING_ID: u32 = 100_000_000;
pub const METADATA_STRING_ID: u32 = MAX_USER_VIRTUAL_STRING_ID + 1;
const INVALID_STRING_ID: u32 = METADATA_STRING_ID + 1;
pub const FIRST_REGULAR_STRING_ID: u32 = INVALID_STRING_ID + 1;
pub struct StringTableBuilder<S: SerializationSink> {
data_sink: Arc<S>,
index_sink: Arc<S>,
}
pub trait SerializableString {
fn serialized_size(&self) -> usize;
fn serialize(&self, bytes: &mut [u8]);
}
impl SerializableString for str {
#[inline]
fn serialized_size(&self) -> usize {
self.len() +
1
}
#[inline]
fn serialize(&self, bytes: &mut [u8]) {
let last_byte_index = bytes.len() - 1;
bytes[0..last_byte_index].copy_from_slice(self.as_bytes());
bytes[last_byte_index] = TERMINATOR;
}
}
pub enum StringComponent<'s> {
Value(&'s str),
Ref(StringId),
}
impl<'s> StringComponent<'s> {
#[inline]
fn serialized_size(&self) -> usize {
match *self {
StringComponent::Value(s) => s.len(),
StringComponent::Ref(_) => 4,
}
}
#[inline]
fn serialize<'b>(&self, bytes: &'b mut [u8]) -> &'b mut [u8] {
match *self {
StringComponent::Value(s) => {
bytes[..s.len()].copy_from_slice(s.as_bytes());
&mut bytes[s.len()..]
}
StringComponent::Ref(string_id) => {
assert!(string_id.0 == string_id.0 & STRING_ID_MASK);
let tagged = string_id.0 | (1u32 << 31);
BigEndian::write_u32(&mut bytes[0..4], tagged);
&mut bytes[4..]
}
}
}
}
impl<'a> SerializableString for [StringComponent<'a>] {
#[inline]
fn serialized_size(&self) -> usize {
self.iter().map(|c| c.serialized_size()).sum::<usize>() +
1
}
#[inline]
fn serialize(&self, mut bytes: &mut [u8]) {
assert!(bytes.len() == self.serialized_size());
for component in self.iter() {
bytes = component.serialize(bytes);
}
assert!(bytes.len() == 1);
bytes[0] = TERMINATOR;
}
}
macro_rules! impl_serializable_string_for_fixed_size {
($n:expr) => {
impl<'a> SerializableString for [StringComponent<'a>; $n] {
#[inline(always)]
fn serialized_size(&self) -> usize {
(&self[..]).serialized_size()
}
#[inline(always)]
fn serialize(&self, bytes: &mut [u8]) {
(&self[..]).serialize(bytes);
}
}
};
}
impl_serializable_string_for_fixed_size!(0);
impl_serializable_string_for_fixed_size!(1);
impl_serializable_string_for_fixed_size!(2);
impl_serializable_string_for_fixed_size!(3);
impl_serializable_string_for_fixed_size!(4);
impl_serializable_string_for_fixed_size!(5);
impl_serializable_string_for_fixed_size!(6);
impl_serializable_string_for_fixed_size!(7);
impl_serializable_string_for_fixed_size!(8);
impl_serializable_string_for_fixed_size!(9);
impl_serializable_string_for_fixed_size!(10);
impl_serializable_string_for_fixed_size!(11);
impl_serializable_string_for_fixed_size!(12);
impl_serializable_string_for_fixed_size!(13);
impl_serializable_string_for_fixed_size!(14);
impl_serializable_string_for_fixed_size!(15);
impl_serializable_string_for_fixed_size!(16);
fn serialize_index_entry<S: SerializationSink>(sink: &S, id: StringId, addr: Addr) {
sink.write_atomic(8, |bytes| {
LittleEndian::write_u32(&mut bytes[0..4], id.0);
LittleEndian::write_u32(&mut bytes[4..8], addr.0);
});
}
impl<S: SerializationSink> StringTableBuilder<S> {
pub fn new(data_sink: Arc<S>, index_sink: Arc<S>) -> StringTableBuilder<S> {
write_file_header(&*data_sink, FILE_MAGIC_STRINGTABLE_DATA);
write_file_header(&*index_sink, FILE_MAGIC_STRINGTABLE_INDEX);
StringTableBuilder {
data_sink,
index_sink,
}
}
pub fn map_virtual_to_concrete_string(&self, virtual_id: StringId, concrete_id: StringId) {
assert!(virtual_id.0 <= MAX_USER_VIRTUAL_STRING_ID);
serialize_index_entry(&*self.index_sink, virtual_id, concrete_id.to_addr());
}
pub fn bulk_map_virtual_to_single_concrete_string<I>(
&self,
virtual_ids: I,
concrete_id: StringId,
) where
I: Iterator<Item = StringId> + ExactSizeIterator,
{
type MappingEntry = [u32; 2];
assert!(std::mem::size_of::<MappingEntry>() == 8);
let to_addr_le = concrete_id.to_addr().0.to_le();
let serialized: Vec<MappingEntry> = virtual_ids
.map(|from| {
let id = from.0;
assert!(id <= MAX_USER_VIRTUAL_STRING_ID);
[id.to_le(), to_addr_le]
})
.collect();
let num_bytes = serialized.len() * std::mem::size_of::<MappingEntry>();
let byte_ptr = serialized.as_ptr() as *const u8;
let bytes = unsafe { std::slice::from_raw_parts(byte_ptr, num_bytes) };
self.index_sink.write_bytes_atomic(bytes);
}
pub(crate) fn alloc_metadata<STR: SerializableString + ?Sized>(&self, s: &STR) {
let concrete_id = self.alloc(s);
let virtual_id = StringId(METADATA_STRING_ID);
assert!(virtual_id.is_virtual());
serialize_index_entry(&*self.index_sink, virtual_id, concrete_id.to_addr());
}
pub fn alloc<STR: SerializableString + ?Sized>(&self, s: &STR) -> StringId {
let size_in_bytes = s.serialized_size();
let addr = self.data_sink.write_atomic(size_in_bytes, |mem| {
s.serialize(mem);
});
StringId::from_addr(addr)
}
}