cocos / cocos-engine

Cocos simplifies game creation and distribution with Cocos Creator, a free, open-source, cross-platform game engine. Empowering millions of developers to create high-performance, engaging 2D/3D games and instant web entertainment.
https://www.cocos.com/en/creator
Other
5.52k stars 1.58k forks source link

Optimize game asset cache database (cacheManager) save when a lot files in cache. #16778

Open Yuki001 opened 4 months ago

Yuki001 commented 4 months ago

Use Case

Use game cache in wechat game. The game project has a lot asset files to cache

Problem Description

When cached files count has a lot (>2000 or above),if the cache insert a file, the database will update in 2 seconds. But the json database (cacheList.json) serializing and writing is very slow , makes the game junk at every write time.

Proposed Solution

Use a database file format for asset cache instead of json file, not serialize all the data for a update. Maybe a sqlite or something else format which is fast for insert and save.

How it works

No response

Alternatives Considered

Maybe a sqlite or something else format which is fast for insert and save.

Additional Information

No response

Yuki001 commented 4 months ago

In my project, I have modified the cacheManager to clear 1/3 when found the cache file count is larger than a threshold (2048) on game init, The modify makes my game run better. but it's not the correct solution and is only a patch. The correct solution is make cache database fast for insert and save.

minggo commented 3 months ago

Can use sqlite in wechat?

Yuki001 commented 3 months ago

May be some other methods can be used for wechat instead of direct use sqlite library

  1. the wechat api support low file io operation using fd read/write with position offset,you can use “insert to back(or deleted blocks)and mark flag to delete“ to fast write file。or you can implement a btree to orgnaize the file。
  2. according the insertion id,you can split the json file by hash id into several buckets, each buckets use one json file
  3. use wechat api‘s tempFilePaths ,not use the json file for cache check,but check the file is exist in tempFilePath to make sure it is in cache。and wechat can auto clear the tempFilePath。