@@ -187,17 +187,19 @@ impl AtomType {
187
187
// which would cause divisions by zero in rust-phf.
188
188
self . atoms . insert ( String :: new ( ) ) ;
189
189
190
- // Strings over 7 bytes and empty string added to static set
191
- let atoms: Vec < & str > = self
190
+ // Strings over 7 bytes + empty string added to static set.
191
+ // Otherwise stored inline.
192
+ let ( static_strs, inline_strs) : ( Vec < _ > , Vec < _ > ) = self
192
193
. atoms
193
194
. iter ( )
194
- . filter ( |s| s. len ( ) > 7 || s. is_empty ( ) )
195
- . map ( |s| & * * s)
196
- . collect ( ) ;
197
- let hash_state = phf_generator:: generate_hash ( & atoms) ;
195
+ . map ( String :: as_str)
196
+ . partition ( |s| s. len ( ) > 7 || s. is_empty ( ) ) ;
197
+
198
+ // Static strings
199
+ let hash_state = phf_generator:: generate_hash ( & static_strs) ;
198
200
let phf_generator:: HashState { key, disps, map } = hash_state;
199
201
let ( disps0, disps1) : ( Vec < _ > , Vec < _ > ) = disps. into_iter ( ) . unzip ( ) ;
200
- let atoms: Vec < & str > = map. iter ( ) . map ( |& idx| atoms [ idx] ) . collect ( ) ;
202
+ let atoms: Vec < & str > = map. iter ( ) . map ( |& idx| static_strs [ idx] ) . collect ( ) ;
201
203
let empty_string_index = atoms. iter ( ) . position ( |s| s. is_empty ( ) ) . unwrap ( ) as u32 ;
202
204
let indices = 0 ..atoms. len ( ) as u32 ;
203
205
@@ -234,45 +236,33 @@ impl AtomType {
234
236
let macro_name = new_term ( & * self . macro_name ) ;
235
237
let module = module. parse :: < proc_macro2:: TokenStream > ( ) . unwrap ( ) ;
236
238
let atom_prefix = format ! ( "ATOM_{}_" , type_name. to_string( ) . to_uppercase( ) ) ;
237
- let const_names: Vec < _ > = atoms
238
- . iter ( )
239
- . map ( |atom| {
240
- let mut name = atom_prefix. clone ( ) ;
241
- for c in atom. chars ( ) {
242
- name. push_str ( & format ! ( "_{:02X}" , c as u32 ) )
243
- }
244
- new_term ( & name)
245
- } )
246
- . collect ( ) ;
239
+ let new_const_name = |atom : & str | {
240
+ let mut name = atom_prefix. clone ( ) ;
241
+ for c in atom. chars ( ) {
242
+ name. push_str ( & format ! ( "_{:02X}" , c as u32 ) )
243
+ }
244
+ new_term ( & name)
245
+ } ;
246
+ let const_names: Vec < _ > = atoms. iter ( ) . copied ( ) . map ( new_const_name) . collect ( ) ;
247
247
248
- // Strings 7 bytes or less (except empty string) stored inline
249
- let short_strs: Vec < & str > = self
250
- . atoms
251
- . iter ( )
252
- . filter ( |s| s. len ( ) < 8 && !s. is_empty ( ) )
253
- . map ( |s| & * * s)
254
- . collect ( ) ;
255
- let short_const_names: Vec < _ > = short_strs
256
- . iter ( )
257
- . map ( |s| {
258
- let mut name = atom_prefix. clone ( ) ;
259
- for c in s. chars ( ) {
260
- name. push_str ( & format ! ( "_{:02X}" , c as u32 ) )
261
- }
262
- new_term ( & name)
263
- } )
264
- . collect ( ) ;
265
- let short_values: Vec < _ > = short_strs
248
+ // Inline strings
249
+ let ( inline_const_names, inline_values_and_lengths) : ( Vec < _ > , Vec < _ > ) = inline_strs
266
250
. iter ( )
267
251
. map ( |s| {
268
- let mut n = 0u64 ;
252
+ let const_name = new_const_name ( s) ;
253
+
254
+ let mut value = 0u64 ;
269
255
for ( index, c) in s. bytes ( ) . enumerate ( ) {
270
- n = n | ( ( c as u64 ) << ( index * 8 + 8 ) ) ;
256
+ value = value | ( ( c as u64 ) << ( index * 8 + 8 ) ) ;
271
257
}
272
- n
258
+
259
+ let len = s. len ( ) as u8 ;
260
+
261
+ ( const_name, ( value, len) )
273
262
} )
274
- . collect ( ) ;
275
- let short_lens: Vec < _ > = short_strs. iter ( ) . map ( |s| s. len ( ) as u8 ) . collect ( ) ;
263
+ . unzip ( ) ;
264
+ let ( inline_values, inline_lengths) : ( Vec < _ > , Vec < _ > ) =
265
+ inline_values_and_lengths. into_iter ( ) . unzip ( ) ;
276
266
277
267
quote ! {
278
268
#atom_doc
@@ -301,7 +291,7 @@ impl AtomType {
301
291
pub const #const_names: #type_name = #type_name:: pack_static( #indices) ;
302
292
) *
303
293
#(
304
- pub const #short_const_names : #type_name = #type_name:: pack_inline( #short_values , #short_lens ) ;
294
+ pub const #inline_const_names : #type_name = #type_name:: pack_inline( #inline_values , #inline_lengths ) ;
305
295
) *
306
296
307
297
#macro_doc
@@ -311,7 +301,7 @@ impl AtomType {
311
301
( #atoms) => { #module:: #const_names } ;
312
302
) *
313
303
#(
314
- ( #short_strs ) => { #module:: #short_const_names } ;
304
+ ( #inline_strs ) => { #module:: #inline_const_names } ;
315
305
) *
316
306
}
317
307
}
0 commit comments