Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #750

Re: `It would be better like this but we can't afford this due to CPU time reasons': dynamic and static links in Java

From Joshua Cranmer <Pidgeot18@verizon.invalid>
Newsgroups comp.lang.java.help
Subject Re: `It would be better like this but we can't afford this due to CPU time reasons': dynamic and static links in Java
Date 2011-06-06 12:50 -0700
Organization A noiseless patient Spider
Message-ID <isjb2g$m2c$1@dont-email.me> (permalink)
References <877h91fm80.fsf@merciadriluca-station.MERCIADRILUCA>

Show all headers | View raw


On 6/4/2011 12:08 PM, Merciadri Luca wrote:
> AFAIK, this is due to the fact that, in Java, methods are
> `dynamically' linked, when variables are `statically' linked.

The proper terminology is that method invocation under normal 
circumstances is virtual.

> I heard
> that methods are dynamically linked because using a dynamic link takes
> a little bit more time (there are ~10 more CPUs ops to do). As a
> result, as methods' running time is generally bigger than a `variable
> use' one can understand the time needed for the dynamic link as
> globally insubstantial as compared to the total running time of the
> method. I also heard that it would be `better' if variables followed
> dynamic linking too.

Step back for a moment. In a native language compiled directly to 
machine code, a virtual method call typically involves reaching into a 
vtable and pulling a function pointer out and invoking it. The overhead 
will be a few clock cycles if the method would not be inlined (the 
likely inability to predict the branch will be the biggest hit, since 
the vtable is already likely to be in the data cache); if the method 
would otherwise be inlined, it might take a dozen or so clock cycles 
more to handle the branch.

Java isn't a native language, though, so the runtime has the ability to 
sit back and compile things based on what it sees. The JIT is smart 
enough to know that, though the call is in theory a virtual call, it is 
in practice a nonvirtual call, which allows the method call the 
opportunity to be inlined. In other words, it is possible to optimize 
out the virtual method dispatch in many cases where the virtual dispatch 
isn't necessary.

> However, would it really be `better?' I think the aforementioned fact
> (see the text before AFAIK) is often used in Java programming, isn't
> it? Could one provide more details on this choice, and on its implications?

There are issues with dynamic dispatch for variables from a programmer's 
perspective, since you're letting the variable be controlled by other 
people.

-- 
Beware of bugs in the above code; I have only proved it correct, not 
tried it. -- Donald E. Knuth

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

`It would be better like this but we can't afford this due to CPU time reasons': dynamic and static links in Java Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-06-04 21:08 +0200
  Re: `It would be better like this but we can't afford this due to CPU time reasons': dynamic and static links in Java Roedy Green <see_website@mindprod.com.invalid> - 2011-06-06 11:08 -0700
  Re: `It would be better like this but we can't afford this due to CPU time reasons': dynamic and static links in Java Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-06-06 12:50 -0700
    Re: `It would be better like this but we can't afford this due to CPU time reasons': dynamic and static links in Java Roedy Green <see_website@mindprod.com.invalid> - 2011-06-21 11:53 -0700

csiph-web