Open
Description
Background and motivation
The various *HeaderValue types defined in System.Net.Http.Headers provide essential functionality to parse incoming headers reliably. Unfortunately, these types are one-shot only, meaning that new instances of the types need to be allocated for every header being parsed for all incoming requests. This leads to garbage and extra collections, slowing down the overall throughput of the service.
API Proposal
I recommend extending the System.Net.Http.Headers types in two ways:
- Make the types implement the new IResettable interface to clear their state back to defaults.
- Add an instance version of their TryParse methods which parses directly into the existing instance, as opposed to the existing static TryParse method which always returns a new instance.
So taking CacheControlHeaderValue as an example:
public class CacheControlHeaderValue : IResettable
{
// implement the IResettable interface
public bool TryReset();
// add a new instance version of TryParse
public bool TryParse(ReadOnlySpan<char> input)
{
// start from scratch
TryReset();
bool parsedOK = DoParsing();
if (!parsedOK)
{
// clean up the mess in case of failure
TryReset();
}
return parsedOK;
}
API Usage
var cchv = new CacheControlHeaderValue();
if (cchv.TryParse(hdr1))
{
// do something with cchv
}
if (cchv.TryParse(hdr2))
{
// do something with cchv
}
Alternative Designs
No response
Risks
No response