Closed Bug 1585585 Opened 6 years ago Closed 6 years ago

Add MOZ_UNLIKELY to if branch in MOZ_TRY macro

Categories

(Core :: MFBT, enhancement, P3)

enhancement

Tracking

()

RESOLVED FIXED
mozilla71
Tracking Status
firefox71 --- fixed

People

(Reporter: arai, Assigned: arai)

Details

Attachments

(1 file, 1 obsolete file)

https://searchfox.org/mozilla-central/rev/f372e8a46ef7659ef61be9938ec2a3ea34d343c6/mfbt/Result.h#502-508

#define MOZ_TRY(expr)                                       \
  do {                                                      \
    auto mozTryTempResult_ = ::mozilla::ToResult(expr);     \
    if (mozTryTempResult_.isErr()) {                        \
      return ::mozilla::Err(mozTryTempResult_.unwrapErr()); \
    }                                                       \
  } while (0)

IMO, the isErr() case is almost always error case and not-hot code path.
adding MOZ_UNLIKELY should improve the generated code.

in BinAST parser (that heavily used MOZ_TRY macro and variants), adding MOZ_UNLIKELY notably improves the performance (~3% speed up with parsing 200 files)

Impressive!

Maybe we should also add it to other cases in Result.h, like all those isOk() ? x : y lines.

Or perhaps isOk() itself could return MOZ_LIKELY(...) instead of .... It does not seem unreasonable to me to impose upon Result the intended semantic that the success case be normal and errors be irregular.

(And of course isErr() would return MOZ_UNLIKELY(...), and whatever other relevant functions that could use similar treatment would get it.)

(In reply to Tooru Fujisawa [:arai] from comment #0)

in BinAST parser (that heavily used MOZ_TRY macro and variants), adding MOZ_UNLIKELY notably improves the performance (~3% speed up with parsing 200 files)

That'd be a case for adding something that uses the BinAST parser to the PGO profile.

bool isOk() const { return MOZ_LIKELY(mImpl.isOk()); }
...
bool isErr() const { return MOZ_UNLIKELY(!mImpl.isOk()); }

Does clang know how to optimize the caller code when a function returns MOZ_LIKELY or MOZ_UNLIKELY? Do you still see the same ~3% speed up as you do when using MOZ_UNLIKELY in the MOZ_TRY macro directly?

(In reply to Chris Peterson [:cpeterson] from comment #6)

bool isOk() const { return MOZ_LIKELY(mImpl.isOk()); }
...
bool isErr() const { return MOZ_UNLIKELY(!mImpl.isOk()); }

Does clang know how to optimize the caller code when a function returns MOZ_LIKELY or MOZ_UNLIKELY? Do you still see the same ~3% speed up as you do when using MOZ_UNLIKELY in the MOZ_TRY macro directly?

thanks for pointing out. you're right. it doesn't work as I expect.

will post fixed one.

Attachment #9098439 - Attachment is obsolete: true
Pushed by arai_a@mac.com: https://hg.mozilla.org/integration/autoland/rev/01862196080a Add {MOZ_LIKELY,MOZ_UNLIKELY} to Result::{isOk,isErr} consumers. r=jwalden
Status: ASSIGNED → RESOLVED
Closed: 6 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla71
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: