0

Use [[likely]] and [[unlikely]] in //chrome/

as discussed in this thread: https://groups.google.com/a/chromium.org/g/cxx/c/bk9YC5qSDF8/m/LLzo7uS8AgAJ?utm_medium=email&utm_source=footer

Bug: 40256217
Change-Id: I99b56f03952457dfb6ba9cc2f2fe9aac3698efbd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5496820
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Commit-Queue: Punith Nayak <punithbnayak@chromium.org>
Reviewed-by: Avi Drissman <avi@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1294753}
This commit is contained in:
punithbnayak
2024-05-01 03:17:35 +00:00
committed by Chromium LUCI CQ
parent 55d4135900
commit dda49e5dd9
7 changed files with 21 additions and 29 deletions

@ -1332,15 +1332,6 @@ _BANNED_CPP_FUNCTIONS : Sequence[BanRule] = (
True,
[_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders.
),
BanRule(
r'/\[\[(un)?likely\]\]',
(
'[[likely]] and [[unlikely]] are not yet allowed ',
'(https://crbug.com/1414620). Use [UN]LIKELY instead.',
),
True,
[_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders.
),
BanRule(
r'/\[\[(\w*::)?no_unique_address\]\]',
(

@ -90,7 +90,7 @@ void AXMediaAppUntrustedHandler::OnOCRServiceInitialized(bool successful) {
if (!dirty_page_ids_.empty()) {
OcrNextDirtyPageIfAny();
}
if (UNLIKELY(media_app_)) {
if (media_app_) [[unlikely]] {
// `media_app_` is only used for testing.
CHECK_IS_TEST();
media_app_->OcrServiceEnabledChanged(true);
@ -128,7 +128,7 @@ void AXMediaAppUntrustedHandler::PerformAction(
ax::mojom::IntAttribute::kScrollYMin));
viewport_box_.set_y(
std::max(viewport_box_.y() - viewport_box_.height(), y_min));
if (UNLIKELY(media_app_)) {
if (media_app_) [[unlikely]] {
// `media_app_` is only used for testing.
CHECK_IS_TEST();
media_app_->SetViewport(viewport_box_);
@ -143,7 +143,7 @@ void AXMediaAppUntrustedHandler::PerformAction(
ax::mojom::IntAttribute::kScrollYMax));
viewport_box_.set_y(
std::min(viewport_box_.y() + viewport_box_.height(), y_max));
if (UNLIKELY(media_app_)) {
if (media_app_) [[unlikely]] {
// `media_app_` is only used for testing.
CHECK_IS_TEST();
media_app_->SetViewport(viewport_box_);
@ -157,7 +157,7 @@ void AXMediaAppUntrustedHandler::PerformAction(
ax::mojom::IntAttribute::kScrollXMin));
viewport_box_.set_x(
std::max(viewport_box_.x() - viewport_box_.width(), x_min));
if (UNLIKELY(media_app_)) {
if (media_app_) [[unlikely]] {
// `media_app_` is only used for testing.
CHECK_IS_TEST();
media_app_->SetViewport(viewport_box_);
@ -171,7 +171,7 @@ void AXMediaAppUntrustedHandler::PerformAction(
ax::mojom::IntAttribute::kScrollXMax));
viewport_box_.set_x(
std::min(viewport_box_.x() + viewport_box_.width(), x_max));
if (UNLIKELY(media_app_)) {
if (media_app_) [[unlikely]] {
// `media_app_` is only used for testing.
CHECK_IS_TEST();
media_app_->SetViewport(viewport_box_);
@ -224,7 +224,7 @@ void AXMediaAppUntrustedHandler::PerformAction(
}
break;
}
if (UNLIKELY(media_app_)) {
if (media_app_) [[unlikely]] {
// `media_app_` is only used for testing.
CHECK_IS_TEST();
media_app_->SetViewport(viewport_box_);
@ -266,7 +266,7 @@ void AXMediaAppUntrustedHandler::PerformAction(
}
void AXMediaAppUntrustedHandler::OnAXModeAdded(ui::AXMode mode) {
if (UNLIKELY(media_app_)) {
if (media_app_) [[unlikely]] {
// `media_app_` is only used for testing.
CHECK_IS_TEST();
media_app_->AccessibilityEnabledChanged(
@ -652,7 +652,7 @@ void AXMediaAppUntrustedHandler::OcrNextDirtyPageIfAny() {
const std::string dirty_page_id = PopDirtyPage();
// TODO(b/289012145): Refactor this code to support things happening
// asynchronously - i.e. `RequestBitmap` will be async.
if (UNLIKELY(media_app_)) {
if (media_app_) [[unlikely]] {
// `media_app_` is only used for testing.
CHECK_IS_TEST();
SkBitmap page_bitmap = media_app_->RequestBitmap(dirty_page_id);

@ -29,7 +29,7 @@ TpmChallengeKey* TpmChallengeKeyFactory::next_result_for_testing_ = nullptr;
// static
std::unique_ptr<TpmChallengeKey> TpmChallengeKeyFactory::Create() {
if (UNLIKELY(next_result_for_testing_)) {
if (next_result_for_testing_) [[unlikely]] {
std::unique_ptr<TpmChallengeKey> result(next_result_for_testing_);
next_result_for_testing_ = nullptr;
return result;

@ -56,7 +56,7 @@ TpmChallengeKeySubtle* TpmChallengeKeySubtleFactory::next_result_for_testing_ =
// static
std::unique_ptr<TpmChallengeKeySubtle> TpmChallengeKeySubtleFactory::Create() {
if (UNLIKELY(next_result_for_testing_)) {
if (next_result_for_testing_) [[unlikely]] {
std::unique_ptr<TpmChallengeKeySubtle> result(next_result_for_testing_);
next_result_for_testing_ = nullptr;
return result;

@ -29,7 +29,7 @@ CertProvisioningWorkerFactory* CertProvisioningWorkerFactory::test_factory_ =
// static
CertProvisioningWorkerFactory* CertProvisioningWorkerFactory::Get() {
if (UNLIKELY(test_factory_)) {
if (test_factory_) [[unlikely]] {
return test_factory_;
}

@ -910,7 +910,7 @@ void ExtensionPlatformKeysService::GenerateRSAKey(
GenerateKeyCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (UNLIKELY(!keystore_service_)) {
if (!keystore_service_) [[unlikely]] {
std::move(callback).Run(/*public_key_spki_der=*/std::vector<uint8_t>(),
crosapi::mojom::KeystoreError::kMojoUnavailable);
return;
@ -946,7 +946,7 @@ void ExtensionPlatformKeysService::GenerateECKey(
GenerateKeyCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (UNLIKELY(!keystore_service_)) {
if (!keystore_service_) [[unlikely]] {
std::move(callback).Run(/*public_key_spki_der=*/std::vector<uint8_t>(),
crosapi::mojom::KeystoreError::kMojoUnavailable);
return;
@ -978,7 +978,7 @@ void ExtensionPlatformKeysService::SignDigest(
SignCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (UNLIKELY(!keystore_service_)) {
if (!keystore_service_) [[unlikely]] {
std::move(callback).Run(/*signature=*/std::vector<uint8_t>(),
crosapi::mojom::KeystoreError::kMojoUnavailable);
return;
@ -997,7 +997,7 @@ void ExtensionPlatformKeysService::SignRSAPKCS1Raw(
SignCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (UNLIKELY(!keystore_service_)) {
if (!keystore_service_) [[unlikely]] {
std::move(callback).Run(/*signature=*/std::vector<uint8_t>(),
crosapi::mojom::KeystoreError::kMojoUnavailable);
return;
@ -1019,7 +1019,7 @@ void ExtensionPlatformKeysService::SelectClientCertificates(
content::WebContents* web_contents) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (UNLIKELY(!keystore_service_)) {
if (!keystore_service_) [[unlikely]] {
std::move(callback).Run(/*matches=*/nullptr,
crosapi::mojom::KeystoreError::kMojoUnavailable);
return;

@ -95,7 +95,7 @@ int IdentifiabilityStudyState::generation() const {
bool IdentifiabilityStudyState::ShouldRecordSurface(
blink::IdentifiableSurface surface) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (LIKELY(!settings_.enabled() && !meta_experiment_active_)) {
if (!settings_.enabled() && !meta_experiment_active_) [[unlikely]] {
return false;
}
@ -103,7 +103,7 @@ bool IdentifiabilityStudyState::ShouldRecordSurface(
if (surface.GetType() == blink::IdentifiableSurface::Type::kReservedInternal)
return true;
if (LIKELY(!settings_.enabled())) {
if (!settings_.enabled()) [[unlikely]] {
return false;
}
@ -165,8 +165,9 @@ void IdentifiabilityStudyState::InitializeRenderer(
bool IdentifiabilityStudyState::DecideInclusionForNewSurface(
blink::IdentifiableSurface new_surface) {
DCHECK(settings_.IsUsingRandomSampling());
if (UNLIKELY(seen_surfaces_.size() > kMaxSelectedSurfaceOffset + 1))
if (seen_surfaces_.size() > kMaxSelectedSurfaceOffset + 1) [[unlikely]] {
return false;
}
MaybeUpdateSelectedOffsets();
@ -547,7 +548,7 @@ bool IdentifiabilityStudyState::IsMetaExperimentActive() {
}
void IdentifiabilityStudyState::InitFromPrefs() {
if (LIKELY(!settings_.enabled())) {
if (!settings_.enabled()) [[unlikely]] {
// Nothing to do if the study is not active. However it is possible that
// this client has switched from active to inactive, in which case we should
// nuke any persisted data.