Skip to content
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

Use IndexedDB as cache storage on Chromium platform #328

Closed
gorhill opened this issue Dec 4, 2018 · 16 comments
Closed

Use IndexedDB as cache storage on Chromium platform #328

gorhill opened this issue Dec 4, 2018 · 16 comments
Labels
Chromium specific to Chromium/Chrome fixed issue has been addressed

Comments

@gorhill
Copy link
Member

gorhill commented Dec 4, 2018

Currently, IndexedDB is used only on Firefox platform to implement cache storage. Chromium uses extension API storage (chrome.storage.local) as a cache storage, i.e. cache items are sharing the same physical storage as user setting ones.

A benefit of using IndexedDB is that the items stored in it are LZ4-compressable (which is enabled by default for that storage). LZ4 compression is not trivially possible with extension API storage, as this requires the storage to support the saving of Blob resources. (An approach would be to create a special encoding/decoding step, but that is just not practical.)

There is no technical obstacle to also use IndexedDB on Chromium. The main obstacle of doing so is that there is no code to automatically transfer cached items from one storage to the other, which would cause stale cache items to linger in the old storage should there be a switch to a new one for cache purpose.

Provide code to move cache items stored in chrome.storage.local to IndexedDB when uBO launches and detect that IndexedDB is being used as cache storage.

@uBlock-user uBlock-user added TODO todo Chromium specific to Chromium/Chrome labels Dec 5, 2018
@gwarser
Copy link

gwarser commented Dec 6, 2018

Isn't extension storage already compressed natively in Chromium? Files in Local Extension Settings/cjpalhdlnbpafiamejdnhcphjbkeiagm/ are already small and not compressible.


Snappy https://en.wikipedia.org/wiki/LevelDB#Features
and "LevelDB is widely noted for being unreliable and databases it manages are prone to corruption."

@gorhill
Copy link
Member Author

gorhill commented Dec 6, 2018

Could be.

However I found that with IndexedDB (seems to be at IndexedDB/chrome-extension_[id]_0.indexeddb.leveldb on my side) I could definitely see less issues with garbage collection after loading filter lists or selfie.

With a valid selfie, when using chrome.storage.local, the baseline memory seems stuck at a higher level than expected. I found that with IndexedDB, the baseline memory usage at launch quickly snaps well under 40 MB on my side, something I am not able to get with chrome.storage.local. With a valid selfie (default lists), uBO's baseline memory usage at launch is now lower than it ever has been I am pretty sure.

I can only speculate but it seems the browser will keep entries from chrome.storage.local in memory more aggressively -- even forcing garbage collection in dev tools does not help, while this does not happen with IndexedDB. There is no point to keep cache-related entries in memory once they are loaded.

@uBlock-user uBlock-user added fixed issue has been addressed and removed TODO todo labels Dec 6, 2018
@uBlock-user
Copy link
Contributor

My storage usage on Chromium on 1.17.3rc5 was 27 MB and now with the upgrade to 1.17.5b0 it went down to 10.3 MB, WOW! Is this because of IndexedDB being used now ?

@gwarser
Copy link

gwarser commented Dec 6, 2018

Measured by uBO?

@uBlock-user
Copy link
Contributor

Yes, the one listed in settings.html ?

@gwarser
Copy link

gwarser commented Dec 6, 2018

On disk for all non-regional list both use ~17MB (needs some time to stabilize and restart to remove overgrown 000*.log)

@uBlock-user
Copy link
Contributor

what do you mean ?

@uBlock-user
Copy link
Contributor

@gorhill The storage value seems to be jumping back and forth on my end. Once I see 10 MB in use and then after few minutes/hours, it's 17 MB in use, is this to be expected ?

@gorhill
Copy link
Member Author

gorhill commented Dec 6, 2018

Probably uBO created a selfie, used for fast loading next time uBO is launched. The selfie is destroyed whenever a filter list is updated, and it will re-create itself after ~17 minutes following the reload of all filter lists.

@uBlock-user
Copy link
Contributor

uBlock-user commented Dec 6, 2018

next time uBO is launched.

So for future use ? Thats odd as I don't close Chromium for hours together(not disabling uBO either) and this happens randomly.

@gwarser
Copy link

gwarser commented Dec 6, 2018

what do you mean ?

Both stable and dev uses comparable amount of disk storage (after stabilisation). Stable uses more initially, releases excess after restart (very lazy, can take hours).

storage.local probably uses *.log files for journaling, and clean this file after long delay. Other files are also keept.

@uBlock-user
Copy link
Contributor

Stable uses more initially, releases excess after restart (very lazy, can take hours).

and what about dev builds as I'm on that ?

@gwarser
Copy link

gwarser commented Dec 6, 2018

Check IndexedDB/chrome-extension_cgbcahbpdhpcegmbfconppldiemgcoii_0.indexeddb.* folders in your Chromium profile.

@uBlock-user
Copy link
Contributor

uBlock-user commented Dec 6, 2018

I did, I see lots of files inside the first folder, the last two have files name LOCK and other files which are created by chromium I suppose.

@gorhill
Copy link
Member Author

gorhill commented Dec 6, 2018

I've run a lot of benchmarks and measurements. Here is the summary: IndexedDB + LZ4 is beneficial to uBO in Chromium, even if whatever is stored on disk is independently compressed by Chromium itself. So this will stay.

If ever there is a demonstration that the overhead of LZ4 compression uBO-side is questionable, I can always trivially add code to turn off compression while keeping IndexedDB.

I used the advanced setting cacheStorageCompression to turn off compression for my measurements. When toggling this setting, it is important to force an update of all filter lists.

Two screenshots of performance benchmark using dev tools to measure launch timings.

No selfie, IndexedDB + compression:
b

No selfie, IndexedDB + no compression:
a

Now it seems the "IndexedDB + compression" did better, but we shouldn't draw such conclusion given the small difference. What is important is to be able to conclude that it does not appear using compression adds undue overhead.

Now, memory baseline at launch time (i.e. memory usage after garbage collection, I waited 5 minutes before screenshots).

uBO 1.17.4: selfie + 2nd-gen hntrie + chrome.storage.local:
a

uBO dev: selfie + 3rd-gen hntrie + chrome.storage.local + no compression:
a

uBO dev: selfie + 3rd-gen hntrie + IndexedDB + compression:
b

I tried many different combos and I conclude that what actually reduces baseline memory usage is uBO-side compression (hopefully I didn't lose myself in all the measurements I ran). If I disable compression but still use IndexedDB, I get the 50 MB baseline. Forcing garbage collection in dev tools accomplishes nothing. I don't know the explanation of this but this is what I have observed.

Thus, given that there is seemingly no undue overhead incurred as a result of using compression, and given that the baseline memory usage is much better when using compression, and given that actually reporting a more accurate on-disk bytes used to users (assuming Chromium does compress itself) is desirable, I will keep compression enabled.

@gwarser
Copy link

gwarser commented Mar 26, 2019

I just discovered (reddit) that Firefox also have compression in indexedDB:

https://hg.mozilla.org/releases/mozilla-release/annotate/0a91847e50e6/dom/indexedDB/ActorsParent.cpp#l1441

But when it's used and for what type of data??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Chromium specific to Chromium/Chrome fixed issue has been addressed
Projects
None yet
Development

No branches or pull requests

3 participants