From 064e2ae9f77ed0fd1fc41e99c4d6dd6d53174597 Mon Sep 17 00:00:00 2001 From: Ken Schlobohm Date: Wed, 5 May 2021 15:15:41 -0500 Subject: [PATCH] Updating performance-best-practices.md (#22192) * Updating performance-best-practices.md * Updates * Updates * Update aspnetcore/performance/performance-best-practices.md Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Luke Latham <1622880+guardrex@users.noreply.github.com> Co-authored-by: Luke Latham <1622880+guardrex@users.noreply.github.com> Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> --- .../performance/performance-best-practices.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/aspnetcore/performance/performance-best-practices.md b/aspnetcore/performance/performance-best-practices.md index 6dbe69e1cc..78dafdabf6 100644 --- a/aspnetcore/performance/performance-best-practices.md +++ b/aspnetcore/performance/performance-best-practices.md @@ -42,7 +42,22 @@ A common performance problem in ASP.NET Core apps is blocking calls that could b A profiler, such as [PerfView](https://github.com/Microsoft/perfview), can be used to find threads frequently added to the [Thread Pool](/windows/desktop/procthread/thread-pools). The `Microsoft-Windows-DotNETRuntime/ThreadPoolWorkerThread/Start` event indicates a thread added to the thread pool. -## Return IEnumerable\ or IAsyncEnumerable\ +## Return large collections across multiple smaller pages + +A webpage shouldn't load large amounts of data all at once. When returning a collection of objects, consider whether it could lead to performance issues. Determine if the design could produce the following poor outcomes: + +* or high memory consumption +* Thread pool starvation (see the following remarks on ) +* Slow response times +* Frequent garbage collection + +**Do** add pagination to mitigate the preceding scenarios. Using page size and page index parameters, developers should favor the design of returning a partial result. When an exhaustive result is required, pagination should be used to asynchronously populate batches of results to avoid locking server resources. + +For more information on paging and limiting the number of returned records, see: + + * [Performance considerations](xref:data/ef-rp/intro#performance-considerations) + * [Add paging to an ASP.NET Core app](xref:data/ef-rp/sort-filter-page#add-paging) +### Return `IEnumerable` or `IAsyncEnumerable` Returning `IEnumerable` from an action results in synchronous collection iteration by the serializer. The result is the blocking of calls and a potential for thread pool starvation. To avoid synchronous enumeration, use `ToListAsync` before returning the enumerable.