Asked  9 Months ago    Answers:  4   Viewed   6 times

Many modern frameworks (Spring, Hibernate) provide very nice dynamic behaviors with use of Java dynamic proxies, but what's the exact performance cost associated with it? Are there public benchmarks available for Sun JVM?

 Answers

3

A few pointers:

  • Debunking myths: proxies impact performance (have a look at the comments too)
  • Java theory and practice: Decorating with dynamic proxies
  • Benchmarking the cost of dynamic proxies
Saturday, September 4, 2021
 
Evan
 
1

EDIT: tl;dr - I've just tried creating the proxy in a different way, as described below, and it produces the output you were after. I just had to change this:

var c = new InterceptedClass();
var i = new Interceptor();

var cp = new ProxyGenerator().CreateClassProxyWithTarget(c, i);

To this:

var i = new Interceptor();
var cp = new ProxyGenerator().CreateClassProxy<InterceptedClass>(i);

As I understand it, the proxy generator is effectively creating a wrapper object. They're two separate objects - one is just a wrapper around the other, with interception etc in the wrapper layer.

It's hard to see how it could change what the instance of InterceptedClass did with its own method calls:

  • DynamicProxy can't change the type of an existing object; once an object is created, its type is fixed
  • DynamicProxy can't change how existing calls to an existing object are bound

If you want Method1 to call Method2 via the wrapper using the current proxy creation code, you'll need to tell the existing object about the wrapper, either as a field within it or as a method parameter.

Alternatively, there may be a different way of creating the proxy to start with - one where the proxy is in some sense the target object. I suspect you may want to look at CreateClassProxy rather than CreateClassProxyWithTarget - I suspect it's the fact that you're supplying the target object which is causing you problems.

Whether the behaviour you're seeing is "expected" or not obviously depends on your expectations - but it's certainly what I would expect, without knowing anything about Castle Dynamic Proxy :)

Monday, August 2, 2021
5

That would depend a lot on the exact scenario - but there is a layer of caching built in, so it is not as terrible as you might expect (it doesn't do reflection every time). It can also vary on the operations (for example, "lifted" nullable-T operations are noticeably slower). You would need to measure, but as it happens I have some timings here for member (property) access, that I took when doing FastMember:

Static C#: 14ms
Dynamic C#: 268ms
PropertyInfo: 8879ms (aka reflection)
PropertyDescriptor: 12847ms (aka data-binding)
TypeAccessor.Create: 73ms (aka FastMember)
ObjectAccessor.Create: 92ms (aka FastMember)

CAVEAT: these are for a single test that may not be representative of your scenario. This code is shown here

So: based on a simple test, about 20-times slower than static regular C#, but about 30 times faster than reflection.

UPDATE: interesting, looks like reflection got faster in .NET 4.5:

Static C#: 13ms
Dynamic C#: 249ms
PropertyInfo: 2991ms
PropertyDescriptor: 6761ms
TypeAccessor.Create: 77ms
ObjectAccessor.Create: 94ms

Here it is only about 12 times faster than reflection, because reflection got faster (not because dynamic got slower).

Tuesday, September 14, 2021
3

The way to intercept methods using dynamic proxies are by:

public class FooHandler implements InvocationHandler {
    private Object realObject;

    public FooHandler (Object real) {
        realObject=real;
    }


    public Object invoke(Object target, Method method, Object[] arguments) throws Throwable {
        if ("execute".equals(method.getName()) {
            // intercept method named "execute"
        }

        // invoke the original methods
        return method.invoke(realObject, arguments);
    }
}

Invoke the proxy by:

Foo foo = (Foo) Proxy.newProxyInstance(
            Foo.class.getClassLoader(),
            new Class[] {Foo.class}, 
            new FooHandler(new FooImpl()));
Thursday, September 30, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :