tag:blogger.com,1999:blog-79101262024-03-07T15:52:20.892+07:00CK's BlogAbout anything interests me ...chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.comBlogger110125tag:blogger.com,1999:blog-7910126.post-52693710822822335852011-12-24T21:41:00.001+07:002011-12-24T21:41:21.178+07:00Learning to know Featherweight Java - An Easy Way<p>It's very long time since last my last post and I am still learning how to properly prove type soundness of a programming language. Here's a list of <a href="http://www.cs.cornell.edu/Courses/cs4110/2010fa/schedule.php">lectures</a> from Cornell that I've been reading and they're quite good to understand.</p>chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com0tag:blogger.com,1999:blog-7910126.post-22956691193275305792010-02-09T20:45:00.001+07:002010-02-09T20:48:19.381+07:00Type system primerGiven an expression e, a type T and a dictionary TT<br />that maps variables to type, the notation<br /><br /><pre>TT |- e : T</pre><br /><br />roughly means<br /><br /><pre>T = typecheck(TT, e)</pre><br /><br />and the horizontal bar notation:<br /><br /><pre><br />P1 P2 P3<br />----------------<br /> Q<br /></pre><br />means<br /><br />Q if P1 and P2 and P3<br /><br />from: <a href="http://wiki.jvmlangsummit.com/pdf/28_Siek_gradual.pdf">http://wiki.jvmlangsummit.com/pdf/28_Siek_gradual.pdf</a> (slide 6)chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com0tag:blogger.com,1999:blog-7910126.post-54946686687334844932009-12-07T18:55:00.001+07:002009-12-07T18:57:50.263+07:00Groovy Performance Update - Long running programs are as fast as JavaIt's been almost one and a half year that I have not updated anything related to Groovy performance in general. As I posted previously that I have been still doing on this topic, now it's time for updating a bit.<br /><br />The technique is to hook the callsite caching process. Each callsite-to-be-called is profiling, its runtime information is analysed, and the faster code for it is then generated. The callsite is replaced by its equivalent direct call. Replacement is done, anyway, through JVMTI similar to the trick implemented in the previous version of GJIT. However, the concept used in this version of GJIT has been changed from <i>totally automatic</i> to <i>manually controlled</i>. This means that only selected callsites will be optimised, and you, as a developer, can control this optimisation via the mechanism provided. This concept is not that new, I have <a title="posted" href="http://chanwit.blogspot.com/2008/01/application-specific-performance-tuning.html" id="cfeh">posted</a> about it a while ago.<br /><br />Latest results I came up with are micro-benchmarks. All of them in the long run, after related call sites are optimised, are running at the same speed as Java programs.<br /><br />Let's examining the graph of the Fibonacci benchmark:<br /><div id="nafk" style="text-align: left;"><img src="http://docs.google.com/File?id=dhddrfh9_6297zqxt2dr_b" height="310" width="660" /></div><br />You can see that there are 2 interesting regions of graphs before the Groovy program getting the same speed as Java.<br />1. At the beginning, there is an overhead of profiling the callsite instantiation system of Groovy.<br />2. For background optimisation, an optimiser thread requires times for analysis and code generation. (I also make a note on the overhead of HotSpot VM, just for referencing).<br /><br />After the optimisation phase is completed, the speed of Groovy program goes overlapping with Java. This approach would be working fine for a long running program, but <i>not</i> for scripting. Why? I left this as an exercise for the readers ;-)chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com0tag:blogger.com,1999:blog-7910126.post-62009990151234843532009-11-14T15:52:00.001+07:002009-11-14T15:53:51.786+07:00Analysis of static Groovy claims<div xmlns='http://www.w3.org/1999/xhtml'>Continued from my <a href='http://chanwit.blogspot.com/2009/11/static-groovy-is-it-feasible.html'>last post</a>, I think I will be in the <a href='http://en.wikipedia.org/wiki/NPOV'>neutral point of view</a> enough to do the analysis for these <a href='http://groovy.dzone.com/articles/groovy-and-static-compilation'>claims</a>. All of this analysis are solely based on my opinion, so discussion is always welcome.<br/><blockquote><i>It is possible to write a statically typed compiler for Groovy keeping all goodies of the beautiful language.</i><br/></blockquote>This is partially true. I do not think I can create a static compiler to cover all Groovy features at once.<br/><blockquote><i>It is possible to implement very powerful type inference of local variable and closures, so we don't need to provide verbose type information more than necessary and/or useful for documentation purposes.</i><br/></blockquote>This is partially true. A language cannot fully support type inference. Type inference itself has its own limits, and it is a known P-complete or NP-complete problem depending on its implementation) [see <a href='http://portal.acm.org/citation.cfm?id=640132'>this</a>].<br/><blockquote><i>It is possibly to have compile-time metagrogramming (properies, categories, default groovy methods, mixins, ast transformations - all but runtime meta-programmings).</i><br/></blockquote>This is also true. But adding more phases to the compiler will make it slow. When the compilation time is slow + startup time of the JVM, you might not be happy using the language as a scripting tool. <a href='http://bracha.org/pluggable-types.pdf'>Pluggable type systems</a> would be possible to help this issue.<br/><blockquote><i>It is possible to support Scala-like traits (also knows as interfaces with default implementation), which is extremly strong meta-programming tool.</i><br/></blockquote>This is true. No doubt.<br/><blockquote><i>It is even possibly to compile piece of code in so called mixed mode (resolve and call statically what we can and call the rest dynamically). This mode is great for example for mixing computation with building markup or populating UI elements with results of computation.</i><br/></blockquote>This is true. There is also something called <a href='http://portal.acm.org/citation.cfm?id=113469'>Soft typing</a> and <a href='http://ece-www.colorado.edu/%7Esiek/gradual-obj.pdf'>Gradual typing</a> which could help one archieve both static and dynamic call in the same language. However, the way of building markup can be just a simpler trick. I have it in my last <a href='http://chanwit.blogspot.com/2009/11/static-groovy-is-it-feasible.html'>post</a>.<br/><blockquote><i>It is even possibly that because of type inference staticly compiled code can be a little bit less verbose compare to Groovy (almost no need in 'as' conversion for example).</i><br/></blockquote>This is probably false. In general, I do not think there will be a better way than a dynamic language to do so.<br/><br/><br/><br/><div class='zemanta-pixie'><img src='http://img.zemanta.com/pixy.gif?x-id=60718c76-2cf8-829c-94cc-b60d4cf90404' alt='' class='zemanta-pixie-img'/></div></div>chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com0tag:blogger.com,1999:blog-7910126.post-60187503835693670902009-11-12T01:07:00.001+07:002009-11-12T19:05:11.257+07:00Static Groovy, is it feasible?<div xmlns='http://www.w3.org/1999/xhtml'>Static Groovy seems to be a hot topic. There have been debates about it in the past. And recently, Alex's call for implementation has been <a href='http://groovy.dzone.com/articles/groovy-and-static-compilation'>posted</a> on Groovy's DZone. Yesterday, Jochen's got his view about it <a href='http://blackdragsview.blogspot.com/2009/11/static-groovy-about-calling-methods.html'>here</a>.<br/><br/>To me, I have been (and still) doing somethings to boost Groovy performance. Actually, it is based on the current call site implementation of Groovy 1.6. But that's another approach (As people said, performance is not only concern for static Groovy, early detection of errors is also what they want).<br/><br/>One of the biggest challenge of implementing static Groovy is that how could we implement a framework like <a href='http://grails.org'>Grails</a> using it. This is really an interesting question to me, as Grails requires a lot of dynamic features of Groovy. So, I try to summarise what static Groovy should be capable of for making a Grails-like framework happen.<br/><br/>Firstly, GORM dynamic finders:<br/>This kind of finders mainly uses missingMethod to perform their actions.<br/><br/>Considering this (posted as a <a href='http://blackdragsview.blogspot.com/2009/11/static-groovy-about-calling-methods.html?showComment=1257928097582#c4777230856299083855'>comment</a> in Jochen's blog):<br/><br/><font face='Courier New'>class C {<br/> def findBy/s/(Object ...) { .. }<br/>}</font><br/><br/>You may notice "s" in the method name. It is of type String and can be referenced in the method body.<br/>So this is basically, findBy* of Grails. Missing method could be modelled in the same way. For example,<br/><br/><font face='Courier New'>class C2 {<br/> def /s/(Object ...) { /* doing something with s */ }<br/>}</font><br/><br/>Secondly, Adding dynamic methods:<br/>This mainly uses in Grails plugins to extend capability of the framework.<br/>The idea of category would be working perfectly for it.<br/><br/><font face='Courier New'>main() {<br/> A.test()<br/>}<br/><br/>class A {<br/>}<br/><br/>class Plugin {<br/> static test(A self) { .. }<br/>}</font><br/><br/>Resolving the <b>test</b> method would be a bit problem as it's not only through the class hierarchy of A, but also other classes that contain methods that <i>crosscut</i> A. It would be a bit easier for a compiler to resolve them, if we have a marker like <font face='Courier New'>@extension static test(A self) { ... }</font> to help it.<br/><br/>Thirdly, interception for implementing invokeMethod:<br/>This may probably involves the concept of AOP. But I will note use its terms here. Actually, the example is came from a work in the AOP area.<br/><br/>When you are going to intercept call of existing methods, this could be working:<br/><br/><font face='Courier New'>class C {<br/> intercept *(X x) {<br/> proceed(x); // intercepting methodForX only<br/> }<br/><br/> intercept *(Y y) {<br/> proceed(y); // intercepting methodForY only<br/> }<br/><br/> intercept *(Object ...) { ... }<br/><br/> def methodForX(X x) { .. }<br/> <br/> def methodForY(Y y) { .. }<br/>}</font><br/><br/>This concept would be called local interception, where an interceptor is capable of only intercepting methods in the same class. System-wide interception would be doing the same. Anyway, it will be something like AspectJ, in eventually.<br/><br/>To sum up, there are really challenges to implement a static counterpart of Groovy. However as you might see, having enough features to implement a Grails-like framework using such language is not that easy. A lot of things need to be proper implemented. Anyway, this is really an interesting thing to do.<br/><br/><br/><div class='zemanta-pixie'><img src='http://img.zemanta.com/pixy.gif?x-id=5286c2c2-6ef5-8ffc-b512-9cee2f2806ae' alt='' class='zemanta-pixie-img'/></div></div>chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com1tag:blogger.com,1999:blog-7910126.post-29991150954045082562009-09-04T22:50:00.001+07:002009-09-04T22:50:55.902+07:00Using AsmNodeBuilder to Test Bytecodes with Pleasure<div xmlns='http://www.w3.org/1999/xhtml'>DSL of Groovy never made me bored. I am working on some bytecode transformation and having a number of tests to run.<br/>Testing low-level transformation means dealing with JVM instructions. For example,<br/><br/><font face='Courier New'>ILOAD 0<br/>INVOKESTATIC java/lang/Integer.valueOf(I)Ljava/lang/Integer;<br/>LDC 0<br/>INVOKESTATIC java/lang/Integer.valueOf(I)Ljava/lang/Integer;<br/>INVOKESTATIC org/codehaus/groovy/runtime/ScriptBytecodeAdapter<br/> .compareLessThan(Ljava/lang/Object;Ljava/lang/Object;)Z<br/>IFEQ L2</font><br/><br/>There is class MethodNode in the tree package of ASM. The class has the field instructions of class InsnList, which contains its method body. In the following, I have <font face='Courier New'>InsnList insn = methodNode.instructions;</font> to perform some optimisation over it.<br/><br/>Before making AsmNodeBuilder, I had to create<br/>a test data using Asm's Tree Nodes. Something looks like:<br/><br/><font face='Courier New'>insn.add(new VarInsnNode(ILOAD, 0))</font><br/><br/>With Groovy and AsmNodeBuilder, now I can write:<br/><br/><font face='Courier New'>insn.append {<br/> iload 0<br/>}</font><br/><br/>When comparing results, I had to do:<br/><font face='Courier New'><br/>assert insn.get(0).opcode == ILOAD<br/>assert insn.get(0).var == 0</font><br/><br/>Now, it becomes:<br/><br/><font face='Courier New'>assertEquals asm { iload 0 }, insn[0]</font><br/><br/>Of course, I can also do:<br/><br/><font face='Courier New'>assertEquals asm {<br/> iload 0<br/> invokestatic Integer,"valueOf",[int],Integer<br/>}, insn</font><br/><br/>to assert all instructions in the list.<br/><br/>Look fun? Convinced? There are two classes you may use:<br/><br/>- <a href='%20http://github.com/chanwit/groovy-aop/blob/c189dcbea716ba871fbb3f0e6ac25e9f9b5a9ff0/src/main/groovy/org/codehaus/groovy/gjit/asm/InsnListHelper.groovy'>InsnListHelper.groovy</a><br/>- <a href='http://github.com/chanwit/groovy-aop/blob/c189dcbea716ba871fbb3f0e6ac25e9f9b5a9ff0/src/main/java/org/codehaus/groovy/gjit/asm/AsmNodeBuilder.java'>AsmNodeBuilder.java</a> (generated from gen_asm_builder.groovy)<br/><br/>Here's how to<br/><ol><li>call InsnListHelper.install() in the very beginning of your Groovy test case.</li><li>Write a test case.</li><li>use <InsnList>.append { ... } to create a method body.</li><li>do your transformation.</li><li>assert the result against an asm { ... } block. Note that Groovy's power assertion won't work with the block for some reasons, use assertEquals instead.</li></ol>You can also look at an example <a href='http://github.com/chanwit/groovy-aop/blob/c189dcbea716ba871fbb3f0e6ac25e9f9b5a9ff0/src/test/groovy/org/codehaus/groovy/gjit/asm/transformer/AutoBoxEliminatorTxTests.groovy'>here</a>.<br/><br/><br/><div class='zemanta-pixie'><img src='http://img.zemanta.com/pixy.gif?x-id=ce8ef127-b8ee-8236-989b-c6fc1712e42e' alt='' class='zemanta-pixie-img'/></div></div>chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com1tag:blogger.com,1999:blog-7910126.post-35692006888702873022009-07-26T20:47:00.001+07:002009-07-26T20:47:47.072+07:00Improved Complex Number in Groovy<div xmlns='http://www.w3.org/1999/xhtml'>Thank you for last comment in <a href='http://chanwit.blogspot.com/2009/06/complex-number-in-groovy.html'>the previous blog</a>. Here's the improved version of Complex Number.<br/><font face='Courier New'><br/>Number.metaClass.plus = { n -><br/> if(n instanceof Imaginary) {<br/> new Complex(r:delegate, i:n.v)<br/> }<br/>}<br/>Number.metaClass.getI = { -><br/> new Imaginary(v: delegate)<br/>}<br/><br/>class Complex {<br/> def r<br/> def i<br/> String toString() {<br/> "$r + ${i}i"<br/> }<br/>}<br/>class Imaginary {<br/> def v<br/> def plus(n) {<br/> if(n instanceof Number) new Complex(r:n, i:this.v)<br/> }<br/>}<br/><br/>def b = 2 + 2.4.i<br/>def c = 4.2.i + 2<br/>println b<br/>println b.class<br/>println c<br/>println c.class</font><br/><br/><div class='zemanta-pixie'><img src='http://img.zemanta.com/pixy.gif?x-id=b80e4f4a-bb73-8b86-8857-44f4f2b0671d' alt='' class='zemanta-pixie-img'/></div></div>chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com0tag:blogger.com,1999:blog-7910126.post-51277839776688912852009-06-11T03:49:00.002+07:002009-06-11T04:41:20.541+07:00Complex Number in Groovy<div xmlns='http://www.w3.org/1999/xhtml'>Here's a quick note of making Groovy to support complex numbers:<br /><br />1. override number operations via metaclass:<br /><font face='Courier New'>Number.metaClass.plus = { n -><br /> if(n instanceof Imaginary) {<br /> return new Complex(r:delegate, i:n.v)<br /> }<br />}<br /></font><br />2. define class Complex to hold real, r, and imaginary, i, parts<br /><font face='Courier New'>class Complex {<br /> def r<br /> def i<br /> String toString() {<br /> "$r + ${i}i"<br /> }<br />}</font><br /><br />3. create class Imaginary to be a wrapper class (I borrow this idea from Scala's implicit converter).<br /><font face='Courier New'>class Imaginary {<br /> def v<br />}</font><br /><br />4. define i as a global closure. It would be in DefaultGroovyMethods, actually.<br /><font face='Courier New'>def i = { v -><br /> new Imaginary(v:v)<br />}</font><br /><br />Then when you write:<br /><font face='Courier New'>def b = 2 + i(2.4)<br />println b<br />println b.class</font><br /><br />This prints "2 + 2.4i" and the type of b is class Complex.<br /><br /></div>chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com2tag:blogger.com,1999:blog-7910126.post-8437624620928801532009-06-06T22:43:00.001+07:002009-06-06T22:43:53.527+07:00Fortress - Hello World<div xmlns='http://www.w3.org/1999/xhtml'>Here's my early experiment of running Fortress Hello world program, which is listed below (taken from the <a href='http://projectfortress.sun.com/Projects/Community/wiki/MITTutorialApril2009'>tutorial</a> at MIT by the Fortress team):<br/><br/><font face='Courier New'>component hello<br/>export Executable<br/><br/>run() = println("Hello, World !")<br/><br/>end</font><br/><br/>I'm a bit surprised that its compilation time is "really" slow. I may be guessing that it's caused by type inference in the Fortress compiler. JVM 1.6.0_12 was used in the experiment. One of my quick conclusion is that Fortress is not suitable for scripting. But I think its compiled program will not be slow (as I mentioned that I am suspecting its type inference engine that makes scripting slow).<br/><br/>Here's 5 times of running:<br/><br/><font face='Courier New'>chanwit@lb /cygdrive/c/fortress/fortress_3625<br/>$ time `bin/fortress chanwit/hello.fss`<br/>bash: Hello,: command not found<br/><br/>real 0m11.393s<br/>user 0m0.244s<br/>sys 0m0.199s<br/><br/>chanwit@lb /cygdrive/c/fortress/fortress_3625<br/>$ time `bin/fortress chanwit/hello.fss`<br/>bash: Hello,: command not found<br/><br/>real 0m11.486s<br/>user 0m0.181s<br/>sys 0m0.214s<br/><br/>chanwit@lb /cygdrive/c/fortress/fortress_3625<br/>$ time `bin/fortress chanwit/hello.fss`<br/>bash: Hello,: command not found<br/><br/>real 0m11.412s<br/>user 0m0.213s<br/>sys 0m0.215s<br/><br/>chanwit@lb /cygdrive/c/fortress/fortress_3625<br/>$ time `bin/fortress chanwit/hello.fss`<br/>bash: Hello,: command not found<br/><br/>real 0m11.377s<br/>user 0m0.197s<br/>sys 0m0.198s<br/><br/>chanwit@lb /cygdrive/c/fortress/fortress_3625<br/>$ time `bin/fortress chanwit/hello.fss`<br/>bash: Hello,: command not found<br/><br/>real 0m11.432s<br/>user 0m0.229s<br/>sys 0m0.198s</font><br/><br/><div class='zemanta-pixie'><img src='http://img.zemanta.com/pixy.gif?x-id=c56206ac-52f2-8511-ba6d-c97f07aeaa0a' class='zemanta-pixie-img'/></div></div>chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com2tag:blogger.com,1999:blog-7910126.post-7517271417458018482009-02-27T14:01:00.001+07:002009-02-27T14:01:36.948+07:00Hybrid call site model based on Groovy<div xmlns='http://www.w3.org/1999/xhtml'>I'm trying to model a class structure that support the following features:<br/><ul><li>callsite; separating call and execution semantics to</li><ul><li>support before/after advice of AspectJ's pointcut-advice model (possible support around advice)</li><li>further support my typing concerns.</li></ul><li>support statically resolved callsite (resolving is done at compile-time, but also changeable at runtime)</li><li>support dynamically resolved callsite (as done in Groovy, JRuby, etc.)</li><li>must be compatible with Java. i.e., Generated classes can be used as normal Java classes.<br/></li></ul> My below POC is based on Alex T. implementation. But instead of showing all kind of callsite, I show you here only statically resolved callsite. This callsite is surely fast (I hope it's gonna be at the same level of Java) because it's compile-time thing. The interesting point is that, I have a special class loader for these callsites, namely StaticCallSiteClassLoader. What does it do? This class loader allows re-definition of these callsites. <br/><br/>What I have observed so far is that All of my callsite classes (<font face='Courier New'>_0_println, _1_ctor, _2_render and _3_index</font>) have not been loaded when there is no "direct" reference to them. So they can be fully lazy loading using the StaticCallSiteClassLoader. (I've checked this with <font face='Courier New'>java --verbose</font>)<br/><br/>How could these callsites are changed at runtime?<br/>You may see the acallsite, an CallSite array that hold all callsites in the example class. It's intended to be public and can be accessed by, for example, a virtual meta-class. <br/>So what's a virtual meta-class? It's kind of meta-class, but in this case, it's not for controlling behaviour of the class (because all dispatch are static). It's for changing behaviour of callsite by reassigning new callsites object to the callsite array.<br/><br/>Notic that I use WeakReference to hold a callsite class loader. This will be making the class loader GC-able. That's it, after deferenceing all old callsites out of the callsite array, the class loader can be unloaded. Therefore, all callsite class definition are gone as well. Then, we can create a new set of callsites for the current class. This technique would be flexible enough for:<br/><ul><li>changing a static call to be a dynamic call</li><li>changing a dynamic call back to a static call</li><li>installing a woven callsite with before, after advice (it features <b>dynamic</b> AOP)</li><li>inserting type advice, which can lead to bytecode inlining</li><li>design a hybrid type system to support both static and dynamic typing in the same system.</li></ul><font face='Courier New'>import java.lang.ref.WeakReference;<br/><br/>import runtime.CallSite;<br/>import classloader.StaticCallSiteClassLoader;<br/>import dm.Render;<br/><br/>public class TestController {<br/><br/> /* synthetic */<br/> private static WeakReference<StaticCallSiteClassLoader> cl;<br/><br/> /* synthetic normal callsite */<br/> public static class _0_println extends CallSite {...}<br/><br/> /* synthetic constructor */<br/> public static class _1_ctor extends CallSite {...}<br/><br/> /* synthetic inter-type declaration callsite. statically resolved */<br/> public static class _2_render extends CallSite {...}<br/><br/> /* synthetic property callsite */<br/> public static class _3_index extends CallSite {...}<br/><br/> /* synthetic */<br/> public static CallSite[] acallsite;<br/><br/> static {<br/> cl = new WeakReference<StaticCallSiteClassLoader>(<br/> new StaticCallSiteClassLoader(TestController.class)<br/> );<br/> acallsite = new CallSite[4];<br/> acallsite[0] = cl.get().get("_0_println");<br/> acallsite[1] = cl.get().get("_1_ctor");<br/> acallsite[2] = cl.get().get("_2_render");<br/> acallsite[3] = cl.get().get("_3_index");<br/> }<br/><br/> public TestController(){<br/> super();<br/> }<br/><br/> public int getIndex() {<br/> return 10;<br/> }<br/><br/> public static void main(String[] args) {<br/> acallsite[0].callStatic("hello world");<br/> TestController t = (TestController)(acallsite[1].callConstructor());<br/> acallsite[2].call(t, "hello world");<br/> acallsite[0].callStatic(acallsite[3].callGetProperty(t, "index"));<br/> }<br/><br/>}</font><br/><br/><div class='zemanta-pixie'><img src='http://img.zemanta.com/pixy.gif?x-id=dd818309-b083-42bc-9665-7dbb76632d3a' class='zemanta-pixie-img'/></div></div>chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com0tag:blogger.com,1999:blog-7910126.post-2846823554557514412009-02-25T00:27:00.001+07:002009-02-25T00:27:10.982+07:00Using AspectJ to find a Grails' bug<div xmlns='http://www.w3.org/1999/xhtml'>It's because I've got a constraint resource (also time). I've found a Grails bug (4060) that is really hard to spot. There is no exception was thrown and I have no idea how to trace this bug because my knowledge about Spring is really limited. I also do not have any good IDE to help debug Grails well. (calling grails-debug and attaching it remotely to Eclipse won't help much in this situation).<br/><br/>After I enabled Grails log to understand some behaviour of Grails' spring package, I saw some point in the log that it's likely a bug, but the log message is inside Spring, not Grails. (Yep, I need a stack trace).<br/><br/>I then got an idea of using AspectJ to help inserting a conditional code to "dump" stack from that point. What I've used is an <font face='Courier New'>execution</font> PCD and a call to <font face='Courier New'>Thread.dumpStack();</font><br/><br/>Here's steps:<br/><ul><li>Enable trace log in grails-app/Config.groovy</li><li>Find a message</li><li>Grep to locate the point that emits the message. I found it in Spring code, not Grails.<br/></li><li>Instead of rebuild Spring, I wrote this AspectJ code:</li></ul><font face='Courier New'> pointcut pc(): <br/> execution(<br/> public Object DefaultSingletonBeanRegistry<br/> .getSingleton(String,ObjectFactory)<br/> );<br/> <br/> before(String name): pc() && args(name,..) {<br/> if(name.equals("localeResolver")) {<br/> Thread.dumpStack();<br/> }<br/> }<br/></font><br/>Note that my target Spring bean is localeResolver, so I put a condition here to dump the current stack to find which part of Grails code is doing with this bean at that time.<br/><ul><li>I then wove the above aspect into spring.jar</li></ul><font face='Courier New'>$ ajc -cp .:aspectjrt.jar:spring.jar <br/> -inpath spring.jar\ // I was going to weave spring.jar<br/> -outjar spring-2.5.6.jar\ // output would be spring-2.5.6.jar<br/> -showWeaveInfo\ // I wanted to see some weaving info<br/> -Xlint:ignore\ // ignore error when resolving some missing classes<br/> Dump.aj // the above aspect</font><br/><br/>And gotcha, I could really see the context around when this Spring bean is loaded, and reported to Grails JIRA (And Graeme, you did fix it really quickly - you're super cool!).<br/><br/>Thus, using AspectJ code + Thread.dumpStack() is kind of a conditional break point that can help you debugging Grails without an IDE.<br/><br/><div class='zemanta-pixie'><img src='http://img.zemanta.com/pixy.gif?x-id=ab87b8dc-228a-482a-b8ef-66334652ef24' class='zemanta-pixie-img'/></div></div>chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com0tag:blogger.com,1999:blog-7910126.post-12714600259511293042009-02-20T07:54:00.001+07:002009-02-20T07:54:35.059+07:00Simulate Object with Groovy's Closures and Map<div xmlns='http://www.w3.org/1999/xhtml'>After reading Chapter 18 of <a href='http://www.amazon.com/Types-Programming-Languages-Benjamin-Pierce/dp/0262162091'>Types and Programming Languages</a> by <a href='http://www.cis.upenn.edu/%7Ebcpierce/'>Benjamin C. Pierce</a>, I've got an idea to simulate how OOP is working with closures and map in Groovy. Just for fun anyway :)<br/><br/><font face='Courier New'>def func_01 = { self, props -><br/> self.name = props["name"]<br/>}<br/><br/>def func_02 = { self -><br/> println "${self.name} is running"<br/>}<br/><br/>def a = [init: func_01, run: func_02]<br/>a.self = a<br/><br/>a.init(a.self, [name:"mr. a"])<br/>a.run(a.self)</font><br/><br/><div class='zemanta-pixie'><img src='http://img.zemanta.com/pixy.gif?x-id=3df6073c-34dd-4be8-a036-9485ce1e56e9' class='zemanta-pixie-img'/></div></div>chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com1tag:blogger.com,1999:blog-7910126.post-46797616981342557392009-02-17T02:01:00.001+07:002009-02-17T02:20:27.005+07:00Explaining Typesheet<div xmlns='http://www.w3.org/1999/xhtml'>I've been designing a language for typesheet, a technique to speeding up Groovy by enforcing type for specific calls.<br/><br/>Current I can enforce type of a call by writing:<br/><br/><font face='Courier New'><b>method</b>(my.package.MyClass#main(String[])) {<br/> <b>call</b>(println(*)) && <b>args</b>(s) {<br/> <font color='#3366ff'>s ~> String;</font><br/> }<br/>}</font><br/><br/>This typesheet reads: With in the main method of class MyClass, match <i>every</i> call to meta-method "println" (<b>regardless input args</b>). For matched calls, <br/><ol><li>let MOP <b>select real methods</b> by assuming the type of input argument to be "String", and</li><li>convert values of those arguments to be "String" before passing them to the calls.</li></ol>You may notice that I emphasis 2 places. There are 2 different phases of the above code to be working. Firstly, in dynamically typed languages, like Groovy, method binding is performed at runtime. So matched call messages are not the real methods. Calls to "println" there may be anything depending on the MOP engine to select.<br/><br/>Secondly, declaration in { <font color='#3366ff'>s ~> String;</font> } (I call it a <i>type intervention</i> block) enforces MOP to select the method according to the type of "s", which is bound to the only argument of the method. ("s" has been binding using an <b>args</b> predicate).<br/><br/>So, what's happening next? You might be guessing correctly :)<br/>MOP will be fully by-passed for those matched calls (because related types are enforced). Therefore, speed will be increased to the level of normal Java calls.<br/></div>chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com0tag:blogger.com,1999:blog-7910126.post-91236990581680040342009-02-09T09:06:00.001+07:002009-02-09T09:06:14.796+07:00Playing around XUL, JavaScript and jQuery<div xmlns='http://www.w3.org/1999/xhtml'><small><font face='Courier New'><?xml version="1.0"?> <br/>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?> <br/>
<x:window <br/>
xmlns:x="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" <br/>
xmlns="http://www.w3.org/1999/xhtml" <br/>
> <br/>
<x:script type="application/x-javascript" <br/>
src="file:///c:/xul/jquery-1.2.6.js"<br/>
/> <br/>
<br/>
<x:script type="application/x-javascript; e4x=1"><br/>
<![CDATA[ <br/>
function test(){ <br/>
$('#b').attr("label", "3");<br/>
var frag = <br/>
<listitem value="4" <br/>
label="new item" <br/>
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"<br/>
/>;<br/>
$('#l').get(0)<br/>
.appendChild(<br/>
(new DOMParser()).parseFromString(<br/>
frag.toXMLString(), 'text/xml'<br/>
).documentElement<br/>
);<br/>
} <br/>
]]></x:script> <br/>
<br/>
<x:button label="click me" oncommand="test();"/> <br/>
<x:button id="b" label="2"/><br/>
<x:listbox id="l"><br/>
<x:listitem value="1" label="item label"/><br/>
<x:listitem value="2" label="item label"/><br/>
<x:listitem value="3" label="item label"/><br/>
</x:listbox><br/>
<br/>
</x:window> </font></small><br/>
<br/>
<br/>The above code is to dynamically adding a new <listitem/> into listbox #l. It will be working fine when we get some XUL fragment remotely from the server-side as well. This is Gecko 1.9.0.5.<br/><br/>What I've learned:<br/><ul><li>$("#id").get(0) returns a single element, while .get() returns a list.</li><li>e4x=1 tells the JavaScript parser to use the newer language spec.</li><li>jQuery works fine with XUL, but XML namespace is needed to avoid some gotcha because it's been designed to work with HTML, not actually XUL.</li><li>DOMParser works as expected</li><li>Gecko 1.9 solved a lot of bugs I was encountering in 1.8.</li><li>With Grails as a back-end, we can now create a dynamically half-Web/half-desktop app.<br/></li></ul></div>chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com3tag:blogger.com,1999:blog-7910126.post-41300824660309131952009-02-02T16:14:00.001+07:002009-02-02T16:16:59.497+07:00What if Groovy has got Structural Typing<div xmlns='http://www.w3.org/1999/xhtml'>Here's a quick mock class after I've been reading this <a href='http://www.jroller.com/aalmiray/entry/griffon_groovy_scala_working_together'>entry</a>.<br/><br/><small><font face='Courier New'><big>class G7Greeter {<br/><br/> def greet(String who, {def setOutput(String text)} model) {<br/> println "Hello from G7 ${who}"<br/> model.output = "Hello from G7 ${who}"<br/> }<br/><br/>}</big><br/></font></small><br/>It would be great if Groovy has Scala's structural typing feature. Structural typing is good for statically "duck typing"-like behaviour. But it seems we need a fork as Groovy dynamic typing allows multi-dispatch already. <br/></div>chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com0tag:blogger.com,1999:blog-7910126.post-73233956537448095512009-01-12T13:52:00.003+07:002009-01-12T14:00:14.032+07:00Fortress SummaryThis is a summary from <a title="Fortress Language Specification 1.0" href="http://research.sun.com/projects/plrg/fortress.pdf" id="hqst">Fortress Language Specification 1.0</a><b>.</b><b><br /><br />Fortress "secure Fortran" in a nutshell.</b><br /><ul><li>General-purpose</li><li>Statically typed</li><li>Component-based</li><li>Designed for producing robust high-performance software with "high programmability"</li><li>Intended to be a "growable language"</li><ul><li>be gracefully extended</li><li>be scaling to "unprecedented" levels of parallelism and of addressable memory</li></ul><li>Supports modular and extensible parsing</li><ul><li>allow new notations and static analyses to be added</li></ul><li>High-performance computation with abstraction and type safety</li><li>Support type inference</li><ul><li>although it's statically and nominally typed</li><li>types can be omit if they are clear in the context</li><li>types can be parametric</li></ul><li>Functions are first-class values</li><li>Components in Fortress are defined as entities with export and import APIs (not in general compared with the same term of Scala)</li><li>Built-in parallel computation support for large scale data structures, e.g. for loop is parallel by default<br /></li></ul><b>Modular Concerns</b><br /><ul><li>Object and Trait</li><ul><li>an object consists of fields and methods</li></ul><ul><li>traits are named constructs that declare sets of methods. This concept is from Self.</li><ul><li>method in traits may be abstract or concrete</li><li>trait may extend other traits</li><li>trait provides inherited methods as well as those declared in its declaration.</li></ul></ul></ul>chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com0tag:blogger.com,1999:blog-7910126.post-42804468451516443012009-01-11T14:48:00.002+07:002009-01-11T14:52:02.126+07:00Scala SummaryI have been reading Scala papers for a couple of days. Here's a quick summary from the first section of "<a title="An overview of the Scala programming language" href="http://www.scala-lang.org/docu/files/ScalaOverview.pdf" id="rkip">An overview of the Scala programming language</a>":<br><br><ul><li>Scala fuses OOP and functional programming together.</li><li>Scala has been designed for construction of components and component system, which is a goal of software industry.</li><li>Components can be in any form, any size. Classes, libraries, frameworks, or processes can be considered as components.</li><li>Most existing languages offer only limited level of abstraction and composition.</li><li>The same concept of programming should be <i>scalable</i> to use for small as well as large programs.</li><li>Scala aims to be scalable by fusing OO and functional features into it.</li><li>Every value is an object, and every operation is a method call in Scala (uniform object model).</li><li>Functions are first-class values in Scala.</li><li>Scala has a unified abstraction for both types and values.</li><li>Scala provides constructs for composing classes and traits.</li><li>Scala provides object decomposition using pattern matching.</li></ul><br>chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com0tag:blogger.com,1999:blog-7910126.post-24508013196619356442008-12-11T07:42:00.001+07:002008-12-11T07:42:30.953+07:00Syntax of Type Sheet<div xmlns='http://www.w3.org/1999/xhtml'>It's beautiful, isn't it?
<p class='code'>
class(C) => {
field(f) => f: String;
method(m) && args(a) => { // bound a
local(v) => v: int;
// order of binding -> is left to right
local(n) && call(Integer.minus(_)) && this(n) => return: int;
call(print(n)) && match(n) {
case call(Integer.plus(_)) => n: int;
default => n: String;
}
}
}
</p></div>chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com0tag:blogger.com,1999:blog-7910126.post-51482315176432230132008-10-02T11:11:00.003+07:002008-10-02T11:19:45.677+07:00Online Twitter App written in GrailsIt is very exciting to tell you that we has now successfully used Grails to clone <a href="http://election.twitter.com">Twitter's Election</a>. It seems to be a very first online Twitter application written in <a href="http://grails.org">Grails</a>. The most important thing is that the prototype has been done in just <span style="font-weight:bold;">2 days</span> and we can have 2 Grails applications (a crawler with Quartz plugin to collect tweets, and a front to serve users) working together. Now it serves for the coming Bangkok Governor Election.<br /><br />It is currently hosted by our lovely cloud, <a href="http://mor.ph">Morph Appspace</a>.<br /><br />Try it here: <a href="http://bangkok51.morphexchange.com">http://bangkok51.morphexchange.com</a>chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com3tag:blogger.com,1999:blog-7910126.post-16561145501572857832008-09-20T15:35:00.003+07:002008-09-20T15:48:57.700+07:00A non-English BDD frameworkI am a big fan of <a href="http://easyb.org">easyb</a>, a BDD framework for Groovy and, of course you can use it to validate spec of your Java programs as it's running on JVM.<br /><br />But what I really want to have is a support of my native, Thai, language. Recently found out that Groovy can compile UTF-8 code, and I have no problem to make it invoke some method with Thai characters. So, I have been porting the idea from easyb into my own, non-English BDD framework, <a href="http://github.com/chanwit/tspec/tree/master">TSpec</a>. <br /><br />Now you can tell a story of software specification in <a href="http://en.wikipedia.org/wiki/Thai_language">Thai</a>, wanna learn it ? <br /><br />Here's at <a href="http://github.com/chanwit/tspec/tree/master">Github</a>.chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com0tag:blogger.com,1999:blog-7910126.post-4265888672258893052008-09-19T19:16:00.004+07:002008-09-19T20:23:02.250+07:00Hangman in ZKGrailsActually, it's not for your eyes, but Robert Lee has found my hangman source code via <a href="http://pastebin.com/f3eea6d28">Pastbin</a>. Then he posted it on <a href="http://www.dzone.com/links/hangman_build_with_groovy_on_zk_instead_of_grails.html">DZone</a>, but what he mentioned is not 100% correct. This hangman app is built on <a href="http://code.google.com/p/zkgrails/">ZKGrails</a>, which is a ZK plugin for Grails (not only Groovy - check the language tag there in the source code it's 'GroovyGrails', not 'Groovy'). <br /><br />Hangman is the first challenge among us, a group of PAW66 developer sites in Thailand.(<a href="http://rails66.com">rails66</a>, <a href="http://grails66.com">grails66</a>, <a href="http://django66.com">django66</a>, <a href="http://seam66.com">seam66</a> etc.) We set this program up to show the power of each Web framework we love. I have blogged about this <a href="http://www.grails66.com/blog/?p=309">hangman</a> in Thai on Grails66.com, if you can read. That's why I said it's not intended for your eyes.<br /><br />One more thing I'd like to mention is that I host <a href="http://hangman.morphexchange.com/index.zul">this app</a> with <a href="http://mor.ph/">Morph AppSpace</a>. It's really cool that not only you can host Rails apps, all kind of my Java apps including <a href="http://grails.org">Grails</a> and even <a href="http://zkoss.org">ZK</a> just work there. You did the great job, guys !chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com2tag:blogger.com,1999:blog-7910126.post-47083690440994224232008-09-18T03:40:00.001+07:002008-09-18T03:43:15.042+07:00Trust me, Grails will be a platformRecently, SAP has released its <a href="https://wiki.sdn.sap.com/wiki/display/Community/Composition+On+Grails">Composition on Grails 1.0</a>, the world first Grails distribution outside the project itself.<br />Yes, I said it is "the world first Grails distribution". This is really important to me as the release sent out a special signal that "Grails" will be "something" more that a normal web framework on JVM.<br /><br />Thanks to Grails plugin architecture, you are able to get its core (thinking of it as a Linux kernel), put some plugins and scripts (think of them as programs around Linux), pack them together and that's it ! You got a web framework distribution that contains special features !<br /><br />For example, imagine that you can have a Blog-ready Grails distro, a distro that contains some preset plugins, which help you create a blog like Wordpress in minutes. Or you can have a CMS-ready Grails distro, which enable you to 'grails create-cms' and you've got a Drupal-like system.<br /><br />This will be a big step to take Grails into the next level ! Trust me, it's going to be a platform !chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com0tag:blogger.com,1999:blog-7910126.post-45683079748309734212008-08-28T06:49:00.005+07:002008-08-28T07:01:51.198+07:00Hey Guillaume Laforge, Thai Geeks are going to interview youAfter Groovy has been got the big attention in Thai community this moment, I've been co-operating with <a href="http://www.isriya.com">Isriya</a> of <a href="http://www.blognone.com/">Blognone.com</a> (a leading technology news site in Thailand) to arrange an interview for <a href="http://glaforge.free.fr/weblog/">Guillaume Laforge</a>. This interview will be a community-based. All question will come from news readers, which of course are Thai geeks. After collecting questions there, I'll forward them to Guillaume via email. <br /><br />This will be the first time for them to get in touch with him.<br /><br />See the post (if you can read Thai): <a href="http://www.blognone.com/node/8770">http://www.blognone.com/node/8770</a>chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com0tag:blogger.com,1999:blog-7910126.post-68614618378444880272008-08-15T20:15:00.001+07:002008-08-15T20:18:25.464+07:00Do anything at BarCamp Bangkok 2To help promote BarCamp Bangkok 2, here's my screencast:<br /><br /><a href="http://screencast.com/t/JRZprfG6">http://screencast.com/t/JRZprfG6</a><br /><br />Enjoy !chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com0tag:blogger.com,1999:blog-7910126.post-39339531893045919832008-08-15T07:19:00.003+07:002008-08-15T07:43:46.139+07:00A Java-near-speed GroovyAlmost 2 months that I have not posted here. I'm still in the final phase of Google Summer of Code. The world is going to be wonderful when you have got a really good result from your hard work, is that right ?<br /><br />I'm currently be able to make Groovy speed step closer to Java. It's a high aim, and it's clear to be possible with JVMIT now. I was working on optimising callsites, but could not find a way to make it faster than Alex T.'s implementation because he did really good job of implementing it. So I went back to look at my old GJIT code. What I found is that something had been fooling my eyes for almost a year. There is a way to avoid JVM 6 specific code to implement an agent for JVM 5. Then I re-read AspectJ source again, and found something useful. I has started to re-write a <a href="http://code.google.com/p/gjit/">GJIT</a> agent for JVM 5 from then. <br /><br />My prototype of the second generation of GJIT was done with <a href="http://www.sable.mcgill.ca/soot/">Soot</a> framework, where I really learned a lot of useful optimisation tricks (e.g., SourceValue). Later, I found that Soot is not suite for implementing JIT, because of 1. it's somewhat big structure 2. it's class loading problem. I then have had to implement a whole thing using <a href="http://asm.objectweb.org/">ASM</a>. It really was a nightmare because there is no Jimple to help your analysis. I re-wrote at least 3-4 version of ASM optimisers, and finally got the current one, which has a good enough structure to patch. <br /><br />Now my goal is to beat all <a href="http://shootout.alioth.debian.org/">shootout</a> benchmarks. Here's the result from <a href="http://shootout.alioth.debian.org/gp4/benchmark.php?test=partialsums&lang=groovy">partial sums benchmark</a> compared with Java on my machine.<br /><br /><p class="code"><br />$ export JAVA_OPTS="-server"<br />$ time `java -cp bin alioth.PartialSumsJ 5000000`<br />real 0m10.246s<br />user 0m0.030s<br />sys 0m0.015s<br /><br />$ time `groovy.bat test/partial_sums.groovy 5000000`<br />real 0m27.110s<br />user 0m0.030s<br />sys 0m0.016s<br /><br />$ time `groovy.bat test/alioth/PartialSums.groovy 5000000`<br />real 0m28.409s<br />user 0m0.030s<br />sys 0m0.015s<br /><br />$ export JAVA_OPTS="-server<br />-javaagent:C:\\groovy-ck1\\healthy\\target\\install\\lib\\gjit-0.1.jar"<br />$ time `groovy.bat test/alioth/PartialSums.groovy 5000000`<br />real 0m13.434s<br />user 0m0.030s<br />sys 0m0.030s<br /></p><br /><br />It's 200% faster than current Groovy, and ~20-30% slower than Java. There's still room to go. I hope to take some more steps closer to Java's speed.chanwithttp://www.blogger.com/profile/00064240675718056931noreply@blogger.com11