-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Remove page size truncate of data and crc file #32
Remove page size truncate of data and crc file #32
Conversation
It's true that The |
@lingol It's true the memory address returned by
Yes, each file occupy at least one block, but |
As for the round up adjustment, it is better this way if not necessary. Not because of mmap's requirement, but for the sake of future usage. MMKV uses a double-size strategy to extend file size if space is not enough. It's better and simple for the file to have a size of n pagesize, and to increase by double. Like 16K, 32K, 64K, etc. You may ask the file is given one pagesize for initialization, what's the need to round up? The fact is shit happens. In production the file's size may end up not rounding up to pagesize, and that causes some itches. So here we are, checking file size and rounding up when opening. |
@lingol Emm, maybe it's for the sake of future usage. You mean if file's size not end up to pagesize, it may cause some issues? 🤔 can you give me some examples? thanks. So I can revert the adjustment for data file, but I still want to make And just some recommends about double-size algorithm, double size when data length is not big is ok, but if data grow up to big size, exponential growth may have some problems, like 500M, double to 1000M, currently, we just mmap the entire file, it would occupy a long continuous memory address, may not easy for kernel to find this demand. Maybe we can use some optimize algorithm, like Slow start of TCP, or split mmap to segments and load in need. However, if the frameworks only used just like |
Well, in the early days of MMKV development & trials, we receive some crash report when writing back to file. By looking into logs we found the file size is not round up. So a highly suspicion is that iOS may not tell the true about file's actual size when truncating, and writing beyond that may crash. We tried rounding up file size for protection, and those crashes are gone.
Currently Okey, 4B is enough for iOS implementation, thought in Android it needs more than that. |
As for the growth algorithm, MMKV currently use the simple double-size algorithm. Mainly to avoid frequently full-write-back, which is slow. In fact, MMKV even makes a guess about future usage, making room not just for current append/insertion, but also for some more operation in-head. Checkout |
Make CRC-32 file length to 4B adjust spacing adjust spacing
c5d7c60
to
3857a82
Compare
Interesting! 🤔
Emm, I know it's for future usage, in my comment, I want to express maybe we can adjust the algorithm, not just always double size, just like Anyway, I updated the |
Double length does consume a bit of space, can it be more optimized in the future? |
It turns out that limiting CRC file's size to 4 byte makes |
CRC-32
file is only need 4B, but we allocate one page 4096B, waste 4092B. And actually, we don't need to align, becausemmap
don't needsize
parameter to be aligned, it's just need not be 0, onlyoff
parameter must be a multiple of pagesize. I add a global variableDEFAULT_FILE_SIZE
, currently it's equal to page size, but we can adjust it.fd
check of validation,fd
is invalid iffd == -1
.