Details
-
Bug
-
Status: Resolved
-
Resolution: Fixed
-
1.0 Beta 1
-
None
-
None
-
Operating System: other
Platform: Other
Description
>
> george moudry wrote:
> >
> > Hi Christophe and Vincent,
> > I modified the javascript code so it stores last few compiled scripts.
> > I don't know the proper format of a patch email, so I'm sending output of a
'diff' command.
> > If I can help with some other format, let me know.
> > Regards,
> > /george
> >
> > PS. This tested okay on my IBM-JDK1.3 system on SuSE 7.2.
> >
> > Modified: sources/org/apache/batik/script/rhino RhinoInterpreter.java
> > gg@pes:~/prj/batik/sources/org/apache/batik/script/rhino > diff
RhinoInterpreter.java.orig RhinoInterpreter.java
> > 28a29
> > > import org.mozilla.javascript.Script;
> > 88a90,112
> > > // this Map holds precompiled objects, keyed by the text of the
script.
> > > private static java.util.TreeMap compiledScriptsMap = new
java.util.TreeMap();
> > >
> > > // store last 32 precompiled objects.
> > > // when we compile the 33rd, the eldest object will be deleted.
> > > private static final int MAX_CACHED_SCRIPTS = 32;
> > >
> > > // allow debugging
> > > public static final boolean DEBUG = false;
> > >
> > > // this variable 'staticIndex' is incremented on every access
> > > // (compiled objects with lowest index will be removed from Map)
> > > private static long staticIndex = 0;
> > >
> > > /** helper class to remember both a compiled object and time when
it was used.
> > > */
> > > class ScriptAndIndex {
> > > org.mozilla.javascript.Script script;
> > > long
lastUseIndex;
> > > ScriptAndIndex(Script s,long i)
> > >
> > >
> > 91c115,116
> > < /**
> > —
> > >
> > > /**
> > 95c120,124
> > < * value of the last expression evaluated in the script
> > —
> > > * value of the last expression evaluated in the script.
> > > *
> > > * 2001.Oct.03: we call compileReader()+exec() instead of
evaluateReader().
> > > * This has the benefit that for "onmousemove=bubbleYes(evt,'red')"
code,
> > > * we do not re-compile and classload for every move of the mouse.
> > 98,99c127,138
> > < throws InterpreterException, IOException {
> > < Object rv = null;
> > —
> > > throws InterpreterException, IOException
> > > {
> > > //first, read in entire script and convert it to string.
> > > // That was, we can use it as a Map key.
> > > StringBuffer sb = new StringBuffer();
> > > int chr = scriptreader.read();
> > > while(chr!=-1)
> > > String codeString = sb.toString();
> > >
> > 101,105c140,187
> > < try {
> > < rv = ctx.evaluateReader(globalObject,
> > < scriptreader,
> > < "<SVG>",
> > < 1, null);
> > —
> > > Script script = null; //script will be either compiled
fresh or looked up in the Map.
> > > synchronized(compiledScriptsMap){
> > > ScriptAndIndex sni = (ScriptAndIndex)
compiledScriptsMap.get(codeString);
> > > if(sni==null){
> > > //this script has not been compiled yet:
> > > //compile it and store it for future use.
> > > script = ctx.compileReader( globalObject,
//scope
> > >
new java.io.StringReader(codeString),
> > >
"<SVG>", //sourceName
> > >
1, //lineNo
> > >
null ); //securityDomain
> > >
> > >
if(compiledScriptsMap.size()>MAX_CACHED_SCRIPTS)
> > > {
> > > //too many cached items - delete
the oldest entry.
> > > java.util.Iterator iter =
compiledScriptsMap.keySet().iterator();
> > > long oldestIndex = Long.MAX_VALUE;
> > > String oldestKey = null;
> > > while(iter.hasNext()){
> > > String key = (String)
iter.next();
> > > sni = (ScriptAndIndex)
compiledScriptsMap.get(key);
> > > if(
sni.lastUseIndex<oldestIndex )
> > > }
> > >
compiledScriptsMap.remove(oldestKey);
> > > }
> > >
> > > // stroring is done here:
> > > sni = new
ScriptAndIndex(script,staticIndex++);
> > > compiledScriptsMap.put(codeString,sni);
> > > if(DEBUG)
> > >
> > > } else {
> > > //this script has been compiled before,
> > > //just update it's index so it won't get
deleted soon.
> > > script = sni.script;
> > > sni.lastUseIndex = staticIndex++;
> > > if(DEBUG)
> > > }
> > >
> > > } //end sychronized
> > >
> > > Object rv = null;
> > > try {
> > > rv = script.exec(ctx, globalObject);