Skip to content
This repository was archived by the owner on Mar 24, 2026. It is now read-only.

aspnetcore: ef updates - #5933

Merged
NateBrady23 merged 15 commits into
TechEmpower:masterfrom
n-stefan:ef-updates
Aug 18, 2020
Merged

NateBrady23 merged 15 commits into
TechEmpower:masterfrom
n-stefan:ef-updates

Conversation

@n-stefan

Copy link
Copy Markdown
Contributor

Try to improve Entity Framework Updates.

int currentValue, newValue;

var ids = Enumerable.Range(1, 10000).OrderBy(x => random.Next()).Take(count);
var ids = Enumerable.Range(1, 10000).Select(x => _random.Next(1, 10001)).Distinct().Take(count).ToArray();

@Kryptos-FR Kryptos-FR Aug 15, 2020 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

In this version, you have a couple of issues:

  • no guarantee that enough distinct numbers will be created to satisfy count
  • it does many allocations: some for the Set<T> in Distinct(), some for the temporary Buffer<T> in ToArray() and one for the final array (with a copy).

The previous implementation does have some allocations implied by OrderBy() but it also has the guarantee of having sufficient elements.

That said, both propositions do too many allocations and are not efficient. Instead we can use a simple Fisher-Yates to generate a randomize range in O(n) with only one allocation. See my own implementation as an example.

var ids = ShuffleUtility.RandomRange(1, 10000).Take(count);

foreach (int id in ids)
{
    // etc.
}

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.

count is 20 at most so enough distinct numbers should be generated, else there may be a problem with Random.Next(). All implementations are required to generate distinct Ids in order to pass test verification, I believe, though it may allow for a margin of error.
Regarding allocations, there may be a few but I doubt it matters, the database is likely the performance bottleneck.
Feel free to open a PR, if it really improves rps, great.

@NateBrady23
NateBrady23 merged commit 9da04d5 into TechEmpower:master Aug 18, 2020
tomciopp pushed a commit to tomciopp/FrameworkBenchmarks that referenced this pull request Oct 21, 2020
* aspcore: add platform multiple queries

* Add missing console output

* tweak postgresql connectionstring

* aspnetcore: ef updates

Co-authored-by: Stefan Negulescu <[email protected]>
@n-stefan
n-stefan deleted the ef-updates branch November 16, 2023 15:07
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants