Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

[Perf] Add ContentLength to IHeaderDictionary #757

Merged
merged 1 commit into from
Jan 24, 2017
Merged

Conversation

Tratcher
Copy link
Member

@Tratcher Tratcher commented Jan 17, 2017

#407 Headers are stored as strings, but multiple components may need to examine the Content-Length header value on a request or response. To avoid redundant conversions allow both representations to be stored. The IHeaderDictionary implementations are responsible for keeping the representations in sync. I did not attempt any optimizations in the default implementations, they are primarily used for testing. The real gains will come from the WebListener and Kestrel implementations.

Do not merge this until the WebListener, Kestrel, and Hosting implementations are ready.

@benaadams Does this give you enough of an abstraction to implement in Kestrel? Do you want to take the first stab at it? The current xproj vs csproj schism may complicate matters.

@Tratcher Tratcher added this to the 2.0.0 milestone Jan 17, 2017
@Tratcher Tratcher self-assigned this Jan 17, 2017
@Tratcher Tratcher requested a review from halter73 January 17, 2017 21:39
@@ -17,5 +17,10 @@ public interface IHeaderDictionary : IDictionary<string, StringValues>
/// <param name="key"></param>
/// <returns>The stored value, or StringValues.Empty if the key is not present.</returns>
new StringValues this[string key] { get; set; }

/// <summary>
/// Strongly typed access to the Content-Type header. Implementations must keep this in sync with the string representation.

Choose a reason for hiding this comment

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

Content-Length

get
{
long value;
var rawValue = this[HeaderNames.ContentLength];
Copy link
Contributor

@JunTaoLuo JunTaoLuo Jan 17, 2017

Choose a reason for hiding this comment

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

So the value in the string is being parsed every time from the string? Don't we want to store this as a long somewhere that's being kept in sync with the value stored in the string dictionary?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this is the same as before. Kestrel and WebListener will provide more optimized versions of IHeaderDictionary. I didn't optimize this one because it's primarily used in tests.

Copy link
Contributor

@JunTaoLuo JunTaoLuo Jan 17, 2017

Choose a reason for hiding this comment

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

Ah yes, I should have read your first comment more carefully before looking at the code.

@@ -97,6 +98,34 @@ public HeaderDictionary(int capacity)
set { this[key] = value; }
}

public long? ContentLength
{
get
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like duplication here and in DictionaryStringValuesWrapper. Should we keep this in a helper method like before and just call it from the getter and setter of the ContentLength properties.

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought about it but the implementation is actually different, their internal stores work a little differently.

@benaadams
Copy link
Contributor

@Tratcher looks good; will have a shot

@benaadams
Copy link
Contributor

Probably should be ulong but nevermind 😄

@halter73
Copy link
Member

I think ulong is a good idea. I mind.

@benaadams
Copy link
Contributor

Is already exposed as long? on Request and Response, assume changing them to ulong? would be a breaking change?

@halter73
Copy link
Member

Hadn't considered that. I guess we should stay consistent then. I think 63 usable bits should be enough. We'll just need to keep guarding against negative values.

@benaadams
Copy link
Contributor

benaadams commented Jan 20, 2017

HeaderUtilities.TryParseInt64 will return false for a value with a sign in it, so shouldn't get negative values

@halter73
Copy link
Member

Interesting that HeaderUtilities.FormatInt64 will create a string for a negative number even though HeaderUtilities.TryParseInt64 would throw trying to parse it.

benaadams added a commit to benaadams/KestrelHttpServer that referenced this pull request Jan 23, 2017
@Tratcher Tratcher force-pushed the tratcher/contentlength branch from 001fc5d to 779115b Compare January 24, 2017 23:17
@Tratcher Tratcher merged commit 779115b into dev Jan 24, 2017
@Tratcher Tratcher deleted the tratcher/contentlength branch January 24, 2017 23:18
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.

6 participants