About the author

Miron Abramson
Me
Software Engineer,
CTO at PixeliT
and .NET addicted for long time.
Open source projects:
MbCompression - Compression library

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2014

Creative Commons License

Blog Flux Directory
Technology Blogs - Blog Top Sites

Fast version of the Activator.CreateInstance method using IL

We all know (I guess) the method Activator.CreateInstance(Type) and the generic version:  Activator.CreateInstance<T>()  that are used to create an instance of a specified Type.

In now days all are talking about 'Entity-relationship model' , 'Object-relational mapping' and that stuff, that basically it means getting a IDataReader, loop over it and fill with the data a List with specfied type of objects and return a List instead of DataSet or DataTable (or similar mechanism). While looping over the IDataReader, every iterate, an instance of the specified type needs to be create. It can be done using one of the methods above, but it can be improved by usind IL and some help using some cache. Here is how can it be done:

public static class FastObjectFactory
{
    private static readonly Hashtable creatorCache = Hashtable.Synchronized(new Hashtable());
    private readonly static Type coType = typeof(CreateObject);
    public delegate object CreateObject();

    /// <summary>
    /// Create an object that will used as a 'factory' to the specified type T 
   /// <returns></returns>
    public static CreateObject CreateObjectFactory<T>() where T : class
    {
        Type t = typeof(T);
        FastObjectFactory.CreateObject c = creatorCache[t] as FastObjectFactory.CreateObject;
        if (c == null)
        {
            lock (creatorCache.SyncRoot)
            {
                c = creatorCache[t] as FastObjectFactory.CreateObject;
                if (c != null)
                {
                    return c;
                }
                DynamicMethod dynMethod = new DynamicMethod("DM$OBJ_FACTORY_" + t.Name, typeof(object), null, t);
                ILGenerator ilGen = dynMethod.GetILGenerator();

                ilGen.Emit(OpCodes.Newobj, t.GetConstructor(Type.EmptyTypes));
                ilGen.Emit(OpCodes.Ret);
                c = (CreateObject)dynMethod.CreateDelegate(coType);
                creatorCache.Add(t, c);
            }
        }
        return c;
    }
}

Note the static HashTable is been used as a cache. The first  time we create a delegate 'CreateObjec' for the given type it is 'slow', so all the point here, is to cache this delegate. The next time we need to create an object from the given type, the 'CreateObjec' delegate will be used from the cache. Without caching it, the whole story worth nothing.

Here are some benchmarks:

1 Object:



100 Objects:

1000 Objects:

Comparing to the generic constraint : new()

Below there is the small project I used for the benchmarks:

FastObjectFactory.zip (50.35 kb)

Currently rated 5.0 by 6 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: C# | Performance | IL
Posted by Miron on Saturday, August 09, 2008 12:52 PM
Permalink | Comments (19) | Post RSSRSS comment feed

High performance css minifier

It is known that minifying the JavaScript and CSS files can really reduce the files size and improve the general site performance. Lot of sites (BlogEngine.NET among them)  do it on run-time and not it the build time. At this point, I want to recommend the JavaScript minifier JSMIN by Douglas Crockford. It does the job very good, and by far, much faster than all the RegularExpression \ Replace minifiers. Because in my Compression project MbCompression I do the minifying on run-time, I decided to use jsmin minifier.

The Css minifier

All the Css minifiers I found are using Regular Expression\ Replace to remove the unneeded characters. This is working fine, but have a realy bad performance, special on run-time. The speed is slow (special using the Regular Expression), and another important thing is, every Replace creates a very big string in the memory! (Strings are immutable, remember?), so if you have several Replaces, it creates several big strings in the server memory!

To improve performance, I took  JSMIN idea, that are not using any strings in the memory, and perform the minifying much faster, and created a CSS minifier that produce a small CSS file, much faster, and with much less memory overhead.

This minifier was tested on several CSS files. Feel free to download it and use it. If there is any file that not been minify correctly, send it to me, and I will try to improve the minifier.

CssMinifier.cs (9.59 kb)

Currently rated 4.9 by 12 people

  • Currently 4.916667/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: C# | Client side | Performance
Posted by Miron on Sunday, June 29, 2008 4:48 AM
Permalink | Comments (19) | Post RSSRSS comment feed

Update to my compression module to compress third party scripts

My last version of the compression module was working great and able to compress and cache pages, WebResources, CSS and JavaScripts files. It have one problem (that similiar to most of the compression modules you can find). To compress the javascripts files, you have to modify the way you register them and change their links to something like that: 

<script type="text/javascript" src="/Scripts/utils.js">

needs to became 

<script type="text/javascript" src="Scripts/jslib.axd?d=~/Scripts/utils.js">

It is not so hard work to do, but it became useless when you use third party controls as 'Telerik' (RadControls)  and force it to doesn't use it's scripts as WebResources. In such case, the controls 'Injects' their scripts links into the HTML code and you can't modify them as needed to let the compression handler to compress it.  In the new version of my compression component, I added a ResponseFilter that parse the Scripts links in the HTML and convert them into 'compressable' links. By default, this option is disabled in the compression module. To enable it (if you use such third party controls), just add the attribute compressThirdParityScripts="true" to the compression configuration.

Full implementation instructions can be found here: Compression module implementation 

Latest code can be downloaded from: http://www.codeplex.com/MbCompression

Currently rated 4.8 by 5 people

  • Currently 4.8/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by Miron on Friday, February 22, 2008 4:15 AM
Permalink | Comments (7) | Post RSSRSS comment feed

New & Shiny WebResource.axd compression Module

This project is now an Open source project on  CodePlex web site. Latest code and updates can be found at  http://www.codeplex.com/MbCompression

Background

Running after the best performance for your web application, one of the biggest improvement you can do is to use compression. Page, Stylesheet & Javascript files (images are already compressed format). There are few compression modules out there, all you needs to do is just 'goolge' for it, and the implementation is really very simple. Not a lot of changes in your code are needed.

ASP.NET bring us a new way to integrate resources into our site - using WebResource.axd handler. Those are like a 'Virtual containers' for other file that embedded into a DLL file. It can be Css, Javascript, Images... Actually this is an handler that load the resource from the DLL file. 

The Problem

The pages, .css & .js files can be easily compress with one of the modules on the net. The basic technique is to pass your response through 'Compression Filter'.

WebResource.axd, that became very large (specially when using AJAX) are break down for unknown reason when you try to compress them that way. That result is you will not compress them, and your client will download big files that contains js or css (sometimes of 70kb and even more)

The Challenge

I couldn't find on the net a compression module that compress this type of file. I decided I will try to do it by myself.

First Try.  Working, but bad performance. ( Compress pages and WebResource.axd files in ASP.NET )

I started to make tests and to play with WebResources files. Only WebResources that contains 'text' content (css, js, text) can be consider as compressible. Images are not.

I was thinking, maybe I can load the WebResource, get it's content as string, compress the string and send it to the response as string (or as byte[]). After some tests, It discovered as true. The WebResource was passing to the client completely and without any missing characters. The problem was to get the WebResource content. For that, I had to make a 'fake' request based on the url, asking again for the WebResource and then compress it.

HttpWebResponse request = (HttpWebRequest)WebRequest.Create(app.Context.Request.Url.OriginalString + "&p=1");
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream responseStream = response.GetResponseStream();

That make every time the client ask for webresource, the server made double HTTP request - what a waste.

This module was working. It was actually compressing the WebResources, But suffer from bad performance.

Second Try. Working better. But not enough. ( Compress your pages, css, js and WebResources.axd files for better performance )

Thanks to Mads Kristensen that point me to the performance issue and offer an idea how to improve it, The second try was better. The first time the WebResource was loading, it was compressed and inserted into the Application object, and next time it was requested, it was served from there directly.

It was actually running on our production servers (Where I work) for a while, and compressed the WebResources. But I was not happy with that solution. The 'fake' request throw an exception once in a while. Maybe one for some thousands of users. Also it was not stable enough on some other servers, and throw 404 errors.

Third Try.  Success! Working as expected. No errors and really WebResource compression module!

The compression part in the first two tries was perfect. compress the content of the webresource as byte[] and serve it to the client. The only problem is to load the actual resource content.

In this time, I decided to 'attack' the problem from another way. .NET using System.Web.Handlers.AssemblyResourceLoader to load and serve WebResource requests. I decided to implement my own AssemblyResourceLoader handler that will support compression. unfortunately,  AssemblyResourceLoader handler is registered into the system, and can't be remove. Any try to do so will lead you to an exception.

So, what I did was to create an HttpModule, using the event PreRequestHandlerExecute that is one step before the handle action, do my stuff here, and then end response and jump over the hander step. That way I can prevented AssemblyResourceLoader handler to do his stuff.

To create my version for the AssemblyResourceLoader, I used the source code of 'ASP.NET 2.0 AJAX Extensions' and a reflector to see the code of the original AssemblyResourceLoader.

There are few steps in that module:

1.  Parsing the request query string. The request for WebResource has an encrypted query string with the the resource data separate with pipes chars ('|'):     Assemby info,  Resource name and a char for the 'assembly type'. The query string is encrypted using the machinekey, and needs to be decrypted using reflection. The problem is, that share hosting servers (such GoDaddy) are not permitting using reflection from within your code. Only from assemblies that are in the GAC. What I had to do is to find a public method from an assembly that in the GAC of every server.

The solution I found (if somebody have a better idea, I will be glad to hear) is creating an empty Membership class, that inherits from MembershipProvider, and implement only one public method that using the 'DecryptPassword' method from the  MembershipProvider. it is also using the machinekey.   (To use that decryption, add the attribute reflectionAlloweded="false" to the CompressorSettings section, and machinekey section to the web.config. See Readme.txt file in the source)

private static MethodInfo _decryptString;
private static readonly Object _getMethodLock = new Object();

internal static string DecryptString(string input)
{
    if (Settings.Instance.ReflectionAlloweded)
    {
        if (_decryptString == null)
        {
            lock (_getMethodLock)
            {
                if (_decryptString == null)
                {
                    _decryptString = typeof(Page).GetMethod("DecryptString", BindingFlags.Static | BindingFlags.NonPublic);
                }
            }
        }
        return (string)_decryptString.Invoke(null, new object[] { input });
    }
    else
    {
        return EmptyMembership.Instance.DecryptString(input);
    }
}

2.  Load the assembly. The char for the 'assembly type' in the query string indicate if the assembly is the System.Web assembly or the one that encrypted in the query.

3.  Load the resource info. Using the resource name from the query, we need to load the resource info - it's content type, if it exist and if it needs to perform substitution calls      (WebResource within another WebResource. If so, we let the original System.Web.Handlers.AssemblyResourceLoader to handle this WebResource). Of course we check in our cache if that resource already asked for, and load the info from the cache.

4.  Setting the HttpCachePolicy headers to cache the request, and adding a ETag for checking in the next time.

5.  Get the resource stream from the assembly.

    Stream resourceStream = assembly.GetManifestResourceStream(resourceName)

6. Compress the resource stream if needed. If the resource is 'compressable' it can be read as a string, so we load it as StreamReader, read it to the end, convert the string into byte[] and compress it. If not (in case of images) we just load the resource, and serve it to the client.

     public static byte[] Compressor(byte[] buffer, string encodingType)
     {
         using (MemoryStream memStream = new MemoryStream())
         {
             Stream compress = null;

             // Choose the compression type and make the compression
             if (String.Equals(encodingType, "gzip", StringComparison.Ordinal))
             {
                 compress = new GZipStream(memStream, CompressionMode.Compress);
             }
             else if (String.Equals(encodingType, "deflate", StringComparison.Ordinal))
             {
                 compress = new DeflateStream(memStream, CompressionMode.Compress);
             }
             else
             {
                 // No compression
                 return buffer;
             }

             compress.Write(buffer, 0, buffer.Length);
             compress.Dispose();

             return memStream.ToArray();
         }
     }

7.  Last thing to do, is to send the byte[] (compressed or not to the response stream, and end the response.

      HttpContext.Response.OutputStream.Write(compressedData, 0, compressedData.Length);

Optional Improvements for the WebResource module (maybe in the feature) 

  • Storing the compressed resource in the HttpContext.Cache
  • Compress using the preferred compression algorithm by the client (parsing the 'Accept-encoding' header)
  • Remove white spaces & comments from css & Javascript resources

Tips when working with WebResources

  • Add to your robots.txt file the following:
    User-agent: *
    Disallow: /*.axd$
  • To prevent the search engines to cache the axd handlers. (not 100% solution, but not harmful Cool)

  • Add a machinekey to your Web.Config. ASP.NET use it for validation and encryption/decryption of the view state & the WebResource query strings. Don't relay on the auto generate key. It can lead you to some Cryptographic Exceptions. Use one of the on-line tools to generate key for you. 

Implementation

Full instruction can be found in the readme.txt file in the source code.

The Source Code

The source code contain handlers to compress .js & .css files, simple module to compress pages, the module to compress the WebResources and helper classes. Every module/handler is 'stand alone' and can be use independent in case you choose to use your own implementation for the pages of css/js.

The Readme.txt file include all needed info to implement the handlers/modules.

Important: If you are using the WebResource compression module on share hosting thatnow allowed using reflection from your code (as GoDaddy), you must  add the attribute reflectionAlloweded="false" to the CompressorSettings section.

Latest code can be downloaded from: http://www.codeplex.com/MbCompression

See also post:  Update to my compression module to compress third party scripts 

Currently rated 4.5 by 15 people

  • Currently 4.466668/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by Miron on Saturday, October 13, 2007 5:59 AM
Permalink | Comments (137) | Post RSSRSS comment feed

Compress your pages, css, js and WebResources.axd files for better performance

(See updated post:  New & Shiny WebResource.axd compression Module

This post is an update for my previews post about http compression. In that post, I posted a http module that compress aspx pages and WebResource.axd files.
Thanks to Mads Kristensen, that found a performance issue in my code and had a solution for that, I improved my compressor component and also I included a complete project to compress asp.net pages, CSS files, JavaScript files and WebResource.axd files as well. The implementation in really very simple and not lot of changes are necessary in existing project. The page compression is very simple, and common to all http compressors:
 Registering the httpmodule in the web.config:

<add name="HttpCompressionModule" type="Miron.HttpHandlersAndModules.HttpCompressionModule"/>

and the compression itself in the httpmodule: 

HttpApplication app = sender as HttpApplication; 

// Check if GZIP is supported by the client

if (IsGzipEncodingSupported())
{
    app.Response.Filter = new GZipStream(app.Response.Filter, CompressionMode.Compress);

    SetEncodingType(GZIP);   // Set the response encoding type header
}
// Check if DEFLATE is supported by the client
else if (IsDeflateEncodingSupported())
{
    app.Response.Filter = new DeflateStream(app.Response.Filter, CompressionMode.Compress);

    SetEncodingType(DEFLATE);  // Set the response encoding type header
}

The WebResource.axd compression is a bit more complicate, but the idea is to make a 'fake' request based on the requested url when the client asked for WebResource.axd file, get the response into byte[], compress it into MemoryStream cache it as a byte[] and send it to the client. The next time the client ask for this WebResource, it served from the cache (thanks again to Mads Kirstensen). The compression is only when the WebResource serve css or javascript files. over wise, the WebResource is send to the client 'as it'.   The CSS and the JS files compression is similar to the page compression, the differents  are that in those cases, we load the requested file, read it into string, manipulate it  (remove unneeded spaces and stuff), compress it, cache it, and send it to the client. The 'problem' is that css and js files are served by the IIS itself, and not by ASP.NET engine, so what we do is, changing the 'src' attribute for js files to jslib.axd or the 'href' attribute for css files to css.axd, and add as query string the real files name (see in the ReadMe.txt file how it done), and those files are handled by ASP.NET engine and to compress tham - for that, we register the following handlers in the web.config: (another option, is to serve those files via WebResources)

<add verb="*" path="jslib.axd" type="Miron.HttpHandlersAndModules.JavaScriptHandler" validate="false"/>

<add verb="*" path="css.axd" type="Miron.HttpHandlersAndModules.CssCompressionHandler" validate="false"/>

The caching for those files have CacheDependency on the files, so the content is removed from the cache in the moment the files are changed, and reload again when requested. Before sending data to the client, we check the ETag, and if its the same as the client sent, so we can stop the response and send only the code 304. 

Extra features

By configuration in the web.config, it is possible to disable any compression type, decide the time (by days) the cached file will be in the cache, and also it is possible to specified mimeTypes content to not compress or specified aspx files to exclude from the compression.  All configuration description in the ReadMe.txt file. 

 Implementation 

All implementation instructions are in the attach zip file in ReadMe.txt file.

Note

The WebResource compression may not work if the request is url that modified in the windows 'host' file. In that case, you will need to disable WebResource compression (See in the  ReadMe.txt)

  (See update post with new & better code: New & Shiny WebResource.axd compression Module)

Latest code can be downloaded from:

http://www.codeplex.com/MbCompression

Currently rated 3.9 by 14 people

  • Currently 3.928571/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by Miron on Monday, September 24, 2007 8:17 AM
Permalink | Comments (17) | Post RSSRSS comment feed