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

Slow initial cache population due to recomputation of hash #171

Closed
phuhl opened this issue Jan 27, 2023 · 2 comments · Fixed by #172
Closed

Slow initial cache population due to recomputation of hash #171

phuhl opened this issue Jan 27, 2023 · 2 comments · Fixed by #172
Assignees
Milestone

Comments

@phuhl
Copy link
Contributor

phuhl commented Jan 27, 2023

I was wandering why, despite #116 the initial image generation was pretty slow (18 seconds for 14 pictures). So I dug into the code and found, that for each size-format combination the following happens:

  • resize
    • this.getFullStats
      • this.getHash
        • this.getFileContents and hash calculation (takes about 50ms+ per run)

Some console log output:

hash 74
hash 65
hash 66
hash 66
hash 65
hash 65
hash 65
hash 66
hash 67
hash 65
hash 65
DONE PROCESSING IMG 814

As you can see, the repeated calling of hash bumps up the time per image to just under a second for just this image.

All of the getHash calls are done on the same Image instance. Hence, I propose this change:

  getHash() {
+    if (this.computedHash) {
+      return this.computedHash;
+    }

    let hash = createHash("sha256");
    // ...
-     return base64hash.substring(0, this.options.hashLength);
+    const resultHash = base64hash.substring(0, this.options.hashLength);
+    this.computedHash = resultHash;
+    return resultHash;
  }

Would this have any downsides? For me it just meant, that an image generates in 50 ms instead of 800ms. My initial build time is down from 18 seconds to 1.5 seconds. 12x faster, not bad in my book 😄

@phuhl
Copy link
Contributor Author

phuhl commented Jan 27, 2023

I think, this is a duplicate of #170 but with the explanation with what's going on

@zachleat zachleat self-assigned this Jan 30, 2023
@zachleat zachleat added this to the v3.0.0 milestone Jan 30, 2023
@zachleat
Copy link
Member

This will ship with 3.0.0

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 a pull request may close this issue.

2 participants