Skip to content

Set data package_uuid to sha256 hash of preloaded contents - #16807

Merged
sbc100 merged 1 commit into
emscripten-core:mainfrom
connorjclark:sha-uuid
Jun 14, 2022
Merged

sbc100 merged 1 commit into
emscripten-core:mainfrom
connorjclark:sha-uuid

Conversation

@connorjclark

Copy link
Copy Markdown
Contributor

This PR sets package_uuid to be the sha256 hash of the contents of the preloaded data file, if applicable. This avoids unnecessary cache misses–currently a new is generated per build, which invalidates the cached value in idb even if nothing changed in the data file.

The hash is generated based on the contents of the preloaded datafile post-compression, if applicable.

In #11952 it was suggested to make this be configurable–however, I don't think there's any reason to want this id to be unstable. At least, I can't think of one.

It's a bit weird that package_uuid is only sometimes actually an uuid. Another approach would be to keep package_uuid the same in the metadata, introduce a new content_hash field, and use this for the idb cache key: metadata['content_hash'] || metadata['package_uuid'] (content_hash only being set when the data is preloaded). Is it important to retain current behavior of the metadata's package_uuid property?

Fixes #11952

@kripken

kripken commented Apr 28, 2022

Copy link
Copy Markdown
Member

I don't remember this code well, but after checking the hash do we also check the contents for equality? I think we don't, since the point is to avoid fetching the contents, but I could be wrong.

If we don't check for equality, then there is some risk here. SHA256 collisions are in theory extremely unlikely, so maybe we should just ignore them. But a collision could lead to a very bad experience that is very hard to figure out, which is a little worrying.

Note that you can run the file packager separately from emcc, so you can run it only when your content actually changes, to avoid creating a new uuid each time.

@connorjclark

Copy link
Copy Markdown
Contributor Author

You really should give zero weight to the possibility of a hash collision. What does actually happen is network requests failing for random reasons. This patch would reduce that by making network requests happen less often.

Comment thread tools/file_packager.py Outdated
@sbc100

sbc100 commented May 1, 2022

Copy link
Copy Markdown
Collaborator

The codesize tests failures should be fixed with a rebase or merge with main.

@connorjclark
connorjclark force-pushed the sha-uuid branch 2 times, most recently from 4e95d76 to 8913f2d Compare May 3, 2022 04:57

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the field name matters too much. lgtm either way.

@kripken

kripken commented May 3, 2022

Copy link
Copy Markdown
Member

You really should give zero weight to the possibility of a hash collision

Fair point, but as mentioned there, "Of course, all of the above assumes that SHA-256 is a "perfect" hash function, which is far from being proven. Still, SHA-256 seems quite robust." In practice, you are right - network issues happen far more often - but depending on this troubles me. If I'm the only one, though, then I won't block this.

@sbc100

sbc100 commented May 3, 2022

Copy link
Copy Markdown
Collaborator

I think depending on SHA-256 for this purpose is totally fine. Caching data based on its hash seems perfectly reasonable.

@connorjclark

Copy link
Copy Markdown
Contributor Author

Anything else to change on my end?

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this lgtm. @kripken ?

@kripken kripken left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, since it seems like I'm the only one that slightly prefers the old way.

@kripken

kripken commented May 24, 2022

Copy link
Copy Markdown
Member

Before landing, please add a comment to the changelog file, as this is a behavior change that in theory might be noticed.

@connorjclark

Copy link
Copy Markdown
Contributor Author

Sorry for delay, should be good now

@kripken

kripken commented Jun 13, 2022

Copy link
Copy Markdown
Member

I still don't see a change to the Changelog file? Perhaps a commit wasn't pushed?

@connorjclark

Copy link
Copy Markdown
Contributor Author

Oops, I messed up the git command. pushed it now.

@sbc100
sbc100 enabled auto-merge (squash) June 14, 2022 16:06
@sbc100
sbc100 merged commit 84249c1 into emscripten-core:main Jun 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Data cached to IndexedDB (--use-preload-cache): cache invalidated even if data did not change

3 participants