Fluorinefx's amfwriter implementation is too careless...

Source: Internet
Author: User

Two days ago, I planned to create an amf3 adapter for the component to communicate with flash. After understanding it, I found that fluorinefx was in.. net. out of curiosity, I read the related information.CodeAs we only need to use serialization, we only pay attention to amfwriter. From the implementation code, amfwriter basically does not consider the gc pressure under high concurrency, the Data Writing Process is basically through the new byte [] replication method. the fluorinefx serialized object is analyzed in memory for further steps.

The results made me confused.

Some unexpected objects appear at the front of the loss column... so the overhead of these two objects is found through detailed tracking, both of which are derived from the amfwriter. getmember method. Looking at this method, I fainted on the spot:

 internal object getmember (object instance, classmember member) {If (instance is asobject) {asobject Aso = instance as asobject; if (ASO. containskey (member. name) return Aso [member. name];} type = instance. getType (); If (member. membertype = membertypes. property) {propertyinfo property = type. getproperty (member. name, member. bindingflags); return property. getvalue (instance, null);} If (member. membertype = membertypes. field) {fieldinfo field = type. getfield (member. name, member. bindingflags); Return field. getvalue (instance);} string MSG = _ res. getstring (_ res. reflection_membernotfound, String. format ("{0 }. {1} ", type. fullname, member. name); throw new fluorineexception (MSG) ;}

the method for getting the Member value is actually getproperty or getfield every time, so the reason why propertyinfo [] occupies the first place in the test memory result is. in fact, both propertyinfo and fieldinfo are thread-safe. so we made a simple adjustment.

Internal object getmember (object instance, classmember member) {If (instance is asobject) {asobject Aso = instance as asobject; If (ASO. containskey (member. name) return Aso [member. name];} type = instance. getType (); If (member. membertype = membertypes. property) {If (member. property = NULL) member. property = type. getproperty (member. name, member. bindingflags); Return member. property. getvalue (instance, null);} If (member. membertype = membertypes. field) {If (member. field = NULL) member. field = type. getfield (member. name, member. bindingflags); Return member. field. getvalue (instance);} string MSG = _ res. getstring (_ res. reflection_membernotfound, String. format ("{0 }. {1} ", type. fullname, member. name); throw new fluorineexception (MSG );}

the writeamf3utf problem is also found in the Code, So I adjusted it.

Static void _ writeamf3utf (string value, amfwriter writer) {lock (_ lockwriterstring) {If (value = string. empty) {writer. writeamf3integerdata (1);} else {If (! Writer. _ stringreferences. containskey (value) {writer. _ stringreferences. add (value, writer. _ stringreferences. count); int bytecount = encoding. utf8.getbytes (value, 0, value. length, encodingdata, 0); int handle = bytecount; handle = handle <1; handle = handle | 1; writer. writeamf3integerdata (handle); If (bytecount> 0) writer. write (encodingdata, 0, bytecount);} else {int handle = (INT) writer. _ string References [value]; handle = handle <1; writer. writeamf3integerdata (handle) ;}}// <summary> // writes a UTF-8 string to the current position in the AMF stream. /// </Summary> /// <Param name = "value"> the UTF-8 string. </param> // <remarks> standard or long string header is not written. </remarks> Public void writeamf3utf (string value) {_ writeamf3utf (value, this); // If (value = string. empty) // {// Writeamf3integerdata (1); //} // else // {// If (! _ Stringreferences. containskey (value) // {// _ stringreferences. add (value, _ stringreferences. count); // utf8encoding utf8encoding = new utf8encoding (); // int bytecount = utf8encoding. getbytecount (value); // int handle = bytecount; // handle = handle <1; // handle = handle | 1; // writeamf3integerdata (handle ); // byte [] buffer = utf8encoding. getbytes (value); // If (buffer. length> 0) // write (buffer); //} // else // {// int handle = (INT) _ stringreferences [value]; // handle = handle <1; // writeamf3integerdata (handle );//}//}}

From the implementation code of the entire amfwriter, it is no wonder that Ms is launching because the author has no awareness of memory overhead. net, it tells us that we don't need to worry about memory, and we will fix it...

Compare the efficiency of adjusting 10000 orders before and after serialization.

In general, every data Writing Method in amfwriter has room for optimization. It seems that fluorinefx has commercial support on the official website, but the code modification date is a long time ago, I was a little surprised to realize this.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.