Path: csiph.com!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: "javax.swing.JSnarker" Newsgroups: comp.lang.java.programmer Subject: Re: "Small" Program Challenge. Date: Mon, 25 Jun 2012 16:49:11 -0400 Organization: media lab? Lines: 74 Message-ID: References: <4fde76ce$0$287$14726298@news.sunsite.dk> <7kIDr.12088$Bp1.3039@newsfe10.iad> <8ac53825-c606-4839-8758-43c99555af85@googlegroups.com> NNTP-Posting-Host: UjC0ZGa12RsbW+ytKGitNQ.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: NewsTap/3.5.5 (iPhone/iPod Touch) X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.java.programmer:15587 On 25/06/2012 3:59 PM, Lew wrote: > javax.swing.JSnarker wrote: >> Daniel Pitts wrote: >>> How I would expect this to work in reality. >>> >>> 1. Load class >>> 2. get a reference to the static method "void main(String[])" >>> 3. Attempt to execute that reference >>> 3.1 Causes class initialization before execution. >>> 3.2 actual execution occurs. >> >> That has a problem, though, in that class initialization will happen on >> every method call, resulting in multiple initializations, if it's part > > That's not what happens. I'm not finished. Class initialization will happen on every method call, resulting in multiple initializations, *if it's part* of "attempt to execute the reference" rather than (as the spec says) something the JVM does immediately *before* the first such attempt (or other action that requires an initialized class for the action to begin). > As the spec says, it happens upon the first attempt to execute a static method > (if the class has not already been initialized). No, the spec does not say "upon" it says "immediately before". >> I suppose you could change 3.1 to "see if the class is initialized, and >> if not, initialize it", but even that would add to *every method call* > > That is what the spec says to do. As previously linked. > >> the overhead of a test-and-branch, and would still be dodgy at best on >> spec-adherence grounds. > > No, it does what it does and adheres to the spec. No, what the spec says to do is to implement a statically-compiled call this way: Class is loaded and initialized by statically-compiled code. Method invocation is simply a bare invokestatic instruction And a reflective/otherwise non-static call this way: Check if class is loaded and if not load and verify it. Check if class is initialized and if not initialize it. Check if method exists and if not throw an exception, otherwise invoke it. And this is apparently what earlier versions did. Surely you aren't suggesting there's a whole raft of if (class_is_loaded), if (class_is_initialized), etc. tests before every method call in Java 7? Because that would make method calls much slower than before, unless you've got some cleverness in place to remove those tests from the code once the class is loaded. In other words, something more like expanding each call into load_and_initialize_if_needed(X.class); invokestatic... where load_and_initialize_if_needed(X.class) strips out all instances of load_and_initialize_if_needed(X.class) from all loaded bytecode as part of its own behavior. But that would have all kinds of difficulties of its own. At least the JIT might be able to skip over it if X is already loaded and an instance is in code it's JITting. -- public final class JSnarker extends JComponent A JSnarker is an NNTP-aware component that asynchronously provides snarky output when the Ego.needsPuncturing() event is fired in cljp.