Caching Reflector.getMethods, as described below, works pretty well. Performance seems acceptable for now. Startup time is still dramatic, need to investigate. Considering releasing changes as a fork on github is anybody is interested.
On Sat, Jan 31, 2009 at 8:35 PM, Remco van 't Veer <rwvtv...@gmail.com> wrote: > Some naive caching code does speed up my sample by 4 times. Will > investigate further later this week, need to take my flu to bed now.. > > > diff --git a/src/jvm/clojure/lang/Reflector.java > b/src/jvm/clojure/lang/Reflector.java > index f530b78..eccebb8 100644 > --- a/src/jvm/clojure/lang/Reflector.java > +++ b/src/jvm/clojure/lang/Reflector.java > @@ -296,7 +296,21 @@ static public Field getField(Class c, String > name, boolean getStatics){ > return null; > } > > +static private java.util.Map<String,List> methodsCache = new > java.util.HashMap<String,List>(); > + > static public List getMethods(Class c, int arity, String name, > boolean getStatics){ > + synchronized (methodsCache) { > + String key = "" + c + "-" + arity + " " + name + " " + getStatics; > + if (methodsCache.containsKey(key)) { > + return methodsCache.get(key); > + } else { > + List methods = originalGetMethods(c, arity, name, getStatics); > + methodsCache.put(key, methods); > + return methods; > + } > + } > +} > +static public List originalGetMethods(Class c, int arity, String > name, boolean getStatics){ > Method[] allmethods = c.getMethods(); > ArrayList methods = new ArrayList(); > ArrayList bridgeMethods = new ArrayList(); > > > On Sat, Jan 31, 2009 at 3:55 PM, Remco van 't Veer <rwvtv...@gmail.com> wrote: >> I've been playing around with clojure on the android platform. To my >> surprise it was pretty easy to get running, after removing the bean >> method from the clojure.jar as hinted by Rich somewhere. Of course >> clojure code needs to be compiled AOT and eval doesn't work, as was to >> be expected, but all seems to run fine otherwise. >> >> There's a problem though, it's really slow.. I've played around with >> the tracing tools of the android sdk and noticed about half of the >> time spend is doing reflection related things. Reflection on Android/ >> Dalvik is known to be slow. At the top of the method list sorted by >> exclusive times is clojure/lang/Reflector.getMethods with a 10% score >> of total time spend, followed by java/lang/reflect/ >> ReflectionAccessImpl.clone, java/lang/reflect/Method.<init>, java/ >> lang/ClassCache.deepCopy, java/lang/reflect/Method.getName and java/ >> lang/String.equals and java/lang/reflect/AccessibleObject.<init>. >> >> My test case was pretty simpel; get the public-time-line from twitter, >> parse it with xml/parse (needs a small tweak to work on android btw), >> populate some struct-maps and display a listview. Unfortunately this >> simple task takes about 12 seconds to complete on a g1 phone. >> >> So I setup *warn-on-reflection* and put type hints in all the >> appropriate places but didn't detect any performance improvements. >> Then I ran compile-clojure in the clojure source tree with an adjusted >> compiler to see the reflection warning in the core. This yields about >> 40 warnings and I wonder if fixing those will improve performance at >> all. >> >> Another approach is to try and speedup clojure/lang/ >> Reflector.getMethods by caching results. Any idea's on what route >> will get me the best results? >> >> Thanks, >> Remco > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---