Speed up file access in unzip.c
If you've read my last post about the working unzip code in zlib, you might have already tried that. These days, I am working on some code to pack game assets to a big file. The code worked like a charm on PC. However when I deployed it on iPhone, it's too slow.
Our game has more than 3000 assets, and it gets slow when loading the contents. After a little profiling, I noticed it takes about 0.2s to load a tiny png file on the device. That's not acceptable. I checked the code, and found the unzLocateFile is way too slow, it tries to find the file like this:
1. Go to first file
2. Check and go to next file
When it goes to a file, it seeks the zip file, and read some description data from it.
OK, it's time to fix this. We could introduce a hash table to boost the search speed. If you look into the unzGoToFirstFile and unzGoToNextFile, you can find it sets only a few parameters in the unzip handle (unz64_s*). We can store the data in the table, so that we could bypass the IO wasted.
What we store in the table are:
1. The filename hash
2. The filename (When the hash matches, we need double check the filename)
3. The parameters the two functions (unzGotoFirstFile and unzGotoNextFile) use.
Now, when searching the files, first we match the hash, then the filename. If both are matched, we simply set the parameters in unzip handle. Done.
After this modification, I profiled the app again, and the load time is reduced to 0.002s. Sweet!

浙公网安备 33010602011771号