Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a binary constant mode to StringValues #64

Closed
lodejard opened this issue Nov 20, 2015 · 3 comments
Closed

Add a binary constant mode to StringValues #64

lodejard opened this issue Nov 20, 2015 · 3 comments
Assignees
Milestone

Comments

@lodejard
Copy link

Something like the following

        public StringValues(string value, Encoding encoding)
        {
            _value = value;
            _values = null;
            _bytes = encoding.GetBytes(value);
            _encoding = encoding;
        }

        public static StringValues CreateBinaryConstant(string value, Encoding encoding)
        {
            return new StringValues(value, encoding);
        }

        public bool TryGetBinaryConstant(out byte[] bytes, out Encoding encoding)
        {
            bytes = _bytes;
            encoding = _encoding;
            return _bytes != null;
        }

would enable an application to contain

  static readonly StringValues _textPlain = StringValues.CreateBinaryConstant("text/plain", Encoding.ASCII);

// and later
    httpResponse.Headers["Content-Type"] = _textPlain;

this would enable a server to optimize the header output

  byte[] bytes; Encoding encoding;
  if (theHeaderValues.TryGetBinaryConstant(out bytes, out encoding) && encoding == _ascii)
  {
    // blit "\r\nThe-Header: " 
    // blit {bytes}
  }
  else
  {
    foreach(var value in headerValues)
    {
      // blit "\r\nThe-Header: "
      // ascii encode {value}
    }
  }
@benaadams
Copy link
Member

Related aspnet/KestrelHttpServer#169

@davidfowl
Copy link
Member

The comparison to ascii isn't great. You could have newed up your own encoder with options. We need a type test or maybe an enumeration

@JunTaoLuo
Copy link

From benchmarks tests, it was found that this optimization is no longer relevant after all the optimization made in Kestrel. Will close for now, see aspnet/KestrelHttpServer#584 for details.

natemcmaster pushed a commit that referenced this issue Nov 7, 2018
Using 'nameof' operator instead of magic strings
@ghost ghost locked as resolved and limited conversation to collaborators Dec 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants