Skip to content

Performance: Replace fold with find_map in locator identification loop #284

Description

Summary

The locator identification loop in locators.rs uses a fold pattern that continues iterating through all locators even after a match is found. This is inefficient and should be replaced with find_map which short-circuits on the first match.

Current Implementation

File: crates/pet/src/locators.rs (lines 106-109 and 120-123)

if let Some(env) = locators.iter().fold(
    None,
    |e, loc| if e.is_some() { e } else { loc.try_from(env) },
) {
    return Some(env);
}

Proposed Change

if let Some(env) = locators.iter().find_map(|loc| loc.try_from(env)) {
    return Some(env);
}

Impact

  • CPU savings: Avoids unnecessary iterations after a match is found
  • Cleaner code: find_map is more idiomatic for this use case
  • Both occurrences in identify_python_environment_using_locators should be updated

Priority

High - This is called frequently during environment discovery and is an easy win.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

debtCode quality issues

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions