@@ -631,6 +631,13 @@ pub(crate) enum Format2Rule<'a> {
631631 Chain ( ChainedClassSequenceRule < ' a > ) ,
632632}
633633
634+ #[ derive( Default ) ]
635+ pub ( crate ) struct SeqCache {
636+ input : FnvHashMap < u16 , bool > ,
637+ backtrack : FnvHashMap < u16 , bool > ,
638+ lookahead : FnvHashMap < u16 , bool > ,
639+ }
640+
634641impl ContextFormat2 < ' _ > {
635642 pub ( crate ) fn coverage ( & self ) -> Option < Result < CoverageTable < ' _ > , ReadError > > {
636643 match self {
@@ -735,20 +742,16 @@ impl Format2Rule<'_> {
735742 input_class_def : & ClassDef ,
736743 backtrack_class_def : Option < & ClassDef > ,
737744 lookahead_class_def : Option < & ClassDef > ,
738- input_cache : & mut FnvHashMap < u16 , bool > ,
739- backtrack_cache : & mut FnvHashMap < u16 , bool > ,
740- lookahead_cache : & mut FnvHashMap < u16 , bool > ,
745+ seq_cache : & mut SeqCache ,
741746 ) -> bool {
742747 match self {
743- Self :: Plain ( table) => table. intersects ( glyphs, input_class_def, input_cache ) ,
748+ Self :: Plain ( table) => table. intersects ( glyphs, input_class_def, & mut seq_cache . input ) ,
744749 Self :: Chain ( table) => table. intersects (
745750 glyphs,
746751 input_class_def,
747752 backtrack_class_def,
748753 lookahead_class_def,
749- input_cache,
750- backtrack_cache,
751- lookahead_cache,
754+ seq_cache,
752755 ) ,
753756 }
754757 }
@@ -760,13 +763,9 @@ fn intersects_class(
760763 class : u16 ,
761764 cache : & mut FnvHashMap < u16 , bool > ,
762765) -> bool {
763- if let Some ( v) = cache. get ( & class) {
764- return * v;
765- }
766-
767- let v = class_def. intersects_class_glyphs ( glyphs, class) ;
768- cache. insert ( class, v) ;
769- v
766+ * cache
767+ . entry ( class)
768+ . or_insert_with ( || class_def. intersects_class_glyphs ( glyphs, class) )
770769}
771770impl ClassSequenceRule < ' _ > {
772771 fn intersects (
@@ -789,36 +788,40 @@ impl ChainedClassSequenceRule<'_> {
789788 input_class_def : & ClassDef ,
790789 backtrack_class_def : Option < & ClassDef > ,
791790 lookahead_class_def : Option < & ClassDef > ,
792- input_cache : & mut FnvHashMap < u16 , bool > ,
793- backtrack_cache : & mut FnvHashMap < u16 , bool > ,
794- lookahead_cache : & mut FnvHashMap < u16 , bool > ,
791+ seq_cache : & mut SeqCache ,
795792 ) -> bool {
796793 if !self
797794 . input_sequence ( )
798795 . iter ( )
799- . all ( |c| intersects_class ( input_class_def, glyphs, c. get ( ) , input_cache ) )
796+ . all ( |c| intersects_class ( input_class_def, glyphs, c. get ( ) , & mut seq_cache . input ) )
800797 {
801798 return false ;
802799 }
803800
804801 if let Some ( backtrack_class_def) = backtrack_class_def {
805- if !self
806- . backtrack_sequence ( )
807- . iter ( )
808- . all ( |c| intersects_class ( backtrack_class_def, glyphs, c. get ( ) , backtrack_cache) )
809- {
802+ if !self . backtrack_sequence ( ) . iter ( ) . all ( |c| {
803+ intersects_class (
804+ backtrack_class_def,
805+ glyphs,
806+ c. get ( ) ,
807+ & mut seq_cache. backtrack ,
808+ )
809+ } ) {
810810 return false ;
811811 }
812812 } else if self . backtrack_glyph_count ( ) != 0 {
813813 return false ;
814814 }
815815
816816 if let Some ( lookahead_class_def) = lookahead_class_def {
817- if !self
818- . lookahead_sequence ( )
819- . iter ( )
820- . all ( |c| intersects_class ( lookahead_class_def, glyphs, c. get ( ) , lookahead_cache) )
821- {
817+ if !self . lookahead_sequence ( ) . iter ( ) . all ( |c| {
818+ intersects_class (
819+ lookahead_class_def,
820+ glyphs,
821+ c. get ( ) ,
822+ & mut seq_cache. lookahead ,
823+ )
824+ } ) {
822825 return false ;
823826 }
824827 } else if self . lookahead_glyph_count ( ) != 0 {
@@ -961,9 +964,7 @@ impl Intersect for ContextFormat2<'_> {
961964 }
962965 } ;
963966
964- let mut input_cache = FnvHashMap :: default ( ) ;
965- let mut backtrack_cache = FnvHashMap :: default ( ) ;
966- let mut lookahead_cache = FnvHashMap :: default ( ) ;
967+ let mut seq_cache = SeqCache :: default ( ) ;
967968 for rule_set in self . rule_sets ( ) . enumerate ( ) . filter_map ( |( c, rule_set) | {
968969 input_class_def
969970 . intersects_class_glyphs ( & retained_coverage_glyphs, c as u16 )
@@ -979,9 +980,7 @@ impl Intersect for ContextFormat2<'_> {
979980 & input_class_def,
980981 backtrack_class_def. as_ref ( ) ,
981982 lookahead_class_def. as_ref ( ) ,
982- & mut input_cache,
983- & mut backtrack_cache,
984- & mut lookahead_cache,
983+ & mut seq_cache,
985984 ) {
986985 return Ok ( true ) ;
987986 }
@@ -1025,9 +1024,7 @@ impl LookupClosure for ContextFormat2<'_> {
10251024 }
10261025 } ;
10271026
1028- let mut input_cache = FnvHashMap :: default ( ) ;
1029- let mut backtrack_cache = FnvHashMap :: default ( ) ;
1030- let mut lookahead_cache = FnvHashMap :: default ( ) ;
1027+ let mut seq_cache = SeqCache :: default ( ) ;
10311028 for rule_set in self . rule_sets ( ) . enumerate ( ) . filter_map ( |( c, rule_set) | {
10321029 input_class_def
10331030 . intersects_class_glyphs ( & retained_coverage_glyphs, c as u16 )
@@ -1051,9 +1048,7 @@ impl LookupClosure for ContextFormat2<'_> {
10511048 & input_class_def,
10521049 backtrack_class_def. as_ref ( ) ,
10531050 lookahead_class_def. as_ref ( ) ,
1054- & mut input_cache,
1055- & mut backtrack_cache,
1056- & mut lookahead_cache,
1051+ & mut seq_cache,
10571052 ) {
10581053 continue ;
10591054 }
0 commit comments