Skip to content

[REFACTOR] Avoid computing samples eagerly in SimulationResults#423

Merged
mattdailis merged 3 commits intodevelopfrom
refactor/sim-results-take-samples-on-demand
Nov 8, 2022
Merged

[REFACTOR] Avoid computing samples eagerly in SimulationResults#423
mattdailis merged 3 commits intodevelopfrom
refactor/sim-results-take-samples-on-demand

Conversation

@mattdailis
Copy link
Copy Markdown
Collaborator

  • Tickets addressed: N/A
  • Review: By commit
  • Merge strategy: Merge (no squash)

Description

This PR removes resourceSamples as a field on SimulationResults. Prior to this PR, this field was derived from realProfiles and discreteProfiles via the takeSamples method - upon calling the SimulationResults constructor, takeSamples would be called eagerly.

In typical usage, resourceSamples is unused - the simulation results are written to the database using the realProfiles and discreteProfiles directly. The only time it is used (outside of testing) is via the explicit resourceSamples hasura action.

This PR moves computation of resource samples out of the SimulationResults constructor, so that they can be computed when requested, but not by default.

I have not done any performance measurements to see whether or not this change is significant - nevertheless, I believe avoiding needless computations is generally a good thing.

Verification

I spun up a local Aerie deployment and checked that running simulations and viewing their results worked, and I also called the resourceSamples endpoint directly.

Documentation

No functional behavior has changed, so no documentation needs to be updated.

Future work

No future work

@mattdailis mattdailis requested a review from a team as a code owner November 7, 2022 14:11
@mattdailis mattdailis temporarily deployed to e2e-test November 7, 2022 14:11 Inactive
@camargo camargo added the refactor A code change that neither fixes a bug nor adds a feature label Nov 7, 2022
Copy link
Copy Markdown
Contributor

@Mythicaeda Mythicaeda left a comment

Choose a reason for hiding this comment

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

Looks fine, although seeing it appear in the mission models makes me wonder how often this calculation will be called per SimulationResults object. Would it be worth it to instead make resourceSamples a private field that is not assigned at construction, and have it accessed through a getter that assigns the output of takeSamples into it if resourceSamples is null? Then we only run takeSamples at most once per SimulationResults object.

@mattdailis
Copy link
Copy Markdown
Collaborator Author

Looks fine, although seeing it appear in the mission models makes me wonder how often this calculation will be called per SimulationResults object. Would it be worth it to instead make resourceSamples a private field that is not assigned at construction, and have it accessed through a getter that assigns the output of takeSamples into it if resourceSamples is null? Then we only run takeSamples at most once per SimulationResults object.

If we expected this computation to happen multiple times, absolutely, I think caching it in a field would be a valid approach. 💯

As it stands, we've moved away from this method (it provides strictly less information than realProfiles and discreteProfiles do, while using more memory) - so we don't expect this method to get more use in the future. If I had my druthers, we'd remove this method entirely, but we're keeping the resourceSamples hasura action in the API as a convenience for clients that don't want to work with continuous profiles.

@mattdailis mattdailis force-pushed the refactor/sim-results-take-samples-on-demand branch from b2d78d9 to 84fe058 Compare November 7, 2022 20:34
@mattdailis mattdailis temporarily deployed to e2e-test November 7, 2022 20:34 Inactive
@Mythicaeda
Copy link
Copy Markdown
Contributor

As it stands, we've moved away from this method (it provides strictly less information than realProfiles and discreteProfiles do, while using more memory) - so we don't expect this method to get more use in the future. If I had my druthers, we'd remove this method entirely, but we're keeping the resourceSamples hasura action in the API as a convenience for clients that don't want to work with continuous profiles.

So this refactor is setup for an eventual deprecation? If so, then I think I'm fine with making it the responsibility of whoever calls the public method to cache its return value.

@mattdailis
Copy link
Copy Markdown
Collaborator Author

So this refactor is setup for an eventual deprecation? If so, then I think I'm fine with making it the responsibility of whoever calls the public method to cache its return value.

When I brought up the possibility of deprecation, @patkenneally explained that we should keep the capability in case API clients want it, but that we should encourage people to use the continuous profiles directly. So - yes, it's "discouraged" if not exactly "deprecated for removal".

@Twisol
Copy link
Copy Markdown
Contributor

Twisol commented Nov 7, 2022

Personally, I'd like to see the resourceSamples code moved out of SimulationResults entirely. It looks like the only place where we access it outside of testing/development code is that API endpoint, and the computation doesn't really feel like the responsibility of SimulationResults anymore.

@mattdailis
Copy link
Copy Markdown
Collaborator Author

Personally, I'd like to see the resourceSamples code moved out of SimulationResults entirely

Done 👍 The sampling logic is now the responsibility of the GetSimulationResultsAction, which was the only client of this functionality. I inlined it into the getResourceSamples method.

@mattdailis mattdailis force-pushed the refactor/sim-results-take-samples-on-demand branch from a620523 to 19f1b68 Compare November 8, 2022 01:18
@mattdailis mattdailis temporarily deployed to e2e-test November 8, 2022 01:18 Inactive
@Twisol
Copy link
Copy Markdown
Contributor

Twisol commented Nov 8, 2022

Awesome! Thanks for doing that!

There are now two separate flows - when a simulation is run, and when
samples are requested explicitly. Samples are not written to the
database, so there is no need to compute samples unless they are
requested.
@mattdailis mattdailis force-pushed the refactor/sim-results-take-samples-on-demand branch from 19f1b68 to d924e07 Compare November 8, 2022 03:17
@mattdailis mattdailis temporarily deployed to e2e-test November 8, 2022 03:17 Inactive
This plan.json is used in the main method in SimulateMapSchedule -
it's not used for any automated tests, but it can be used for manual
testing. In #334, we added a z parameter to the FooActivity, but
forgot to update this file.
@mattdailis mattdailis force-pushed the refactor/sim-results-take-samples-on-demand branch from d924e07 to 2aaa270 Compare November 8, 2022 03:55
@mattdailis mattdailis temporarily deployed to e2e-test November 8, 2022 03:55 Inactive
@mattdailis mattdailis merged commit 17594b8 into develop Nov 8, 2022
@mattdailis mattdailis deleted the refactor/sim-results-take-samples-on-demand branch November 8, 2022 15:33
@camargo camargo added the breaking change A change that will require updating downstream code label Nov 14, 2022
@camargo camargo mentioned this pull request Nov 14, 2022
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking change A change that will require updating downstream code refactor A code change that neither fixes a bug nor adds a feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants