-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
optimized textwidth(::Char) for ASCII #55398
Conversation
Can it be faster if the const lookup table is a Tuple? The reason is that const-ness at the moment only refers to the binding, so a const vector will do a triple load (load the underlying memoryref, then load the pointer, then load the offset) whereas an immutable Tuple might generate better code. |
@jakobnissen, good catch, a tuple table indeed seems to be faster: julia> @btime textwidth('x'); @btime textwidth2('x'); @btime textwidth3('x');
3.166 ns (0 allocations: 0 bytes)
1.958 ns (0 allocations: 0 bytes)
1.125 ns (0 allocations: 0 bytes) ( |
Another possibility would be to omit the table entirely and replace it with a couple of (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0) Update it looks like b < 0x7f && return Int(b >= 0x20); is about the same speed as the tuple lookup table, maybe 1% faster, and is simpler, so I'll switch to this. (It omits a fast path for the ASCII U+007f "DEL" character, which has width 0, but having that odd character being slightly slower doesn't seem like a big deal to me). |
Looks like an unrelated failure in the REPL tests? |
Co-authored-by: Jeff Bezanson <[email protected]>
Co-authored-by: Jeff Bezanson <[email protected]>
For the common case of ASCII characters, I found in https://github.com/JuliaLang/julia/pull/55351/files#r1705746669 that
textwidth
can be sped up by60%2.7x or more by adding a fast-path ASCIIlookup tablehard-coded rule.The slowdown for non-ASCII characters seems negligible (1% or less). Here,
textwidth2
is the new function, andtextwidth
is the old function (Julia 1.10.4, Apple M1 Pro):