Skip to content

Commit fa51ac0

Browse files
committed
[read-fonts] AI unsafe reads
Major speedup observed in HR benchmarks.
1 parent dd3873d commit fa51ac0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1392
-1253
lines changed

font-codegen/src/fields.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -825,13 +825,15 @@ impl Field {
825825
let range_stmt = self.getter_range_stmt();
826826
let mut read_stmt = if let Some(args) = &self.attrs.read_with_args {
827827
let get_args = args.to_tokens_for_table_getter();
828-
quote!( self.data.read_with_args(range, &#get_args).unwrap() )
828+
quote!( unsafe { self.data.read_with_args_unchecked(range, &#get_args).unwrap() } )
829829
} else if is_var_array {
830-
quote!(VarLenArray::read(self.data.split_off(range.start).unwrap()).unwrap())
830+
quote!(
831+
unsafe { VarLenArray::read(self.data.split_off_unchecked(range.start)).unwrap() }
832+
)
831833
} else if is_array {
832-
quote!(self.data.read_array(range).unwrap())
834+
quote!(unsafe { self.data.read_array_unchecked(range) })
833835
} else {
834-
quote!(self.data.read_at(range.start).unwrap())
836+
quote!(unsafe { self.data.read_at_unchecked(range.start) })
835837
};
836838
if is_versioned {
837839
read_stmt = quote!(Some(#read_stmt));

read-fonts/generated/font.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,34 +71,34 @@ impl<'a> TableDirectory<'a> {
7171
/// 0x00010000 or 0x4F54544F
7272
pub fn sfnt_version(&self) -> u32 {
7373
let range = self.sfnt_version_byte_range();
74-
self.data.read_at(range.start).unwrap()
74+
unsafe { self.data.read_at_unchecked(range.start) }
7575
}
7676

7777
/// Number of tables.
7878
pub fn num_tables(&self) -> u16 {
7979
let range = self.num_tables_byte_range();
80-
self.data.read_at(range.start).unwrap()
80+
unsafe { self.data.read_at_unchecked(range.start) }
8181
}
8282

8383
pub fn search_range(&self) -> u16 {
8484
let range = self.search_range_byte_range();
85-
self.data.read_at(range.start).unwrap()
85+
unsafe { self.data.read_at_unchecked(range.start) }
8686
}
8787

8888
pub fn entry_selector(&self) -> u16 {
8989
let range = self.entry_selector_byte_range();
90-
self.data.read_at(range.start).unwrap()
90+
unsafe { self.data.read_at_unchecked(range.start) }
9191
}
9292

9393
pub fn range_shift(&self) -> u16 {
9494
let range = self.range_shift_byte_range();
95-
self.data.read_at(range.start).unwrap()
95+
unsafe { self.data.read_at_unchecked(range.start) }
9696
}
9797

9898
/// Table records array—one for each top-level table in the font
9999
pub fn table_records(&self) -> &'a [TableRecord] {
100100
let range = self.table_records_byte_range();
101-
self.data.read_array(range).unwrap()
101+
unsafe { self.data.read_array_unchecked(range) }
102102
}
103103
}
104104

@@ -287,43 +287,43 @@ impl<'a> TTCHeader<'a> {
287287
/// Font Collection ID string: \"ttcf\"
288288
pub fn ttc_tag(&self) -> Tag {
289289
let range = self.ttc_tag_byte_range();
290-
self.data.read_at(range.start).unwrap()
290+
unsafe { self.data.read_at_unchecked(range.start) }
291291
}
292292

293293
/// Major/minor version of the TTC Header
294294
pub fn version(&self) -> MajorMinor {
295295
let range = self.version_byte_range();
296-
self.data.read_at(range.start).unwrap()
296+
unsafe { self.data.read_at_unchecked(range.start) }
297297
}
298298

299299
/// Number of fonts in TTC
300300
pub fn num_fonts(&self) -> u32 {
301301
let range = self.num_fonts_byte_range();
302-
self.data.read_at(range.start).unwrap()
302+
unsafe { self.data.read_at_unchecked(range.start) }
303303
}
304304

305305
/// Array of offsets to the TableDirectory for each font from the beginning of the file
306306
pub fn table_directory_offsets(&self) -> &'a [BigEndian<u32>] {
307307
let range = self.table_directory_offsets_byte_range();
308-
self.data.read_array(range).unwrap()
308+
unsafe { self.data.read_array_unchecked(range) }
309309
}
310310

311311
/// Tag indicating that a DSIG table exists, 0x44534947 ('DSIG') (null if no signature)
312312
pub fn dsig_tag(&self) -> Option<u32> {
313313
let range = self.dsig_tag_byte_range()?;
314-
Some(self.data.read_at(range.start).unwrap())
314+
Some(unsafe { self.data.read_at_unchecked(range.start) })
315315
}
316316

317317
/// The length (in bytes) of the DSIG table (null if no signature)
318318
pub fn dsig_length(&self) -> Option<u32> {
319319
let range = self.dsig_length_byte_range()?;
320-
Some(self.data.read_at(range.start).unwrap())
320+
Some(unsafe { self.data.read_at_unchecked(range.start) })
321321
}
322322

323323
/// The offset (in bytes) of the DSIG table from the beginning of the TTC file (null if no signature)
324324
pub fn dsig_offset(&self) -> Option<u32> {
325325
let range = self.dsig_offset_byte_range()?;
326-
Some(self.data.read_at(range.start).unwrap())
326+
Some(unsafe { self.data.read_at_unchecked(range.start) })
327327
}
328328
}
329329

0 commit comments

Comments
 (0)