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 / ArrayBufferWriter.ArrayPool.cs
Created January 29, 2019 01:11
Sample implementation of ArrayBufferWriter, a class that implements IBufferWriter<byte> backed by ArrayPool<byte>.Shared.
public class ArrayBufferWriter : IBufferWriter<byte>, IDisposable
{
private byte[] _rentedBuffer;
private int _written;
private long _committed;
private const int MinimumBufferSize = 256;
public ArrayBufferWriter(int initialCapacity = MinimumBufferSize)
{
@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 / CaseSensitive.cs
Created January 28, 2020 00:47
Benchmark for System.Text.Json Deserialize with various casing approaches
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
using System.Collections.Generic;
namespace System.Text.Json.Serialization.Tests
{
@ahsonkhan
ahsonkhan / Json-Poco-BlogPost.cs
Created July 25, 2019 23:13
Generating a .NET POCO for a json file found on the web (some blog post)
public class Rootobject
{
public Value value { get; set; }
public Mentioneduser[] mentionedUsers { get; set; }
public Collaborator[] collaborators { get; set; }
public bool hideMeter { get; set; }
public Collectionuserrelation[] collectionUserRelations { get; set; }
public object mode { get; set; }
public References references { get; set; }
}
@ahsonkhan
ahsonkhan / Scenario1_expected.json
Last active June 19, 2019 17:52
The expected output
// 1b) Expected output:
{"Email":"james@example.com","Active":true,"CreatedDate":"2013-01-20T00:00:00.0000000Z","Roles":["User","Admin"]}
// 2c) Expected output:
{
"Email": "james@example.com",
"Active": true,
"CreatedDate": "2013-01-20T00:00:00Z",
"Roles": [
@ahsonkhan
ahsonkhan / Class_Utf8JsonWriter.md
Created April 17, 2019 12:07
Utf8JsonWriter Re-design based on usability and reliability feedback

Motivation - Usability and Reliability:

  • The current writer is a ref struct, which requires passing by ref.
  • The use of array pool could result in reliability concerns if misused.
  • Async writes and state passing could be problematic.
  • No IBufferWriter implementation built in.

Goals:

  • Continue to support IBufferWriter (i.e. PipeWriter) directly
  • Keep ability for user to control buffering with ability to avoid data copies.
@ahsonkhan
ahsonkhan / CallStack-StackOverflow.txt
Created February 27, 2019 01:47
ApiCompat write diff stack overflow for uap
[External Code]
> Microsoft.Cci.Extensions.dll!Microsoft.Cci.Writers.CSharp.CSDeclarationWriter.WriteEnumValue(Microsoft.Cci.IMetadataConstant constant, Microsoft.Cci.ITypeReference constantType) Line 67 C#
Microsoft.Cci.Extensions.dll!Microsoft.Cci.Writers.CSharp.CSDeclarationWriter.WriteMetadataConstant(Microsoft.Cci.IMetadataConstant constant, Microsoft.Cci.ITypeReference constantType) Line 258 C#
Microsoft.Cci.Extensions.dll!Microsoft.Cci.Writers.CSharp.CSDeclarationWriter.WriteEnumValue(Microsoft.Cci.IMetadataConstant constant, Microsoft.Cci.ITypeReference constantType) Line 123 C#
Microsoft.Cci.Extensions.dll!Microsoft.Cci.Writers.CSharp.CSDeclarationWriter.WriteMetadataConstant(Microsoft.Cci.IMetadataConstant constant, Microsoft.Cci.ITypeReference constantType) Line 258 C#
Microsoft.Cci.Extensions.dll!Microsoft.Cci.Writers.CSharp.CSDeclarationWriter.WriteEnumValue(Microsoft.Cci.IMetadataConstant constant, Microsoft.Cci.ITypeReference constantType) Line 123 C#
Microsoft.Cci.Extensions.dll!M
@ahsonkhan
ahsonkhan / ref-api-diff.md
Last active February 22, 2019 08:44
Difference in reference assemblies between .NET Core 3.0 master and .NET Core 3.0 UpdateRefs branch
@ahsonkhan
ahsonkhan / ArrayBufferWriter.cs
Created January 28, 2019 08:10
Sample implementation of ArrayBufferWriter, a class that implements IBufferWriter<byte>
namespace System.Buffers
{
public class ArrayBufferWriter : IBufferWriter<byte>, IDisposable
{
private byte[] _rentedBuffer;
private int _written;
private long _committed;
private const int MinimumBufferSize = 256;
@ahsonkhan
ahsonkhan / JsonReaderPerf-report-github.md
Last active May 12, 2018 00:51
JsonLab and Json.NET JsonReader Performance Comparison Results for various JSON strings (UTF-8 and UTF-16)
BenchmarkDotNet=v0.10.14.534-nightly, OS=Windows 10.0.16299.371 (1709/FallCreatorsUpdate/Redstone3)
Intel Xeon CPU E5-1620 v2 3.70GHz, 1 CPU, 8 logical and 4 physical cores
Frequency=3604598 Hz, Resolution=277.4234 ns, Timer=TSC
.NET Core SDK=2.1.300-rtm-008823
  [Host]     : .NET Core 2.1.0-rtm-26508-02 (CoreCLR 4.6.26508.04, CoreFX 4.6.26508.03), 64bit RyuJIT
  Job-JIQQBR : .NET Core 2.1.0-rtm-26508-02 (CoreCLR 4.6.26508.04, CoreFX 4.6.26508.03), 64bit RyuJIT

InvocationCount=1024 TargetCount=5 WarmupCount=3