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

Reduce memory allocations from closures and lambdas #2197

Merged
merged 11 commits into from
Sep 24, 2021

Conversation

habbes
Copy link
Contributor

@habbes habbes commented Sep 22, 2021

Issues

This pull request fixes #2192 .

Description

Optimizes the hot spots specified in the issue description and also adds new benchmarks to the performance tests solution.

  • Avoid lambda capturing variable from outer scope in ODataWriterCore.CheckForNestedResourceInfoWithContentAsync
  • Pass lambda instead of extension method as func argument in ExtensionMethods.FindType
  • Replace list.FirstOrDefault with for loop to avoid closure allocations in EdmExtensions.ReplaceAlias
  • Replace DerivedTypeConstraints.Any() with foreach loop in WriterValidationUtils.ValidatePropertyDerivedTypeConstraints. Using foreach does cause an IEnumerator to be allocated on the heap, but so does using Any() in this case (since the underlying IEnumerable is not a collection with a cheap count). There's a separate issue that aims to reduce IEnumerator allocations, and that would involve checking whether this collection can be converted to a List<T>. I will do that investigation when dealing with that issue.
  • Replace derivedTypeConstraints.Any() with foreach loop in WriterValidationUtils.ValidateDerivedTypeConstraints. (Same remarks as the item above)
  • Replace this.children.Any() with this.children.TryGetValue in SelectedPropertiesNode.GetSelectedPropertiesForNavigationProperty. Also replace the this.GetMatchinTypeSegments().Select() with a method GetSelectedPropertieForTypeSegmentsNavigationProperty that encapsulates that does the same thing as the Select() but without lambdas.

I've also added a new benchmarks project. The purpose of these benchmarks is to compare performance between the JsonSerializer and ODataWriter (both sync and async). These are based on the experiments that I've been running locally to uncover issues with writing. The pre-existing benchmarks did not cover async code paths and do not give us insight into how we're doing compared System.Text.Json or some other "standard" serialization library.

Benchmarks

On my local profile, the number of total allocations from Microsoft.OData.Core assembly went down from 1,475,609 to 1,385,600 (About 6% reduction).

The benchmarks below were run before and after the set of changes. They indicate a reduction of about 3.2-3.4% in allocated memory for the synchronous ODataWriter, and about 1.4% reduction for the async ODataWriter.

There also seems to be consistent reduction in run time, but I'm not sure to which extent this reduction is significant.

These benchmarks have been added to the repo and you can run them using (not sure if this was the best name of the benchmarks):

crank --config benchmarks.yml --scenario SerializerBaselines --profile lab-windows

Before
image

After
image

Here are the pre-existing ODataWriter benchmarks:

crank --config benchmarks.yml --scenario Writer --profile lab-windows

Before
image

After
image

Checklist (Uncheck if it is not completed)

  • Test cases added
  • Build and test with one-click build and test script passed

Additional work necessary

If documentation update is needed, please add "Docs Needed" label to the issue and provide details about the required document change in the issue.

cc @joaocpaiva

@habbes habbes force-pushed the perf/2192-reduce-closure-allocs branch from 97aa8ed to 488e33a Compare September 23, 2021 14:33
@habbes habbes requested a review from chrisspre September 23, 2021 14:46
@pull-request-quantifier-deprecated

This PR has 443 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Large
Size       : +422 -21
Percentile : 81.43%

Total files changed: 19

Change summary by file extension:
.md : +4 -0
.yml : +20 -2
.sln : +0 -0
.cs : +385 -19
.csproj : +13 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detetcted.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@habbes habbes merged commit b2d8bf7 into OData:master Sep 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory Usage - Lambda closures in hot paths cause excessive allocations
4 participants