Skip to content

Commit

Permalink
Peformance enhancement for script
Browse files Browse the repository at this point in the history
1. only initialize standard javascript object one time for engine.
2. Add script cache to report runnable.
  • Loading branch information
Gang Liu committed Jul 30, 2008
1 parent 6c26651 commit 56d4fdd
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package org.eclipse.birt.core.script;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -48,10 +49,15 @@ public class ScriptContext
*/
protected Scriptable scope;

/**
* a cache storing compiled script
*/
protected Map<String, Script> compiledScripts = new HashMap<String, Script>( );

/**
* a cache storing ScriptExpression
*/
protected HashMap compiledScripts = new HashMap( );
protected HashMap<String, ScriptExpression> scriptExpressionCache = new HashMap<String, ScriptExpression>( );

/**
* for BIRT globel varible "params"
Expand All @@ -74,12 +80,18 @@ public ScriptContext( ScriptableObject root )
global = new ImporterTopLevel( );
if ( root != null )
{
global.exportAsJSClass( 3, global, false );
global.delete( "constructor" );
global.setPrototype( root );
}
global.initStandardObjects( context, true );
else
{
global.initStandardObjects( context, true );
}
this.scope = global;
sharedScope = context.newObject( scope );

sharedScope.setParentScope( scope );

}
catch ( Exception ex )
{
Expand All @@ -93,6 +105,11 @@ public ScriptContext( ScriptableObject root )
}
}

public void setCompiledScripts(Map<String, Script> compiledScripts)
{
this.compiledScripts = compiledScripts;
}

/**
* @param name
* the name of a property
Expand All @@ -115,7 +132,8 @@ public void exit( )
{
Context.exit( );
context = null;
compiledScripts.clear( );
compiledScripts = null;
scriptExpressionCache.clear();
}
}

Expand Down Expand Up @@ -143,7 +161,7 @@ public Scriptable enterScope( Scriptable newScope )
{
newScope = context.newObject( scope );
}
newScope.setPrototype( scope );
newScope.setParentScope( scope );
scope = newScope;
sharedScope.setPrototype( scope );
return newScope;
Expand All @@ -154,7 +172,7 @@ public Scriptable enterScope( Scriptable newScope )
*/
public void exitScope( )
{
Scriptable protoScope = scope.getPrototype( );
Scriptable protoScope = scope.getParentScope( );
if ( protoScope != null )
scope = protoScope;
sharedScope.setPrototype( scope );
Expand Down Expand Up @@ -206,12 +224,12 @@ public Object eval( String source )
{
if ( null != source && source.length( ) > 0 )
{
ScriptExpression scriptExpression = (ScriptExpression) compiledScripts
ScriptExpression scriptExpression = scriptExpressionCache
.get( source );
if ( scriptExpression == null )
{
scriptExpression = new ScriptExpression( source );
compiledScripts.put( source, scriptExpression );
scriptExpressionCache.put( source, scriptExpression );
}
return eval( scriptExpression );
}
Expand All @@ -232,10 +250,18 @@ public Object eval( ScriptExpression expr )
Script script = expr.getCompiledScript( );
if ( script == null )
{
script = context.compileString( expr.getScriptText( ),
expr.getId( ),
expr.getLineNumber( ),
null );
String source = expr.getScriptText( );
if ( context.getDebugger( ) != null )
{
source = source + expr.getLineNumber( );
}
script = compiledScripts.get( source );
if ( script == null )
{
script = context.compileString( expr.getScriptText( ), expr
.getId( ), expr.getLineNumber( ), null );
compiledScripts.put( source, script );
}
expr.setCompiledScript( script );
}
Object value = script.exec( context, scope );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public ReportRunnable saveDesign( ReportRunnable runnable,
{
if ( originalRunnable != null && runnable != originalRunnable )
{
ReportDesignHandle design = runnable.getReport( );
ReportDesignHandle design = originalRunnable.getReport( );
out = archive.createRandomAccessStream( ORIGINAL_DESIGN_STREAM );
// design.serialize( out );
DocumentUtil.serialize( design, out );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private void setupScriptScope( )
}
try
{
rootScope = new ImporterTopLevel(cx);//cx.initStandardObjects( null, true );
rootScope = cx.initStandardObjects( );
registerBeans( rootScope, config.getConfigMap( ) );
registerBeans( rootScope, config.getScriptObjects( ) );
IStatusHandler handler = config.getStatusHandler( );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@
package org.eclipse.birt.report.engine.api.impl;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.birt.report.engine.api.IImage;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.script.element.IReportDesign;
import org.eclipse.birt.report.engine.ir.Report;
import org.eclipse.birt.report.engine.parser.ReportParser;
import org.eclipse.birt.report.engine.script.internal.element.ReportDesign;
import org.eclipse.birt.report.model.api.ConfigVariableHandle;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.FactoryPropertyHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.elements.structures.EmbeddedImage;
import org.mozilla.javascript.Script;

/**
* Engine implementation of IReportRunnable interface
Expand All @@ -44,6 +45,23 @@ public class ReportRunnable implements IReportRunnable
* reference to report engine
*/
protected IReportEngine engine = null;

protected Hashtable<String, Script> cachedScripts = new Hashtable<String, Script>();

public Map<String, Script> getScriptCache()
{
return cachedScripts;
}

public Script getScript(String source)
{
return cachedScripts.get( source );
}

public void putScript(String source, Script script)
{
cachedScripts.put( source, script );
}

/**
* constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,10 @@ private void initializeScriptContext( )
{
scriptContext = new ScriptContext( );
}
if (runnable != null && runnable instanceof ReportRunnable)
{
scriptContext.setCompiledScripts( ((ReportRunnable)runnable).getScriptCache() );
}

Context context = scriptContext.getContext( );
try
Expand Down Expand Up @@ -1213,6 +1217,7 @@ public void setRunnable( IReportRunnable runnable )
this.runnable = (ReportRunnable)runnable;
if (scriptContext != null)
{
scriptContext.setCompiledScripts( ((ReportRunnable)runnable).getScriptCache( ) );
registerDesign( runnable );
}
}
Expand All @@ -1224,6 +1229,11 @@ public void updateRunnable( IReportRunnable newRunnable )
this.originalRunnable = this.runnable;
}
this.runnable = (ReportRunnable) newRunnable;
if (scriptContext != null)
{
scriptContext.setCompiledScripts( ((ReportRunnable)runnable).getScriptCache( ) );
registerDesign(runnable);
}
reportIR = null;
}

Expand Down

0 comments on commit 56d4fdd

Please sign in to comment.