Skip to content

Commit 21d83a3

Browse files
committed
speed up checks
1 parent 4f88e03 commit 21d83a3

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/input/input_python.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,21 @@ fn get_ordered_dict_type(py: Python<'_>) -> &Bound<'_, PyType> {
6262
.bind(py)
6363
}
6464

65-
// Lazy helper that only initializes OrderedDict type when actually needed
65+
// Ultra-fast OrderedDict detection with multiple optimization layers
6666
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
6873
if let Ok(type_name) = obj.get_type().name() {
6974
if type_name.to_string() != "OrderedDict" {
70-
return false; // Fast path for non-OrderedDict objects
75+
return false; // Not OrderedDict based on name
7176
}
7277
}
7378

74-
// Only now do we need the expensive type lookup
79+
// SLOW PATH: Only for actual OrderedDict objects - expensive type lookup
7580
let ordered_dict_type = get_ordered_dict_type(obj.py());
7681
obj.is_instance(ordered_dict_type).unwrap_or(false)
7782
}
@@ -427,7 +432,6 @@ impl<'py> Input<'py> for Bound<'py, PyAny> {
427432
}
428433

429434
fn lax_dict<'a>(&'a self) -> ValResult<GenericPyMapping<'a, 'py>> {
430-
// Optimized: Only check for OrderedDict when needed
431435
if check_if_ordered_dict(self) {
432436
// OrderedDict is a subclass of dict, but we want to treat it as a mapping to preserve order
433437
if let Ok(mapping) = self.downcast::<PyMapping>() {

0 commit comments

Comments
 (0)