@@ -62,16 +62,21 @@ fn get_ordered_dict_type(py: Python<'_>) -> &Bound<'_, PyType> {
62
62
. bind ( py)
63
63
}
64
64
65
- // Lazy helper that only initializes OrderedDict type when actually needed
65
+ // Ultra-fast OrderedDict detection with multiple optimization layers
66
66
fn check_if_ordered_dict ( obj : & Bound < ' _ , PyAny > ) -> bool {
67
- // Quick type name check first - avoid Python import if possible
67
+ // FASTEST PATH: Check if it's exact PyDict first - skip everything for regular dicts
68
+ if obj. is_exact_instance_of :: < PyDict > ( ) {
69
+ return false ; // Regular dict - absolutely not OrderedDict
70
+ }
71
+
72
+ // FAST PATH: Quick type name check - avoid Python import if possible
68
73
if let Ok ( type_name) = obj. get_type ( ) . name ( ) {
69
74
if type_name. to_string ( ) != "OrderedDict" {
70
- return false ; // Fast path for non-OrderedDict objects
75
+ return false ; // Not OrderedDict based on name
71
76
}
72
77
}
73
78
74
- // Only now do we need the expensive type lookup
79
+ // SLOW PATH: Only for actual OrderedDict objects - expensive type lookup
75
80
let ordered_dict_type = get_ordered_dict_type ( obj. py ( ) ) ;
76
81
obj. is_instance ( ordered_dict_type) . unwrap_or ( false )
77
82
}
@@ -427,7 +432,6 @@ impl<'py> Input<'py> for Bound<'py, PyAny> {
427
432
}
428
433
429
434
fn lax_dict < ' a > ( & ' a self ) -> ValResult < GenericPyMapping < ' a , ' py > > {
430
- // Optimized: Only check for OrderedDict when needed
431
435
if check_if_ordered_dict ( self ) {
432
436
// OrderedDict is a subclass of dict, but we want to treat it as a mapping to preserve order
433
437
if let Ok ( mapping) = self . downcast :: < PyMapping > ( ) {
0 commit comments