Interface method inlining
hi
I'm not sure, if this is the right forum to post to. Please move this thread to the correct one, if it is not.
Here is my question:
I want to write an "OpenGL wrapper-wrapper", that allows an abstract access to JOGL, LWJGL, etc. (for now only the two). This is only possible through an interface. In an OpenGL application there are many, many small calls to many OpenGL commands. So, if these methods are not inlined, the whole wrapper-wrapper doesn't make much sense.
I could not find any clear documentation about it. Are such methods inlined by the JIT compiler? I'm using Java 1.5 and 1.6. I know, this question cannot be answered generally. But all these methods will look similar to these ones:
Interface:
publicvoid glDoSomething(int foo, FloatBuffer bar);
publicvoid glDoSomethingElse(int foo);
publicvoid glEnable(int state);
publicvoid glDisable(int state);
(e.g.) JOGL implementaion
publicvoid glDoSomething(int foo, FloatBuffer bar)
{
// invoke the JOGL method in this implementation
gl.glDoSomething( foo, bar );
}
publicvoid glDoSomethingElse(int foo)
{
// invoke the JOGL method in this implementation
gl.glDoSomethingElse( foo );
}
publicvoid glEnable(int state)
{
// invoke the JOGL method in this implementation
if (!gl_states[ state ] )
{
gl.glEnable( gl_const_map[ state ] );
gl_states[ state ] =true;
}
}
publicvoid glDisable(int state)
{
if (gl_states[ state ] )
{
// invoke the JOGL method in this implementation
gl.glDisable( gl_const_map[ state ] );
gl_states[ state ] =false;
}
}
Would this be inlined by Java 1.5 and/or Java 1.6? Is there any good documentation about this problematic? Is there any way to ask the VM about it?
Any help is very appreciated.
Marvin
hmm... I wonder, how this board works. Is there any chance to get an answer before getting flooded down to page 10 in one day? Did I do anything wrong? Is there any further information needed?Marvin
> Is there any chance to get an answer before getting flooded down
> to page 10 in one day?
Usually interesting or annoying questions get replies.
> Is there any further information needed?
Well, I'm not quite sure I understood what you are asking for.
Is it possible to create an extra layer of abstraction on top of JOGL/LWJGL? I don't see why not, although it might get tricky in some places.
If you're asking whether there will be a performance hit from the abstraction layer, I would say that it is very likely that the hotspot compiler will compile away the abstraction layers.
But it's hard to answer with anything else than an educated guess.
> Usually interesting or annoying questions get replies.
Well, I thought, this was a support forum. So I assumed, that people might be here to help or to get help. But maybe I'm wrong.
> If you're asking whether there will be a performance hit from the abstraction
> layer, I would say that it is very likely that the hotspot compiler will compile
> away the abstraction layers.
>
> But it's hard to answer with anything else than an educated guess.
Well, would be cool, if there was any docu for it. Do you (or anyone else) know of something like that?
Marvin
Well, the JIT compiler only inlines actual code. So you can't tell from an interface definition whether it will decide to inline an implementation of a method or not.
> > Usually interesting or annoying questions get
> replies.
>
> Well, I thought, this was a support forum. So I
> assumed, that people might be here to help or to get
> help. But maybe I'm wrong.
>
No, you're right. Why would the fact that interesting or annoying questions get replies lead you to think otherwise? The volunteers here answer whatever questions they feel like answering. If nobody finds yours interesting enough or konws enough about it to answer it, it may go untouched. Or maybe the handful of people who would be interested/qualified just didn't see it before others' questions came in and pushed it down the list. If you don't get an answer after several hours or a day, a polite *bump* just to push it back to the forefront is fine. Just don't get too impatient or people will start getting irritated and be more inclined to flame your for your bad manners than to help you.
jverda at 2007-7-11 23:43:04 >

> I want to write an "OpenGL wrapper-wrapper", that> allows an abstract access to JOGL, LWJGL, etc. (for> now only the two). I could be wrong but I believe this already exists.
> Well, the JIT compiler only inlines actual code. So you can't tell from an
> interface definition whether it will decide to inline an implementation of a
> method or not.
Well, that's exactly, what my initial question was aiming at. I do know, that the implementing method is simple enough to be inlined. I just wanted to know, if the JIT compiler is even able to inline such code, that just implementes an interface. Is there any clear documentation about it?
####
No, you're right. Why would the fact that interesting or annoying questions get replies lead you to think otherwise? The volunteers here answer whatever questions they feel like answering. If nobody finds yours interesting enough or konws enough about it to answer it, it may go untouched. Or maybe the handful of people who would be interested/qualified just didn't see it before others' questions came in and pushed it down the list. If you don't get an answer after several hours or a day, a polite *bump* just to push it back to the forefront is fine. Just don't get too impatient or people will start getting irritated and be more inclined to flame your for your bad manners than to help you.
###
I'm very sorry, if I said anthing offending. Sometimes I do, even if I didn't want to. I was just wondering, if this forum actually makes sense in this way, if too few people are here, who answer questions. Believe me, when I say, that I do know, that it is (or can be) a hard job to keep track of all requests in a forum or even in a complete board. I will always be greatful for anybody, who takes up this work.
But I am convinced, that my question is an interesting one not only for me. So I was wondering, how such a forum could ever fullfill its role, if quesions like this get flooded down to page 10 in such a short time. Many questions will stay unanswered, if they would need to be recognized in minutes to not get flooded on pages, where noone will ever look again for them.
And further belive me, that this is not to be offending in any way. I just wanted to explain, why I was so shocked, that I wrote something like the above. Sorry, if this was misunderstandable.
> I could be wrong but I believe this already exists.
I would be highly interested, if you could point me to where it already exists.
Thanks.
Marvin
> I just wanted to know,
> if the JIT compiler is even able to inline such code,
> that just implementes an interface.
I don't see why not. Even if there was documentation on how the JIT worked, I can hardly see it containing statements such as "This works even if the method implements a method declared in an interface".
> I'm very sorry, if I said anthing offending.
> Sometimes I do, even if I didn't want to. I was just
> wondering, if this forum actually makes sense in this
> way, if too few people are here, who answer
> questions. Believe me, when I say, that I do know,
> that it is (or can be) a hard job to keep track of
> all requests in a forum or even in a complete board.
> I will always be greatful for anybody, who takes up
> this work.
>
> But I am convinced, that my question is an
> interesting one not only for me. So I was wondering,
> how such a forum could ever fullfill its role, if
> quesions like this get flooded down to page 10 in
> such a short time. Many questions will stay
> unanswered, if they would need to be recognized in
> minutes to not get flooded on pages, where noone will
> ever look again for them.
>
> And further belive me, that this is not to be
> offending in any way. I just wanted to explain, why I
> was so shocked, that I wrote something like the
> above. Sorry, if this was misunderstandable.
No, you weren't being offensive. I was just explaining how questions can sometimes go unanswered long enough to get pushed down, and what you can do about it. No worries.
jverda at 2007-7-11 23:43:04 >

it's bound to optimize away those layers. using interfaces in that way - abstract factories and the like - is such a common pattern that the developers of the compiler - who had time and budget constraints - were sure to write some inlining code for it
> I could not find any clear documentation about it.
> Are such methods inlined by the JIT compiler? I'm
> using Java 1.5 and 1.6.
The JIT is a very intelligent beast. First of all, it will inline methods that contain below a certain number of bytecodes, I can't exactly remember how many, but I think it's around 20 or so. Secondly, if your JIT find out that a method from your interface has multiple implementations, but is always called on the same implementation, it will completely remove the interface, and call the implementation directly. Of couse you need to let some thime for the JVM/JIT to warm up before that happense, but it will do it.
This means that in the end, the small methods you described here will be called directly, and their code will most likely be inlined. Basically, your wrappers won't incur any performance penalty.