Skip to content

Defer reading of transform source file - #13419

Closed
mitchhentgesspotify wants to merge 2 commits into
jestjs:mainfrom
mitchhentgesspotify:avoid-source-reads
Closed

mitchhentgesspotify wants to merge 2 commits into
jestjs:mainfrom
mitchhentgesspotify:avoid-source-reads

Conversation

@mitchhentgesspotify

Copy link
Copy Markdown
Contributor

If test files are already compiled and in the FS cache, then their source doesn't need to be read for the purposes of transforming.

Note that they're probably still read elsewhere for hashing, but this reduces some potential unnecessary reads.

Signed-off-by: Mitchell Hentges mhentges@spotify.com

Test plan

Within _scriptTransformer.transform(...), it already has behaviour to populate source when it's not provided, so this should be solid.
The existing test suite should provide some confidence too.

filename: string,
options?: InternalModuleOptions,
): string {
const source = this.readFile(filename);

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.

hmm, readFile looks in cacheFS, same as the transformer. does this actually change anything? it's the same read operation cached at slightly different times?

https://github.com/facebook/jest/blob/c791d97114024391b1894d9923688c0a0216cc4f/packages/jest-runtime/src/index.ts#L2308-L2318

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.

If I'm wrong and it's not the same cache, we should fix that

@mitchhentgesspotify mitchhentgesspotify Oct 14, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yep, it's the same backing cacheFS instance 👍

does this actually change anything

Err, you're absolutely correct. This needs to go another layer deeper:

TL;DR: This PR needs more work to avoid the unnecessary readFile, as it depends on whether or not coverage reporting is going on.
Sorry about that!

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.

Thanks for checking! I'm super happy somebody is looking into these performance improvements 🙂 death by a thousand cuts and all that

If test files are already compiled and in the FS cache, then their
source doesn't need to be read for the purposes of transforming.

Note that they're probably still read elsewhere for hashing, but this
reduces some potential unnecessary reads.

Signed-off-by: Mitchell Hentges <mhentges@spotify.com>
Signed-off-by: Mitchell Hentges <mhentges@spotify.com>
@mitchhentgesspotify

Copy link
Copy Markdown
Contributor Author

Oof, I shouldn't be working on these things while travelling, I think the premise of this PR is entirely flawed: the key of transformed files is based off of the hash of source file contents. So, the source must be read anyways.

I mentioned this in my summary ("Note that they're probably still read elsewhere for hashing, but this reduces some potential unnecessary reads."), but based off of your comment and looking at this again, I think that my premise is flawed.


I'm going to close this for now: it would be nice if we could avoid reading source files when we know that the transformed results are up-to-date, but I don't think that up-to-date checking is possible without, well, hashing contents.
I'll take another look at this with fresh eyes next week, but I think that this PR is a miss.
Sorry for the spam :)

@SimenB

SimenB commented Oct 14, 2022

Copy link
Copy Markdown
Member

We could probably stat the files and look at mtime rather than reading the file. But that feels somewhat unreliable (especially for any sort of distributed caches which, last time I talked with Spotify engineers, was a thing you used).

@SimenB SimenB reopened this Oct 14, 2022
@SimenB

SimenB commented Oct 14, 2022

Copy link
Copy Markdown
Member

Sorry, missclick

@SimenB SimenB closed this Oct 14, 2022
@mitchhentgesspotify

Copy link
Copy Markdown
Contributor Author

We could probably stat the files and look at mtime rather than reading the file

True, I'd be more comfortable with something like this in watch mode than on fresh processes due to the limitations of mtime checks (distributed caches, etc).

@github-actions

Copy link
Copy Markdown

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Nov 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants