What surprises me is that accessing by property is a lot slower than setter. I 
suppose I figured once Groovy fully initialized the MOP and call site that 
t.member = 1 would perform essentially exactly the same as t.setMember(1).

However, this test might have the normal micro-benchmark faults, since the 
side-effects are not used, we can’t really predict what the JIT might do (such 
as eliminating some instructions entirely), or triggering a method optimization 
on the script’s run method after the first for loop but before the second (I 
don’t remember precisely how JIT works when you are not re-entering the method 
continuously, but I don’t think every instruction is a possible point where it 
can jump to the JIT-ed version). I wonder if using a tool like JMH would change 
the results.

Jason

From: Cédric Champeau [mailto:[email protected]]
Sent: Thursday, January 21, 2016 10:52 AM
To: [email protected]
Subject: Re: A @CompileStatic for class members

Your benchmark is wrong, because the caller is not statically compiled. What 
you have statically compiled is the `Test` class, but the caller is not. So 
when you do `t.setMember(..)`, the call is dynamic. It's the caller that you 
have to statically compile (or both, ideally).

2016-01-21 16:39 GMT+01:00 Marshall, Simon 
<[email protected]<mailto:[email protected]>>:
Hi all, I’d like class field access in specific classes to be without groovy 
magic, so that it is as fast as possible.  I guess this means disabling runtime 
metaprogramming for specific classes (or fields).

At first, the 
http://docs.groovy-lang.org/latest/html/api/groovy/transform/CompileStatic.html 
annotation looked like it might do the trick, as it mentions class properties 
being covered.  It certainly works for class methods, but in my testing it 
didn’t make any difference to class members.  Setting the field “directly” (ie, 
via t.member below) is still an order of magnitude slower than using its 
implicit setter directly (ie, t.setMember()).  Similarly for field access vs 
getter.  That annotation does not seem to cover field access or assignment.

So, is there a way to make field access as quick as using the implicit 
getter/setter?  Thanks, Simon.

For example, on my 3+GHz Xeon with jdk1.8.0_66/groovy-all-2.4.3, the below code 
gives me:

t.member = val          34.577
t.setMember(val)        3.529
t.with { member = val } 127.922

package test

import groovy.transform.CompileStatic

@CompileStatic
class Test {
    String member
}

String val = 'test'
Test t = new Test()

for (long i = 0; i < 100 * 1000 * 1000; ++i) {
    t.setMember(val)
}

long time = 1000 * 1000 * 1000
long beg = System.currentTimeMillis()
for (long i = 0; i < time; ++i) {
    t.member = val
}
println('t.member = val\t\t'+(System.currentTimeMillis() - beg) / 1000.0)

beg = System.currentTimeMillis()
for (long i = 0; i < time; ++i) {
    t.setMember(val)
}
println('t.setMember(val)\t'+(System.currentTimeMillis() - beg) / 1000.0)

beg = System.currentTimeMillis()
for (long i = 0; i < time; ++i) {
    t.with { member = val }
}
println('t.with { member = val }\t'+(System.currentTimeMillis() - beg) / 1000.0)

"Misys" is the trade name of the Misys group of companies. This email and any 
attachments have been scanned for known viruses using multiple scanners. This 
email message is intended for the named recipient only. It may be privileged 
and/or confidential. If you are not the named recipient of this email please 
notify us immediately and do not copy it or use it for any purpose, nor 
disclose its contents to any other person. This email does not constitute the 
commencement of legal relations between you and Misys. Please refer to the 
executed contract between you and the relevant member of the Misys group for 
the identity of the contracting party with which you are dealing.


----------------------------------------------------------------------
This email message and any attachments are for the sole use of the intended 
recipient(s). Any unauthorized review, use, disclosure or distribution is 
prohibited. If you are not the intended recipient, please contact the sender by 
reply email and destroy all copies of the original message and any attachments.

Reply via email to