Skip to content

Instantly share code, notes, and snippets.

View ahsonkhan's full-sized avatar
💭
Working to empower every developer on the planet to achieve more.

Ahson Khan ahsonkhan

💭
Working to empower every developer on the planet to achieve more.
  • Microsoft
View GitHub Profile
@ahsonkhan
ahsonkhan / JsonEscapingCheck.md
Last active November 30, 2020 07:26
Optimize the check to see whether a JSON string contains characters that need to be escaped.

The default behavior of the System.Text.Json JsonSerializer and Utf8JsonWriter follows the behavior of JavascriptEncoder.Default, which means the following character sets are escaped:

  • Any non-ascii character
  • Characters required escaping based on the JSON RFC
  • HTML-specific ascii characters

Current Approach

The current approach involves going through the list of characters, one at a time, and check whether that character needs escaping. We special case the "null" encoder (which is the default when the user doesn't specify their own) and use a 256 byte bit-mask to indicate which character needs to be escaped. https://github.com/dotnet/corefx/blob/52d63157c78c31816b81be1599a5dacf96b5e5ca/src/System.Text.Json/src/System/Text/Json/Writer/JsonWriterHelper.Escaping.cs#L82-L109

New Approach Chosen

@ahsonkhan
ahsonkhan / SpanSliceIndexOf.md
Last active March 11, 2017 02:12
Comparing Span Slice then IndexOf vs using an IndexOf overload that takes index and count

Here are the latest results and conclusion.

Essentially, for Span specifically, calling Slice then IndexOf is just as good (if not better) than calling the IndexOf overload which removes the need to Slice.

dotnet/corefxlab#1278

(I am limiting the discussion to fast span since the conclusion doesn’t generally change for slow span)

image