3 ms·
You would be trading 4 bytes for an extra branch on getting the size. Since the instance needs 8-byte alignment, those 4 bytes would be lost in padding or alloc
by vnorilo 7y ago
You would be trading 4 bytes for an extra branch on getting the size. Since the instance needs 8-byte alignment, those 4 bytes would be lost in padding or allocation overhead anyway.
- Const-me 7y ago> 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 )`
- vnorilo 7y agoIn 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.