Featured Post

Simple factory for open dynamic delegates

During the course of my development adventures I came to a point where I needed to call instance methods dynamically based only on information that can be obtained through reflection. It is well known that MethodInfo.Invoke is quite slow, and so I decided to create and cache delegates based on the given...

Read More

Code Snippet of the day

Posted by BernhardGlueck | Posted in Uncategorized | Posted on 18-01-2011

0

As I wrote some code for a declarative mapping system of members to certain data sources today, and I wanted to keep things DRY, I needed to have both read and write access to a property
which was only given to me by a simple selector style C# Lambda Expression. Of course one could generate the required contrary method based on Reflection.Emit ( IL generation ) or some
other way ( CSharpCodeDom etc ) but .NET Expression Trees to the rescue, my job was much easier than I thought.

Here is my implementation ( which could use a little better error checking I know ).


  public static Action<TObject, TProperty> GetterToSetter<TObject,TProperty>(Expression<Func<TObject, TProperty>> getter)
        {
            Contract.Requires<ArgumentNullException>(getter != null, "getter");

            var memberAccess = getter.Body as MemberExpression;

            if (memberAccess != null)
            {
                var memberName = memberAccess.Member.Name;

                var property = typeof (TObject).GetProperty(memberName);

                if (property != null && !property.CanWrite)
                {
                    throw new ArgumentException("Property it not writeable");
                }

                var member = property ?? (MemberInfo) typeof (TObject).GetField(memberName);

                if (member != null)
                {
                    var parameter1          = Expression.Parameter(typeof (TObject), "obj");
                    var parameter2          = Expression.Parameter(typeof (TProperty), "value");
                    var memberAccessClone   = Expression.MakeMemberAccess(parameter1, member);

                    var body    = Expression.Assign(memberAccessClone,parameter2);
                    var setter  = Expression.Lambda(typeof (Action<TObject, TProperty>), body,new[] {parameter1, parameter2});

                    return (Action<TObject, TProperty>) setter.Compile();
                }
            }

            throw new ArgumentException("Invalid getter expression, only simple property/field accesses are supported");
        }

Given an expression that selects a property like “s => s.Id” this will give you an action that accepts both the owning object as well as a new value for the property and assigns it.
Also since Expression trees use DynamicMethods for code generation the new function will be defined in the owning objects type scope, so even if the setter of the property is not reachable publicly
the returned function will circumvent that members visibility and still work.

The end result is a pair of functions that allow read/write access to a property or field of an object instance.

Here is a usage sample:


        public class Simple
        {
            public int Id { get; set; }
        }

        public static void Sample()
        {
            var setter = GetterToSetter<Simple, int>( s => s.Id );
            setter( new Simple(), 10 );
        }

My given implementation is not very useful on it’s own, but can be helpful in code that already has the owning objects type as a generic parameter.

  • Share/Bookmark

Thrift for Windows

Posted by BernhardGlueck | Posted in Uncategorized | Posted on 01-04-2010

1

I recently wanted to play around with Thrift, the serialization/communication protocol/ddl that was open sourced by Facebook which is quite similar to Google
protocol buffers.

While Thrift itself supports windows just fine, and there is even a runtime library for C#, the compiler is quite hard to build for Windows, and binaries are not provided by the project itself. Since many people seem to have the same problems, I decided to build it myself and provide the binaries for everybody to use.

The binaries where built from the latest svn head, using cygwin, dependent cygwin libraries are included.

Download

  • Share/Bookmark

MacOs Security

Posted by BernhardGlueck | Posted in Uncategorized | Posted on 30-03-2010

0

Just found at fefe’s blog: http://support.apple.com/kb/HT4077

Security holes galore, and some really strange ones at that, I mean arbitrary code execution through spell checking ? Come on..
I think we can conclude that MacOs is not more secure than Windows, just not the main center of attack.

  • Share/Bookmark

Shameless Mercurial Plugg

Posted by BernhardGlueck | Posted in Uncategorized | Posted on 29-03-2010

0

Take a look at this video, it reiterates a lot of the points very eloquently that I try to make often when talking to people who need to decide on their next source control system.

http://tekpub.com/codeplex

  • Share/Bookmark

Silverlight : Flash = 1: 0

Posted by BernhardGlueck | Posted in Uncategorized | Posted on 29-03-2010

3

Maybe a bit of a strong title, but I have made no secret of my opinion that I think IF there is a need for a “Rich Internet Application” technology in your project and you have to use something more than HTML5/JavaScript then Silverlight is the best choice. ( That is if you plan to write some actual code with it, not just embed a video )

My main reasoning is that Flash in my opinion is still a graphics designer tool that has some half baked version of ECMAScript tacked onto it ( AS3 ) while Silverlight is a developer tool that has additional rich support for design/graphics work.

One big thing I recently found out ( I have to admit my AS3 experience is limited to helping out others debug some nasty problems with it, otherwise I don’t touch it with a 10 foot pole ) is that the Flash Player 10 garbage collector is like a government employee ….. lazy to the max.

Normally a garbage collector keeps a list of “root” objects and maintains a graph of dependent objects for them, in order to figure out what memory can be reclaimed when it is not accessible to user code anymore. Turns out when the Flash garbage collector sees a particularly large subgraph of objects that would take some time to actually analyze … it just says “oh well that’s a lot of work, I am not in the mood, I’ll just say it’s still used nobody will know …”. That’s right the Flash garbage collector stops analyzing larger graphs and just keeps them around, giving you huge memory leaks for free !

Now that’s a feature.

Original Post

  • Share/Bookmark

Interesting benchmark results

Posted by BernhardGlueck | Posted in Uncategorized | Posted on 20-03-2010

0

I recently had some discussions about .NET performance, JIT generated code performance in general, and the applicability of managed code platforms to “number crunching” domains, like scientific computing or game development.

After some research I found that there are nearly no current benchmark results posted anywhere on up to date versions of different runtimes/platforms. So I decided to do my own investigation.

For the pretty narrow domain of “number crunching” I choose the well known SciMark2 in its vanilla Java and Ansi C version, as well as it’s C# port that lives in the pnetmark project ( original version from Cornell university ).

Please keep in mind that this is no general benchmark that should be used in “my runtime is faster than yours” contests, but a narrow view on some performance characteristics for the above mentioned problem domains.

 

The test machine was:

Dual CPU i7 920 (8 physical, 16 virtual cores), 16 GB 1600 MHZ DDR3 Ram, Windows 7 Ultimate 64 bit.

( Note that scimark only uses one thread at the moment so it’s a single core benchmark )

The test contenders:

  • NET 4.0 RC1
  • Visual C 2010
  • Java 1.6.18
  • Mono 2.6.1 ( default JIT )
  • Mono 2.6.1 ( LLVM as JIT )

The binaries for each platform were built using maximum optimizations ( Link Time Code Generation, SSE2 and Maximize Speed for Visual C ) and subsequently run 10 times each, and the results averaged.

Here are the results ( sorted from best to worst )

 

Runtime MFLOPS
Visual C 2010 925
Mono 2.6.1 (LLVM) 602
Java 1.6.18 527
NET 4 RC1 525
Mono 2.6.1 420

 

Conclusion:

Basically Java and .NET offer the same performance, Mono with LLVM seems to be the fastest managed environment and AOT compilers still beat JIT compilers by a big margin. Roughly switching from Native code to Managed code will cost you 45 % performance. If that is worth it for you depends on your needs of course.

Again please note that normal application workloads where a lot of other stuff like allocations, branching etc occur will show less of a performance difference since the pure number crunching is offset by other overhead.

In the future i would like to test some other JIT based environments, maybe even some bytecode interpreters, and also try out Intel C++ as well as give Mono’s full AOT mode with maximum optimizations a try. Java also requires an upgrade to 1.7 prelease as soon as a real “beta” arrives.

  • Share/Bookmark