fix(mmap): handle over-allocated files by petersalomonsen · Pull Request #10526 · emscripten-core/emscripten
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when appending to a file expandFileStorage will increase the buffer size for that file more than the bytes requested to be written. This is to avoid having to increase the buffer for every call to write (which would be very slow). So instead the actual file size is tracked in a separate property called usedBytes.
mmap allocates memory based on the usedBytes, and calls javascripts ArrayBuffer set method on the heap with the files buffer as parameter. The set method has no parameter for the length, and so it copies the full contents of the file buffer. To avoid copying more than the actual file size the file buffer is sliced before calling set, but before this fix, slicing would only occur if the requested length was less than used bytes (which is mostly not the case if mapping the entire file). So instead the criteria for slicing must be if the requested length is less than the length of the file buffer - which is normally more than usedBytes for expanded files.