Is it possible to hint a specific if-statement branch is most likely to be executed in the Delphi compiler?

StackOverflow https://stackoverflow.com/questions/17503927

문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top