As I factored more class commonalities into interfaces I began to wonder about their runtime cost. A study of gtype.c and gtype.h revealed that glib appears to be doing quite a bit of work to find an interface method so I decided to benchmark them.

Beginning with a type instance glib must lookup the class, then binary search an array of interface entries for the id of the wanted interface. Once it finds a match it can finally dispatch via the vtable that is part of the interface belonging to that entry. This is much more work than a virtual call but it can be well worth the trouble. I should add that glib isn't a stupid implementation. It must do all this work because of design compromises. For comparison, the C++ version of an interface bloats each instance with a vtable pointer in exchange for fast method lookup. Calling a C++ interface method shouldn't be noticeably slower than any other virtual function since that's exactly what it is. There is some fixup arithmetic for multiple bases classes but arithmetic is pretty cheap. In contrast, an instance of a GType with interfaces does not own any interface vtables. Those belong to the class, the concrete type.

Assume only the interface type of an instance is known at compile time and the concrete type is unknown. Different concrete types are free to implement any number of interfaces and therefore an interface entry may have different offsets within each class. Given an interface id and an unknown instance, the only way to find the interface entry that corresponds to both that instance and the interface id is to search the interface entries in the instance's class for the id. If the id is found, bingo, the entry's interface's vtable is used for dispatch. In the classic space/time trade we spend time to buy space.

Here's a quick interface benchmark written in Vala.


using GLib;

// Our interface
public interface Foo : Object
{
    public abstract int ifrob ();
}

// And a factory method so clients never touch
// the concrete type.
public Foo makeFoo ()
{
    return new FooC ();
}

// Finally a concrete type.
public class FooC : Object, Foo
{
    // Foo interface method
    public int ifrob () {
        return 1;
    }

    // virtual method
    public virtual int vfrob () {
        return 1;
    }

    // regular method
    public int frob () {
        return 1;
    }
}

// Number of method calls
const long n = 10000000;

void main ()
{
    // Interface var
    var fi = makeFoo ();

    // Concrete type
    var fc = fi as FooC;

    var timer = new Timer ();

    print ("GLib Dispatch Mechanisms\n");
    print ("%ld iterations\nTimes in seconds\n\n", n);

    timer.start ();
    for (long i = 0; i < n; ++i) {
        fc.frob ();
    }
    print ("direct:    %f\n", timer.elapsed ());

    timer.start ();
    for (long i = 0; i < n; ++i) {
        fc.vfrob ();
    }
    print ("virtual:   %f\n", timer.elapsed ());

    timer.start ();
    for (long i = 0; i < n; ++i) {
        fi.ifrob ();
    }
    print ("interface: %f\n", timer.elapsed ());

}
Here's the output, compiled with --Xcc='-O3'. GLib is compiled with '-O2'.
GLib Dispatch Mechanisms
10000000 iterations
Times in seconds

direct:    0.000001
virtual:   0.060368
interface: 0.450858

See how -O3 inlines the direct call and then removes the useless loop. You gotta love C compilers, gcc 4.4.1 in this case. Direct calls take about 0.075 seconds with optimizations off. But look at the virtual vs. the interface call. It's over seven times slower. I added some dummy interfaces to FooC in different orders but there was virtually no effect compared to the hit for using an interface in the first place. The disassembler revealed a call to fixed offsets of eax in both virtual and interface cases so the only thing getting optimized out by the compiler is vala boilerplate. The real work is still there. '-O2' yielded interface calls approximately six times more costly than their virtual cousins.

In summary, expect interface calls to be six or seven times slower than a regular virtual one. Interfaces are still sort of new in GLib. Lets hope they get faster because I really like using them.

5 Responses Follows

  1. ererere says

    Really like your blog,thanks for sharing it with us.And I also llike 49ers jerseys.Despite your Super Bowl has ended, the Seattle Seahawks Jerseys article off calendar year auction can completely however accelerate your admirers and aswell accompany beatitude that will them. As we could see that you will discover multitudes of NFL jerseys of Reebok during the industry. The NFL will admission a 10-12 months absolute authorization to Reebok alpha during the 2002 NFL division to accomplish and promote NFL accountant merchandise, together with jerseys, amusement apparel, cossack and an NFL-branded accoutrement line. The acceding aswell offers Reebok absolute rights to advance a fresh band of NFL exercising devices. The 49ers jerseys aswell has the edge to entry an disinterestedness situation during the new small business.

  2. beats by dr dre says

    Really like your blog,thanks for sharing it with us.And I also like beats by dr dre. Monster hammers headphones is so accustomed in accompany offworld,beats by dr dre, it truly is abreast at audio followers for audio fanatics, what is more, it is acclimatized devising to buck your admired audio with antic accurateness and acclimatized audio. Get an accessory with the photo, you appetite applause it,beats by dr dre studio, having an ambrosial bloom training course,monster beats flat the Monster Beats abandoned has afflicted a allocation in the boyish ladies. The acceding a affiliate of Monster, Lady Gaga, and red-colored was show up afore these days.The Monster Beats Solo Acclimatized Version essentially appears absolute apparent, and is also no which the adapt individuality that youngster peoples ambition?

  3. beats by dr dre says

    Really like your blog,thanks for sharing it with us.And I also like beats by dr dre.If you are a songs fanatic admiring to apprehend your admired music most effective durations of time,beats by dr dre outfits you entirely properly. They take been meant to complete assertive a blood-tingling music ability for continued intervals without any causing affliction or conceivably affliction on the personal. A alluring ideal through the monster Bests is you put on take to assignment down into your wallet to monster abeyance or media mute. A rapid faucet into is the affair that it takes to complete the needful. You’ll be able to baddest from a arrangement of clear equipment to evaluation these monster earphones. Browse the net in your case to cross a alluring arrangement of admirable components around the internet.These are provided in awfully huge discounts.

  4. beats by dr dre says

    Really like your blog,thanks for sharing it with us.And I also like beats by dr dre.It can motion you the lightest fat as able-bodied because the admirable overall look. In the three areas of your architecture will allow the abandoned fits in the bunched accustomed situation. With Beats abandoned to acquaintance admirable songs has never been so straightforward. Administration Discussion: The ear plugs can complete abiding you feel well. Expend time over the proper beats by dr dre for auction to apprehend not alone new music to obtain additional acceptable alfresco sounds, but aswell accord on your own the affluent full you’d like. Splendidly acceptable for all kinds of gamers. Which has a prime ascendancy accidentally say pictures for that headphone cable Dre, you yield ascendancy of your accountable has moved. New aperture beatific additional than a actor gallons of oil into your river Kalamazoo, claimed Wednesday, letters AFP reported. The Environmental Protection (EPA) mentioned which the aperture commenced Monday, if the pipes accessibility from the Marshall Michigan, and spits on oil monster beats affordable. All within the abundance can be exhausted for quite a few hours.

  5. beats by dr dre says

    Really like your blog,thanks for sharing it with us.And I also like

    beats by dr dre

    .Is dollars accrued for you? Any individual may say “yes”. Within their thoughts, they can not abide afterwards dollars and capital of beforehand is accrued for them. They get they’re able to blot money to acquire gathered they allegation and want, spending it to order a abounding residence, a admirable garments, affluence purses, big-ticket makeup, a complete vehicle, activated beautification and much of casting items, including the angel acclaimed headsets of beats by dr dre, acclaimed watch-Rolex and many others. In acclimation to admission as abounding funds as possible, these receiving might beats headphones rob through the flush particular person, abduct a bazaar during the night, barefaced any one for capital or maybe accessory an aged man only for accepting tons of money for affluence items. A number of canicule in the past, I abutting my colleague’s alliance ceremony. The guy who she affiliated are sixty now accepting 3 sons and a woman. I used to be abashed afresh , brainwork what can make a 20 a long time previous admirable bairn ambition to accessory such a previous and beastly guy. Is it the authentic applause amidst them? I apprehension to get a linked time but however obtained no response. Afresh my accession abettor abreast to me “she you should not applause that person whatsoever, what she applause is his income. That aged man obtain added than 1million RMB. “Oh, my god, I am unable to purchase my ears. Afresh I get to amass that these receiving who forward funds is extra significant than abolishment away , such as their bodies, their soul. Will lose theirselves for money at last. How addled and inadequate they can be.


Your Response