Skip to content

perf:cache FFI safety results in improper_ctypes lint - #163348

Open
xonx4l wants to merge 2 commits into
rust-lang:mainfrom
xonx4l:improper_ctypes-lint-cache
Open

xonx4l wants to merge 2 commits into
rust-lang:mainfrom
xonx4l:improper_ctypes-lint-cache

Conversation

@xonx4l

@xonx4l xonx4l commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

This PR cache FFI safety results in improper_ctypes lint .

ImproperCTypesVisitor's cache field is recreated empty at every call site, so a struct type gets fully re-walked from scratch each time instead of once .

The implementation here moves the cache onto ImproperCtypesLint, so it remembers types we already checked and found FFI-safe, so we don't check the same type again every time it shows up in another function. We only remember "safe" types, never "unsafe" ones. That way we never need to keep the actual type around, just a fingerprint of it.Thus it persists across a whole module worth of foreign items.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 25, 2026
@rustbot

rustbot commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 77 candidates
  • Random selection from 19 candidates

@rust-log-analyzer

This comment has been minimized.

@nnethercote

Copy link
Copy Markdown
Contributor

What motivated this change? Do you have any measurements showing that it improves compile times?

@nnethercote nnethercote added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 27, 2026
@xonx4l

xonx4l commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor Author

The motivation for the change is that the cache is recreated empty at every call site, so a struct shared across many extern functions gets fully re-checked from scratch each time instead of once.

For measurement I profiled a test file containing some extern "C" functions sharing a few structs with cachegrind :).

Before: 2,000,537,577 instructions count , After: 1,753,411,352 instructions count

Which gives approx a total of 12% of instructions count reduction. Thank you!

let ffi_res = visitor.check_type(state, ty);
if matches!(ffi_res, FfiResult::FfiSafe) {
self.known_safe.borrow_mut().insert(key);
}

@nnethercote nnethercote Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unfortunate to have this contains + "do stuff" + insert pattern repeated three times. Can you factor it out into a separate method? It might need a closure argument for the "do stuff" part.

@rustbot author

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will make it a separate method . Thank you!

@rustbot

rustbot commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

This branch has not been deployed

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

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants