Fix #563: filesizeformat with proper accuracy#568
Conversation
f989c7c to
92d705c
Compare
|
Working on u128 would probably be faster than working on f32. I guess formatting directly into the output stream is what makes the new code slower. |
Nope, if floats and integers are of the same size, the floats are always slower, however floats are always faster than bigger integers (so u64 > f32 > u32). Also, what kind of numbers would we need above 18 exabytes ( |
92d705c to
17ef961
Compare
|
I knew binary search was probably slower than linear search in this case, but I didn't think it was noticeable... Now I'm at |
I tried to port it and couldn't represent the previous functionality, because the set of SI_PREFIXES in the current implementation overflowed the |
3a563a9 to
7b1e266
Compare
|
Just some final formatting improvements. |
7b1e266 to
39278c9
Compare
|
Might be better if you did so you can learn: what did you try? There are a few things needed to make it work: the |
dcedaf9 to
74474ff
Compare
I had trouble understanding how the new argument parsing stuff works (how to fill the |
74474ff to
c8740f1
Compare
3aed30a to
1706904
Compare
|
One more improvement. Instead of just stupidly limiting the precision to the completely arbitrary number of Allowing a larger let mut fractional = remainder.saturating_mul(scale) / unit.1;to: let mut fractional = remainder / (unit.1 / scale);which effectively avoids overflow due to the multiplication, while allowing much higher precision. |
| ' '.write_into(dest, values)?; | ||
| unit.0.write_into(dest, values)?; | ||
| 'B'.write_into(dest, values)?; |
There was a problem hiding this comment.
| ' '.write_into(dest, values)?; | |
| unit.0.write_into(dest, values)?; | |
| 'B'.write_into(dest, values)?; | |
| write!(dest, " {}B", unit..0)?; |
There was a problem hiding this comment.
I can do this change, but I would strongly advise against it.
This will outright destroy performance.
(I came from there 😅)
There was a problem hiding this comment.
Wait really? I'm surprised, generally the I/O calls are slow so we try to do as little of them as possible.
There was a problem hiding this comment.
The std calls are SUPER slow. That's why I use askama's FastWritable::write_into() everywhere.
I can do 100s of calls of FastWritable::write_into() and will still be faster than one write!().
Inlining sure can work wonders :)
1706904 to
968a1d4
Compare
968a1d4 to
767ae9e
Compare
|
Looks all good to me, great work! |
|
Thank you two! |
I refactored the current
humansize/filesizeimplementation fromf32tou128and added more unit-tests.Additionally, the new refactoring now allows changing the requested precision (not yet plumbed on the code generation side).
u128is required to handle the SI Prefixes:Getting something as performant as the original version is quite challenging. Here are my benchmark results for the current solution:
Benchmarks (time for
250000000format & display invocations):f32):1.9542559su64:2.638588188su128):3.29419516s