3

This is a common question for other compilers (C#, VC++, GCC.) I would like to know the same thing for the Delphi compiler (any version; I'm currently using 2010 and XE2 and will use XE4 soon.)

I have a situation in high-performance code I'm writing where a condition has to be checked, but in most cases no action needs to be taken:

if UnlikelyCondition then
  HandleUnlikelyCondition
else
  HandleLikelyCondition
end;

Often nothing needs to be done for the likely case:

if UnlikelyCondition then
  HandleUnlikelyCondition
else
  Exit
end;

I would like to hint to the compiler that the second branch of the if statement is the one to optimize for. How can I do this in Delphi?

Current code

Currently, I have written my code assuming that the if statement's condition equalling true is the best thing to optimise for:

if LikelyCondition then
  HandleLikelyCondition
else
  HandleUnlikelyCondition
end;

or

if LikelyCondition then Exit;
HandleUnlikelyCondition;

In a test just now using the first of these two examples, I get a 50% extra performance boost restructuring my if statements like this, ie assuming the if statement's condition is true. Perhaps another way of phrasing the question is, is this the best I can do?

If you have not encountered branch misprediction before, this epic answer is an illuminating read.

Community
  • 1
  • 1
David
  • 13,360
  • 7
  • 66
  • 130
  • Note that this won't be about branch (mis)prediction... – Oliver Charlesworth Jul 06 '13 at 14:37
  • @OliCharlesworth Could you explain please? – David Jul 06 '13 at 14:37
  • 2
    So far as I'm aware, GCC's `__builtin_expect` (and similar) don't directly control the branch predictor (I'm not sure that's possible on modern x86), they just alter the generated code in favour of the "likely" path, such that it requires fewer instructions. – Oliver Charlesworth Jul 06 '13 at 14:46
  • 2
    I don't believe any such thing exists for the Delphi compiler. – Nick Hodges Jul 06 '13 at 16:55
  • Borland compilers suck at optimzation nowadays :( You have to write likely branch code after `then` and unlikely one after `else` (which in most cases is good code style anyways) – OnTheFly Jul 07 '13 at 04:37
  • @user539484: Not all of them - I believe some of them are based on LLVM under the hood and are really quite good. Unfortunately the stock Win32 compilers haven't been migrated to this new architecture (yet; they will be, afaik.) – David Jul 07 '13 at 13:01

1 Answers1

1

There is nothing in the language or compiler that allows you to supply hints for branch prediction. And in any case, modern architectures would ignore those hints even if the compiler emitted object code that contained hints.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thanks David. Are you sure that modern architectures would ignore the hints? The other questions on SO seem fairly sure there is a benefit to hinting - at least hinting to the compiler, so that it can optimise the code so the internal prediction is more accurate. And, would you say that the current code I'm using is the best approach towards optimising this? – David Jul 06 '13 at 17:35
  • The other questions didn't say that. They say that P4 has hinted jump opcodes but that the P4 ignores them. And modern branch predictors or even stronger, with no hinting. – David Heffernan Jul 06 '13 at 19:20
  • I was referring to (this question)[http://stackoverflow.com/questions/3702903/portable-branch-prediction-hints/3702965#3702965] where the 'true' branch is the one to assume runs. The compiler could reorganise code, perhaps. Still, thanks for your answer (and @NickHodges too) - good to have it confirmed! – David Jul 07 '13 at 13:03
  • 1
    @David: Note that compile time branch hints [don't actually turn into runtime hints to the CPU branch predictor](https://stackoverflow.com/questions/30130930/is-there-a-compiler-hint-for-gcc-to-force-branch-prediction-to-always-go-a-certa/30136196#30136196), they just affect function layout and choice of branchless (cmov) vs. branchy. [How do the likely/unlikely macros in the Linux kernel work and what is their benefit?](https://stackoverflow.com/a/31133787) shows example code-gen. Not-taken branches disrupt the pipeline less than taken, even when both are correctly predicted. – Peter Cordes Jun 12 '21 at 00:59