I have been working on downloads. One of the things that I have to do is calculate the size of the streamed download. I thought I was doing this fine, but then found that if I compared the size I calculated and the size of the file when it was downloaded, my calculation was always smaller.
Originally, I was using
String.length. That counts graphemes. OK, got it, a grapheme may be made up of multiple bytes. So we went to
Kernel.byte_size. Now, we were a few bytes larger in the calculation than what Mac OS was showing with
ls -l. So closer, but still not there. Then we went to
String.codepoints |> Enum.count and it was spot on. Looking at
byte_size I noticed this message:
Returns the number of bytes needed to contain
That is, if the number of bits in
bitstringis not divisible by 8, the resulting number of bytes will be rounded up (by excess). This operation happens in constant time.
That seems to explain the problem, but I don’t understand the value of rounding up. Is that an optimization, sacrificing some accuracy for speed?