Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Conversation

@jensjoha
Copy link
Contributor

@jensjoha jensjoha commented Jun 1, 2022

When checkIgnoredLines is true parseJson reads files to find
ignore data for that file.
If calling parseJson several times for the same project,
as for instance done in flutter, this adds significant overhead.

This commit adds a new function - parseJsonSync - which is passed
a cache of the lookups. This way future lookups for the same
file will be faster. The new function is also sync avoiding any
potential race conditions when filling up the cache.

This - in combination with a change on the Flutter side -
reduces the overhead of collecting coverage in Flutter.
On my machine running flutter test --coverage in flutter/packages/flutter
goes from ~13.5 minutes to ~10 minutes.

@jensjoha jensjoha requested review from grouma and liamappelbe June 1, 2022 12:30
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.02%) to 93.097% when pulling d67414d on jensjoha:cacheable_parseJson into 6fe9402 on dart-lang:master.

///
/// Throws away all entries that are not resolvable.
static Future<Map<String, HitMap>> parseJson(
static Map<String, HitMap> parseJsonSync(
Copy link
Contributor

Choose a reason for hiding this comment

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

Why add a separate parseJsonSync function? Why not just add a cache to parseJson?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This - at least I think - would be a non-breaking change.

I think adding it to parseJson (and removing the async) would make it breaking, though I'm quite frankly not sure exactly what constitutes a breaking change.

I've made it sync, btw, to avoid potential race conditions in filling in the cache.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, making it sync would be breaking. So if we need a sync version it'll need to be separate.

I wonder if we need the sync version though. I guess the race condition would manifest as occasionally parsing a file twice. Intuitively, I would have thought that's a smaller performance hit than making the function sync.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's hard to reason about how often a file will be read twice and that's one reason for me to favor the sync version.

As for speed, generally async can be faster when doing multiple io operations simultaneously. If only doing one IO operation at a time though it is slower. parseJson only reads one file at a time. Therefore the only way the async version of parseJson could be faster would be if it was called several times concurrently. Considering parseJson is not optimized for being called several times (at least not if the calls share sources as they for instance do in flutter which is the entire point of the PR) I don't think that's a use-case that should make us favor async over sync.

Said another way, my intuition would certainly be that for the common use case a sync version would be faster than an async version.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok

///
/// Throws away all entries that are not resolvable.
static Future<Map<String, HitMap>> parseJson(
static Map<String, HitMap> parseJsonSync(
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok

@liamappelbe
Copy link
Contributor

CI is redness is unrelated to your PR. I'm working on it. Let's wait for it to turn green before merging your change though.

Copy link
Contributor

@liamappelbe liamappelbe left a comment

Choose a reason for hiding this comment

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

Oh, just remembered, this will need a minor version bump and a changelog entry

jensjoha added 3 commits June 16, 2022 08:25
When checkIgnoredLines is true parseJson reads files to find
ignore data for that file.
If calling parseJson several times for the same project,
as for instance done in flutter, this adds significant overhead.

This commit adds a new function - parseJsonSync - which is passed
a cache of the lookups. This way future lookups for the same
file will be faster. The new function is also sync avoiding any
potential race conditions when filling up the cache.
@jensjoha jensjoha force-pushed the cacheable_parseJson branch from c0b56e0 to 1c0e6e9 Compare June 16, 2022 06:30
@jensjoha
Copy link
Contributor Author

Oh, just remembered, this will need a minor version bump and a changelog entry

Done.

@jensjoha jensjoha requested a review from liamappelbe June 16, 2022 06:31
Copy link
Contributor

@liamappelbe liamappelbe left a comment

Choose a reason for hiding this comment

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

LGTM, but let's wait for CI to turn green before merging. I've submitted a fix to the VM, and I'm just waiting for it to roll into dev.

@liamappelbe liamappelbe merged commit 92c480b into dart-archive:master Jun 21, 2022
mosuem pushed a commit to dart-lang/tools that referenced this pull request Aug 28, 2024
…rchive/coverage#391)

* Add version of parseJson that can cache file reads and parses

When checkIgnoredLines is true parseJson reads files to find
ignore data for that file.
If calling parseJson several times for the same project,
as for instance done in flutter, this adds significant overhead.

This commit adds a new function - parseJsonSync - which is passed
a cache of the lookups. This way future lookups for the same
file will be faster. The new function is also sync avoiding any
potential race conditions when filling up the cache.

* Remove createResolver

* Feedback: minor version bump and a changelog entry
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants