-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Open
Labels
Needs: DesignThis issue requires design work before implementating.This issue requires design work before implementating.area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etc
Milestone
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
Found while investigating #46154
The app below has 30,000 endpoints. The first request to /
takes 0.5 seconds with 280 MB memory usage:
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Http.Metadata;
using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.Use(async (HttpContext context, Func<Task> next) =>
{
Console.WriteLine("Start time");
Stopwatch stopwatch = Stopwatch.StartNew();
await next();
stopwatch.Stop();
Console.WriteLine(stopwatch.Elapsed.TotalSeconds);
});
app.UseRouting();
Task Plaintext(HttpContext context) => context.Response.WriteAsync("Hello, World!");
for (int i = 0; i < 30_000; i++)
{
var url = "/plaintext/nested" + i;
app.MapGet(url, Plaintext);
}
app.MapGet("/", (HttpContext context) =>
{
return context.Response.WriteAsync("Hello world");
});
Console.WriteLine("Running app");
app.Run();
If I change the Plaintext
endpoint to be a minimal API (aka use RequestDelegateFactory
) like so:
- Task Plaintext(HttpContext context) => context.Response.WriteAsync("Hello, World!");
+ string Plaintext() => "Hello, World!";
With 30,000 minimal APIs, it now takes 32 seconds to get the first request. And memory usage is 1,065 MB.
Expected Behavior
I expect a fast startup time.
I think the problem is RequestDelegateFactory
is building and compiling expressions for all minimal API endpoints when routing starts. Creating a minimal API's expression should be lazy and wait until an endpoint is first visited.
Steps To Reproduce
No response
Exceptions (if any)
No response
.NET Version
No response
Anything else?
No response
Metadata
Metadata
Assignees
Labels
Needs: DesignThis issue requires design work before implementating.This issue requires design work before implementating.area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etc