Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

> for an extra branch on getting the size

Modern CPUs have branch prediction.

The predictors work by caching which branches were taken, and assuming the same outcome is likely to happen next time the branch at that address is checked. That particular branch is checked all the time, and unless you actually have >2GB strings the result is always the same.

Only branches which dynamic runtime behavior are bad for performance, like binary search when the same branch is taken/not taken randomly, depending on the data and the key being searched. Branches with stable runtime behavior are OK thanks to branch prediction. Examples of good branches: `while( i < 10000 )`, `if( string_length < INT_MAX )`



view as:

In isolation, yes. Predictor resources are not infinite. Predictable is better than dynamic, but no branch is optimal.

Consider the implications to inlining: the size getter is a prime candidate, but suddenly it gets less predictor-friendly.

Edit: this is naturally a good case for manual hinting.


Legal | privacy