11
11
//! content of its components. The content of a `TAG_STR_VAL` is its actual
12
12
//! UTF-8 bytes. The content of a `TAG_STR_REF` is the contents of the entry
13
13
//! it references.
14
+ //!
15
+ //! Each string is referred to via a `StringId`. `StringId`s may be generated in two ways:
16
+ //! 1. Calling `StringTable::alloc()` which returns the `StringId` for the allocated string.
17
+ //! 2. Calling `StringTable::alloc_with_reserved_id()` and `StringId::reserved()`.
18
+ //!
19
+ //! Reserved strings allow you to deduplicate strings by allocating a string once and then referring
20
+ //! to it by id over and over. This is a useful trick for strings which are recorded many times and
21
+ //! it can significantly reduce the size of profile trace files.
22
+ //!
23
+ //! `StringId`s are partitioned according to type:
24
+ //!
25
+ //! > [0 .. MAX_PRE_RESERVED_STRING_ID, METADATA_STRING_ID, .. ]
26
+ //!
27
+ //! From `0` to `MAX_PRE_RESERVED_STRING_ID` are the allowed values for reserved strings.
28
+ //! After `MAX_PRE_RESERVED_STRING_ID`, there is one string id (`METADATA_STRING_ID`) which is used
29
+ //! internally by `measureme` to record additional metadata about the profiling session.
30
+ //! After `METADATA_STRING_ID` are all other `StringId` values.
14
31
15
32
use crate :: file_header:: {
16
33
read_file_header, strip_file_header, write_file_header, FILE_MAGIC_STRINGTABLE_DATA ,
@@ -47,13 +64,17 @@ const TAG_STR_VAL: u8 = 1;
47
64
/// Marks a component that contains the ID of another string.
48
65
const TAG_STR_REF : u8 = 2 ;
49
66
67
+ /// The maximum id value a prereserved string may be.
50
68
const MAX_PRE_RESERVED_STRING_ID : u32 = std:: u32:: MAX / 2 ;
51
69
70
+ /// The id of the profile metadata string entry.
71
+ pub ( crate ) const METADATA_STRING_ID : u32 = MAX_PRE_RESERVED_STRING_ID + 1 ;
72
+
52
73
/// Write-only version of the string table
53
74
pub struct StringTableBuilder < S : SerializationSink > {
54
75
data_sink : Arc < S > ,
55
76
index_sink : Arc < S > ,
56
- id_counter : AtomicU32 , // initialized to MAX_PRE_RESERVED_STRING_ID + 1
77
+ id_counter : AtomicU32 , // initialized to METADATA_STRING_ID + 1
57
78
}
58
79
59
80
/// Anything that implements `SerializableString` can be written to a
@@ -129,7 +150,7 @@ impl<S: SerializationSink> StringTableBuilder<S> {
129
150
StringTableBuilder {
130
151
data_sink,
131
152
index_sink,
132
- id_counter : AtomicU32 :: new ( MAX_PRE_RESERVED_STRING_ID + 1 ) ,
153
+ id_counter : AtomicU32 :: new ( METADATA_STRING_ID + 1 ) ,
133
154
}
134
155
}
135
156
@@ -144,10 +165,16 @@ impl<S: SerializationSink> StringTableBuilder<S> {
144
165
id
145
166
}
146
167
168
+ pub ( crate ) fn alloc_metadata < STR : SerializableString + ?Sized > ( & self , s : & STR ) -> StringId {
169
+ let id = StringId ( METADATA_STRING_ID ) ;
170
+ self . alloc_unchecked ( id, s) ;
171
+ id
172
+ }
173
+
147
174
#[ inline]
148
175
pub fn alloc < STR : SerializableString + ?Sized > ( & self , s : & STR ) -> StringId {
149
176
let id = StringId ( self . id_counter . fetch_add ( 1 , Ordering :: SeqCst ) ) ;
150
- debug_assert ! ( id. 0 > MAX_PRE_RESERVED_STRING_ID ) ;
177
+ debug_assert ! ( id. 0 > METADATA_STRING_ID ) ;
151
178
self . alloc_unchecked ( id, s) ;
152
179
id
153
180
}
0 commit comments