Skip to content

Commit 6662aed

Browse files
[PERF] Optimize 3rd heaviest func, (81b -> 10m) (#15043)
So this is funny, the query `tcx.module_children` was top 3 in most time consuming functions in Clippy, it was being called 24384 times in tokio. "Unacceptable!" I thought. Digging a bit around, turns out that `clippy::strlen_on_c_strings` was calling for `get_def_path` via `match_libc_symbol`. This query pretty-prints things and performs some analysis. Yes, we were running early lint checks to see if symbols were from `libc`. I don't really trust callgrind when it says I've turn 81 billion instructions into like 10 million. So I benchmarked this the good ol' "compiling 20 times without incr" method and it went from 0.31s-0.45s to 0.25s constistently. (Profiled, and "benchmarked") on tokio. What I can get behind is via `strlen_on_c_strings` changing from 31 million instructions into 76k. 🎉 🥳 changelog: [`strlen_on_c_strings`]: Optimize it by 99.75%
2 parents 59d8345 + 6cd55b9 commit 6662aed

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

clippy_utils/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,10 +1793,9 @@ pub fn in_automatically_derived(tcx: TyCtxt<'_>, id: HirId) -> bool {
17931793

17941794
/// Checks if the given `DefId` matches the `libc` item.
17951795
pub fn match_libc_symbol(cx: &LateContext<'_>, did: DefId, name: Symbol) -> bool {
1796-
let path = cx.get_def_path(did);
17971796
// libc is meant to be used as a flat list of names, but they're all actually defined in different
17981797
// modules based on the target platform. Ignore everything but crate name and the item name.
1799-
path.first().is_some_and(|s| *s == sym::libc) && path.last().copied() == Some(name)
1798+
cx.tcx.crate_name(did.krate) == sym::libc && cx.tcx.def_path_str(did).ends_with(name.as_str())
18001799
}
18011800

18021801
/// Returns the list of condition expressions and the list of blocks in a

0 commit comments

Comments
 (0)