Skip to content

Conversation

@TimothyMakkison
Copy link
Contributor

@TimothyMakkison TimothyMakkison commented Oct 18, 2025

1 width characters are between 0 and 0x1100, uses ContainsAnyExceptInRange to do a vectorised check for characters outside this range. If they are all within the range the width is the exact same as the string length, otherwise we fallback to the old manual loop.

I've deliberately outlined CalculateActualWidth to avoid the cost of jitting and loading the function each time, this does make a small improvement.

Benchmarks

This can be tricky, I opted to collect each string that is normally passed to GetPrintedWidth and use that as the benchmark data. I stored it in a dictionary and then recreate the data and shuffle it to be realistic. Turns out if you don't shuffle the data and just have a series of identical ordered text the branch predictor makes the manual foreach loop the fastest.
See #1712 for code

Complex

Method Mean Error StdDev Median
ForEachLoop 5.642 ms 0.1128 ms 0.2476 ms 5.534 ms
Vector 4.746 ms 0.0890 ms 0.1694 ms 4.685 ms
VectorOutline 4.531 ms 0.0900 ms 0.1994 ms 4.437 ms

Tests

Method Mean Error StdDev
ForEachLoop 2.231 ms 0.0445 ms 0.1092 ms
Vector 1.895 ms 0.0378 ms 0.0478 ms
VectorOutline 1.829 ms 0.0362 ms 0.0541 ms

@TimothyMakkison TimothyMakkison marked this pull request as ready for review October 21, 2025 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant