Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improve call to Path::ends_with #199

Merged
merged 1 commit into from
Jun 29, 2024
Merged

perf: improve call to Path::ends_with #199

merged 1 commit into from
Jun 29, 2024

Conversation

Boshen
Copy link
Member

@Boshen Boshen commented Jun 29, 2024

Path::ends_with converts the comparator to a Path and compares all components.

    #[must_use]
    pub fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool {
        self._ends_with(child.as_ref())
    }

    fn _ends_with(&self, child: &Path) -> bool {
        iter_after(self.components().rev(), child.components().rev()).is_some()
    }

module_name in cached_path.path().ends_with(module_name) is guaranteed to be a single component so this overhead can be avoided.

I also changed the order of comparisons to check node_modules because that's the majority of the case.

Copy link

codspeed-hq bot commented Jun 29, 2024

CodSpeed Performance Report

Merging #199 will not alter performance

Comparing perf-ends-with (1e02488) with main (94efa90)

Summary

✅ 2 untouched benchmarks

Copy link

codecov bot commented Jun 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.89%. Comparing base (94efa90) to head (be1c77c).

Current head be1c77c differs from pull request most recent head 1e02488

Please upload reports for the commit 1e02488 to get more accurate results.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #199   +/-   ##
=======================================
  Coverage   96.89%   96.89%           
=======================================
  Files          11       11           
  Lines        2283     2284    +1     
=======================================
+ Hits         2212     2213    +1     
  Misses         71       71           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

`Path::ends_with` converts the comparator to a `Path` and compares all components.

`module_name` in `cached_path.path().ends_with(module_name)` is
guaranteed to be a single component so this overhead can be avoided.

```rust
    #[must_use]
    pub fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool {
        self._ends_with(child.as_ref())
    }

    fn _ends_with(&self, child: &Path) -> bool {
        iter_after(self.components().rev(), child.components().rev()).is_some()
    }
```
@Boshen Boshen merged commit 9dd2903 into main Jun 29, 2024
18 checks passed
@Boshen Boshen deleted the perf-ends-with branch June 29, 2024 06:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant