Author: sshafroi Date: 2008-07-14 10:18:45 +0200 (Mon, 14 Jul 2008) New Revision: 6738
Modified: trunk/query-api/src/main/java/no/sesat/search/query/Visitor.java trunk/query-api/src/main/java/no/sesat/search/query/parser/AbstractReflectionVisitor.java Log: Implement caching of methods used in the visitor pattern. Below are some measurements of the changed times for searches. 100x http://localhost.no:8080/katalog/1 2 3 4 5 6 7 8 9 228.053s 48.716s 79% improvement 100x http://localhost.no:8080/fisk 16.187s 13.399s 100x http://localhost.no:8080/h?\195?\165vard fr?\195?\184iland oslo 176.020s 134.738s 24% improvement 10x http://localhost.no:8080/snekker oslo 17.982s 13.170s 27% improvement Modified: trunk/query-api/src/main/java/no/sesat/search/query/Visitor.java =================================================================== --- trunk/query-api/src/main/java/no/sesat/search/query/Visitor.java 2008-07-09 18:25:04 UTC (rev 6737) +++ trunk/query-api/src/main/java/no/sesat/search/query/Visitor.java 2008-07-14 08:18:45 UTC (rev 6738) @@ -1,5 +1,5 @@ /* - * Copyright (2005-2007) Schibsted Søk AS + * Copyright (2005-2008) Schibsted Søk AS * This file is part of SESAT. * * SESAT is free software: you can redistribute it and/or modify @@ -29,5 +29,5 @@ * * @param clause the object the visitor will operate on. */ - void visit(Object clause); + void visit(Clause clause); } Modified: trunk/query-api/src/main/java/no/sesat/search/query/parser/AbstractReflectionVisitor.java =================================================================== --- trunk/query-api/src/main/java/no/sesat/search/query/parser/AbstractReflectionVisitor.java 2008-07-09 18:25:04 UTC (rev 6737) +++ trunk/query-api/src/main/java/no/sesat/search/query/parser/AbstractReflectionVisitor.java 2008-07-14 08:18:45 UTC (rev 6738) @@ -1,4 +1,4 @@ -/* Copyright (2005-2007) Schibsted Søk AS +/* Copyright (2005-2008) Schibsted Søk AS * This file is part of SESAT. * * SESAT is free software: you can redistribute it and/or modify @@ -24,6 +24,12 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.WeakHashMap; + +import no.sesat.Interpreter; +import no.sesat.Interpreter.Context; +import no.sesat.search.query.Clause; import no.sesat.search.query.Visitor; import org.apache.log4j.Logger; @@ -69,8 +75,22 @@ * closest match to the clause subclass. * @param clause the clause we're visiting. */ - public void visit(final Object clause) { - final Method method = getMethod(clause.getClass()); + private static WeakHashMap <Class<? extends Visitor>, HashMap<Class<? extends Clause>, Method>> cache = + new WeakHashMap<Class<? extends Visitor>,HashMap<Class<? extends Clause>, Method>>(); + + public void visit(final Clause clause) { + HashMap<Class<? extends Clause>, Method> map = cache.get(getClass()); + if (map == null) { + map = new HashMap<Class<? extends Clause>, Method>(); + cache.put(getClass(), map); + } + Method method = map.get(clause.getClass()); + if (method == null) { + method = getMethod(clause.getClass()); + map.put(clause.getClass(), method); + } + assert method.equals(getMethod(clause.getClass())); + try { method.setAccessible(true); method.invoke(this, new Object[] {clause}); @@ -106,10 +126,6 @@ } private Method getMethod(final Class clauseClass) { - - // XXX This is one of the applications performance hotspots. - // It could be benefit to keep a weak reference map to remember what method to use. - Method method = null; LOG.trace("getMethod(" + clauseClass.getName() + ")"); @@ -205,4 +221,27 @@ } } + /** + * Add some debug function to the interpreter. + */ + static { + Interpreter.addFunction("visitor-methods", new Interpreter.Function() { + public String execute(Context ctx) { + String res = ""; + for (Class<? extends Visitor> cv : cache.keySet()) { + res += cv.getName() + "\n"; + for (Class<? extends Clause> cc : cache.get(cv).keySet()) { + res += " " + cc.getName() + " - " + cache.get(cv).get(cc).toGenericString() + "\n"; + } + } + res += "Total: " + cache.size(); + return res; + } + + public String describe() { + return "Print out the methods in the cach used by the visitors."; + } + + }); + } } _______________________________________________ Kernel-commits mailing list [email protected] http://sesat.no/mailman/listinfo/kernel-commits
