We don't lack a JSON serialization library, but we don't have a very good library, which is very important for a Web site. Today, I found Jil.
He's an open source code: Https://github.com/kevin-montrose/Jil
On his homepage There is a detailed description of its performance, I do not repeat here, his most important feature is the performance, emit that certainly not less, when want to let him beyond the other Kuguang a emit certainly not, he has a lot of other optimizations.
To introduce the pressure of GC, he used a function buffer such as Builder.commoncharbuffer, Builder.commonstringbuffer is also such an application.
Many of the methods are labeled [MethodImpl (methodimploptions.aggressiveinlining)] so that the compiler can inline as much as possible.
For example, to convert int to string, the original code is written in this way.
1 [MethodImpl (methodimploptions.aggressiveinlining)]2 Static void_customwriteint (TextWriter writer,intNumberChar[] buffer)3 {4 //Gotta special case This, we can ' t negate it5 if(Number = =int. MinValue)6 {7Writer. Write ("-2147483648");8 return;9 }Ten One varPTR = inlineserializer<Object. Charbuffersize-1; A - varcopy =Number ; - if(Copy <0) the { -copy =-copy; - } - + Do - { + varIX = copy%Ten; ACopy/=Ten; at -BUFFER[PTR] = (Char)('0'+ix); -ptr--; -} while(Copy! =0); - - if(Number <0) in { -BUFFER[PTR] ='-'; toptr--; + } - theWriter. Write (buffer, PTR +1, inlineserializer<Object. Charbuffersize-1-ptr); *}
I think it's a good thing, but they're still making progress and changing it like this:
1 [MethodImpl (methodimploptions.aggressiveinlining)]2 Static void_customwriteint (TextWriter writer,intNumberChar[] buffer)3 {4 varPTR = inlineserializer<Object. Charbuffersize-1;5 6 UINTcopy;7 if(Number >=0)8copy = (UINT) number;9 ElseTen { OneWriter. Write ('-'); Acopy =1+ (UINT)~Number ; - } - the Do - { - varIX = copy% -; -Copy/= -; + - varchars =Digitpairs[ix]; +buffer[ptr--] =chars. Second; Abuffer[ptr--] =chars. First; at} while(Copy! =0); - - if(Buffer[ptr +1] =='0') -++ptr; - -Writer. Write (buffer, PTR +1, inlineserializer<Object. Charbuffersize-1-ptr); in}
Among them, what is that digitpairs?
1 structtwodigits2 {3 Public ReadOnly CharFirst ;4 Public ReadOnly CharSecond;5 6 PublicTwodigits (CharFirstCharsecond)7 {8First =First ;9Second =second;Ten } One } A - Private Static ReadOnlytwodigits[] Digitpairs; - the StaticMethods () - { -Digitpairs =Newtwodigits[ -]; - for(varI=0; I < -; ++i) +Digitpairs[i] =NewTwodigits ((Char)('0'+ (I/Ten)), (Char)+('0'+ (i%Ten))); -}
Is the idea very high?
The original method was this.
1 Static BOOLIswhitespace (intc)2 {3 //perHttp://www.ietf.org/rfc/rfc4627.txt4 //insignificant whitespace in JSON is defined as5 //\u0020-space6 //\u0009-tab7 //\u000a-new Line8 //\u000d-carriage return9 Ten return Onec = =0x20|| Ac = =0x09|| -c = =0x0A|| -c = =0x0D; the}
Can change, haha, in fact, I want to change, do not know right? Because I think most of the time is not blank, so I have to judge 4 times to return, and I changed to this:
1 Static BOOLIswhitespace (intc)2 {3 //perHttp://www.ietf.org/rfc/rfc4627.txt4 //insignificant whitespace in JSON is defined as5 //\u0020-space6 //\u0009-tab7 //\u000a-new Line8 //\u000d-carriage return9 Ten return OneC <0x21&& ( Ac = =0x20|| -c = =0x09|| -c = =0x0A|| thec = =0x0D); -}