Skip to content

[Clang] Optimize -Wunsafe-buffer-usage. #124554

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

Closed
wants to merge 1 commit into from

Conversation

ivanaivanovska
Copy link
Contributor

@ivanaivanovska ivanaivanovska commented Jan 27, 2025

The Clang disgnostic -Wunsafe-buffer-usage was adding up to +15% compilation time when used. Profiling showed that most of the overhead comes from the use of ASTMatchers.

This change replaces the ASTMatcher infrastructure with simple matching functions and keeps the functionality unchanged. It reduces the overhead added by -Wunsafe-buffer-usage by 87.8%, leaving a negligible additional compilation time of 1.7% when the diagnostic is used.

Old version without -Wunsafe-buffer-usage:

$ hyperfine -i -w 1 --runs 5 '/tmp/old_clang -c -std=c++20 /tmp/preprocessed.cc -ferror-limit=20'
Benchmark 1: /tmp/old_clang -c -std=c++20 /tmp/preprocessed.cc -ferror-limit=20
  Time (mean ± σ):     231.035 s ±  3.210 s    [User: 229.134 s, System: 1.704 s]
  Range (min … max):   228.751 s … 236.682 s    5 runs

Old version with -Wunsafe-buffer-usage:

$ hyperfine -i -w 1 --runs 10 '/tmp/old_clang -c -std=c++20 /tmp/preprocessed.cc -ferror-limit=20 -Wunsafe-buffer-usage'
Benchmark 1: /tmp/old_clang -c -std=c++20 /tmp/preprocessed.cc -ferror-limit=20 -Wunsafe-buffer-usage
  Time (mean ± σ):     263.840 s ±  0.854 s    [User: 262.043 s, System: 1.575 s]
  Range (min … max):   262.442 s … 265.142 s    10 runs

New version with -Wunsafe-buffer-usage:

$ hyperfine -i -w 1 --runs 10 '/tmp/new_clang -c -std=c++20 /tmp/preprocessed.cc -ferror-limit=20 -Wunsafe-buffer-usage'
Benchmark 1: /tmp/new_clang -c -std=c++20 /tmp/preprocessed.cc -ferror-limit=20 -Wunsafe-buffer-usage
  Time (mean ± σ):     235.169 s ±  1.408 s    [User: 233.406 s, System: 1.561 s]
  Range (min … max):   232.221 s … 236.792 s    10 runs

Copy link
Contributor

@ilya-biryukov ilya-biryukov left a comment

Choose a reason for hiding this comment

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

Thanks a lot for this!

I have left quite a few comments, mostly NITs.
A few things that really stood out and I think we should fix:

  • increasing the number of macros and signatures we use seems like a bad trade-off. It's much better to have more parameters and ignore them in most cases, but only have a single macro to deal with as a return. The lower complexity of the code using the macros is really something we should optimize for.
  • the code is using the ASTMatchers terminology despite switching away from it. I find it to be causing more confusion than good. We are really replacing matchers with AST traversal, so I left a few suggestions that go in the direction of switching from
  • some code has been moved around, making it a little hard to diff between the original and updated version. I suspect it would not compile otherwise, but I am happy to help find workarounds to keep the code compiling and keep every single function and class that reimplemented the original code at the same place. I think this should really help to ensure the code is equivalent and will make it easier to review.

@ivanaivanovska ivanaivanovska changed the title Optimize -Wunsafe-buffer-usage. [Clang] Optimize -Wunsafe-buffer-usage. Jan 29, 2025
Copy link
Contributor

@ilya-biryukov ilya-biryukov left a comment

Choose a reason for hiding this comment

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

I only have a few suggestions left, overall this LG.

Could you also add a description for the PR that explains the motivation and approach taken for the rewrite?
Benchmark numbers would also be needed there.

Thanks!

Copy link
Contributor

@ilya-biryukov ilya-biryukov left a comment

Choose a reason for hiding this comment

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

This generally looks good, just one more minor comment from me.
Could we put this out of the draft state and send to upstream owners of the check to start a discussion about landing it?

DeclUseTracker &Tracker;
};
public:
FixableGadgetList &FixableGadgets;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it really necessary to make these members public?
They only seem to be used for reporting results internally, or am I missing something?

@ilya-biryukov
Copy link
Contributor

FYI: this PR was taking too long to pick changes after rebase, so the discussions continued in #125492.

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.

2 participants