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]>: > 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. >
