Share a fast Json (reverse) serialization open-source project Jil and jsonjil
We do not lack the JSON serialization library, but we lack a library with excellent performance, which is very important for websites. Today I found Jil.
He is an open source code: https://github.com/kevin-montrose/Jil
On his homepage, I will not describe its performance in detail. His most important feature is performance. Of course, Emit will not be less, if you want him to surpass other libraries and one Emit, there will be many other optimizations.
To introduce GC pressure, he used functional buffers such as builder. CommonCharBuffer and builder. CommonStringBuffer.
Many methods are marked with [MethodImpl (MethodImplOptions. AggressiveInlining)] so that the compiler can inline as much as possible.
For example, if int is converted to string, the original code is written in this way.
1 [MethodImpl(MethodImplOptions.AggressiveInlining)] 2 static void _CustomWriteInt(TextWriter writer, int number, char[] buffer) 3 { 4 // Gotta special case this, we can't negate it 5 if (number == int.MinValue) 6 { 7 writer.Write("-2147483648"); 8 return; 9 }10 11 var ptr = InlineSerializer<object>.CharBufferSize - 1;12 13 var copy = number;14 if (copy < 0)15 {16 copy = -copy;17 }18 19 do20 {21 var ix = copy % 10;22 copy /= 10;23 24 buffer[ptr] = (char)('0' + ix);25 ptr--;26 } while (copy != 0);27 28 if (number < 0)29 {30 buffer[ptr] = '-';31 ptr--;32 }33 34 writer.Write(buffer, ptr + 1, InlineSerializer<object>.CharBufferSize - 1 - ptr);35 }
Do you think it is good, but they are still making progress, and they are changed to the following:
1 [MethodImpl(MethodImplOptions.AggressiveInlining)] 2 static void _CustomWriteInt(TextWriter writer, int number, char[] buffer) 3 { 4 var ptr = InlineSerializer<object>.CharBufferSize - 1; 5 6 uint copy; 7 if (number >= 0) 8 copy = (uint)number; 9 else10 {11 writer.Write('-');12 copy = 1 + (uint)~number;13 }14 15 do16 {17 var ix = copy % 100;18 copy /= 100;19 20 var chars = DigitPairs[ix];21 buffer[ptr--] = chars.Second;22 buffer[ptr--] = chars.First;23 } while (copy != 0);24 25 if (buffer[ptr + 1] == '0')26 ++ptr;27 28 writer.Write(buffer, ptr + 1, InlineSerializer<object>.CharBufferSize - 1 - ptr);29 }
What is DigitPairs?
1 struct TwoDigits 2 { 3 public readonly char First; 4 public readonly char Second; 5 6 public TwoDigits(char first, char second) 7 { 8 First = first; 9 Second = second;10 }11 }12 13 private static readonly TwoDigits[] DigitPairs;14 15 static Methods()16 {17 DigitPairs = new TwoDigits[100];18 for (var i=0; i < 100; ++i)19 DigitPairs[i] = new TwoDigits((char)('0' + (i / 10)), (char)+('0' + (i % 10)));20 }
Is there a high idea?
The original method is as follows:
1 static bool IsWhiteSpace(int c) 2 { 3 // per http://www.ietf.org/rfc/rfc4627.txt 4 // insignificant whitespace in JSON is defined as 5 // \u0020 - space 6 // \u0009 - tab 7 // \u000A - new line 8 // \u000D - carriage return 9 10 return11 c == 0x20 ||12 c == 0x09 ||13 c == 0x0A ||14 c == 0x0D;15 }
You can change it like this. Haha, actually I want to change it like this. I don't know, right? Because I don't think it is blank in most cases, I have to judge it four times before returning it, and I changed it to this:
1 static bool IsWhiteSpace(int c) 2 { 3 // per http://www.ietf.org/rfc/rfc4627.txt 4 // insignificant whitespace in JSON is defined as 5 // \u0020 - space 6 // \u0009 - tab 7 // \u000A - new line 8 // \u000D - carriage return 9 10 return11 c < 0x21 && (12 c == 0x20 ||13 c == 0x09 ||14 c == 0x0A ||15 c == 0x0D);16 }
A special enough json deserialization
Under picurl, we can still forcibly convert newtonsoft. json object processing, but we only need to set one or more layers of structure. If you encounter "xxxx": [...], which contains brackets, it can be processed according to the json array.
The source code of a third-party library for json serialization and deserialization of an iOS complex entity-class object.
This is a demo of serialization and deserialization. You can refer to the following and it is relatively simple to use.