You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by Felix Meschberger <fm...@gmail.com> on 2009/04/06 11:32:53 UTC

Support for Java Scripting Script Precompilation (was: Support XML-compliant script delimiter)

Hi,

Bertrand Delacretaz schrieb:
> On Mon, Apr 6, 2009 at 11:00 AM, Felix Meschberger <fm...@gmail.com> wrote:
>> ...We should then probably also extend the Rhino script engine to cache
>> compiled ecma scripts and reuse them, similar to what the JSP script
>> engine does (no we probably don't need to write class files)....
> 
> This is something that we should do in any case, to improve ESP
> scripting performance.
> 
> Rhino does provide a compiler
> (https://developer.mozilla.org/en/Rhino_JavaScript_Compiler), haven't
> tested it yet.

This works, and I used it before ...

Rhino always runs scripts  in two phases: "compilation" and "execution".
The result of the first step is generally an instance of the Script
interface. This interface has a method to execute the script, which is
call by RHino in the second phase.

How about this approach, thus also leveraging the full scope of JSR 223
(Java Scripting): The Java Scripting API provides API to allow for
precompiled scripting languages. By leveraging this mechanism we can add
support for precompiled scripts and enhanced performance to all
scripting languages.

We maintain a script cache in the Scripting Core bundle. This cache has
the following attributes:

  * Indexed by script path name, entries are weak or soft references
    to JSR-223 CompiledScript instances (and some time stamp to help
    decide for recompilation)

  * When trying to run a script the following steps take place:

        - check for a cache entry
        - if existing and script source is not more recent
           than the cache entry, us it and we are done
        - if the cache entry is outdated, remove it and continue

        - check whether the ScriptEngine allows precompilation.
        - if yes: compile the script, enter it into the cache
            and run the CompiledScript
        - if no: just have the ScriptEngine read and run the
            script.

This can already be easily used for our own JSP Script Engine. It may
probably also be used for the Groovy ScriptEngine, since it supports
script precompilation. For our own Rhino ScriptEngine we would just add
support for this and be done.

WDYT ?

Regards
Felix

Re: Support for Java Scripting Script Precompilation

Posted by Felix Meschberger <fm...@gmail.com>.
Hi,

Bertrand Delacretaz schrieb:
> On Mon, Apr 6, 2009 at 11:32 AM, Felix Meschberger <fm...@gmail.com> wrote:
>> Hi,
>>
>> Bertrand Delacretaz schrieb:
>>> On Mon, Apr 6, 2009 at 11:00 AM, Felix Meschberger <fm...@gmail.com> wrote:
>>>> ...We should then probably also extend the Rhino script engine to cache
>>>> compiled ecma scripts and reuse them, similar to what the JSP script
>>>> engine does (no we probably don't need to write class files)....
>>> ...Rhino does provide a compiler
>>> (https://developer.mozilla.org/en/Rhino_JavaScript_Compiler)...
> 
>> ...How about this approach, thus also leveraging the full scope of JSR 223
>> (Java Scripting): The Java Scripting API provides API to allow for
>> precompiled scripting languages. By leveraging this mechanism we can add
>> support for precompiled scripts and enhanced performance to all
>> scripting languages.
>>
>> We maintain a script cache in the Scripting Core bundle. This cache has
>> the following attributes:
>>
>>  * Indexed by script path name, entries are weak or soft references
>>    to JSR-223 CompiledScript instances (and some time stamp to help
>>    decide for recompilation)
> 
> Ok. I think this cache should also have some form of size limitation -
> maybe simply limit the total number of CompiledScript instances in the
> cache, and purge least recently used entries when that limit is
> reached?

Sure. And of course the limit would be configurable ;-)

I just failed to add this other restriction

Regards
Felix


> 
>> ... * When trying to run a script the following steps take place:
>>
>>        - check for a cache entry
>>        - if existing and script source is not more recent
>>           than the cache entry, us it and we are done
>>        - if the cache entry is outdated, remove it and continue
> 
> ok
> 
>>        - check whether the ScriptEngine allows precompilation.
>>        - if yes: compile the script, enter it into the cache
>>            and run the CompiledScript
>>        - if no: just have the ScriptEngine read and run the
>>            script.
> 
> ok
> 
>> This can already be easily used for our own JSP Script Engine. It may
>> probably also be used for the Groovy ScriptEngine, since it supports
>> script precompilation. For our own Rhino ScriptEngine we would just add
>> support for this and be done.
> 
> Sound great!
> -Bertrand
> 

Re: Support for Java Scripting Script Precompilation (was: Support XML-compliant script delimiter)

Posted by Bertrand Delacretaz <bd...@apache.org>.
On Mon, Apr 6, 2009 at 11:32 AM, Felix Meschberger <fm...@gmail.com> wrote:
> Hi,
>
> Bertrand Delacretaz schrieb:
>> On Mon, Apr 6, 2009 at 11:00 AM, Felix Meschberger <fm...@gmail.com> wrote:
>>> ...We should then probably also extend the Rhino script engine to cache
>>> compiled ecma scripts and reuse them, similar to what the JSP script
>>> engine does (no we probably don't need to write class files)....
>>
>> ...Rhino does provide a compiler
>> (https://developer.mozilla.org/en/Rhino_JavaScript_Compiler)...

> ...How about this approach, thus also leveraging the full scope of JSR 223
> (Java Scripting): The Java Scripting API provides API to allow for
> precompiled scripting languages. By leveraging this mechanism we can add
> support for precompiled scripts and enhanced performance to all
> scripting languages.
>
> We maintain a script cache in the Scripting Core bundle. This cache has
> the following attributes:
>
>  * Indexed by script path name, entries are weak or soft references
>    to JSR-223 CompiledScript instances (and some time stamp to help
>    decide for recompilation)

Ok. I think this cache should also have some form of size limitation -
maybe simply limit the total number of CompiledScript instances in the
cache, and purge least recently used entries when that limit is
reached?

> ... * When trying to run a script the following steps take place:
>
>        - check for a cache entry
>        - if existing and script source is not more recent
>           than the cache entry, us it and we are done
>        - if the cache entry is outdated, remove it and continue

ok

>
>        - check whether the ScriptEngine allows precompilation.
>        - if yes: compile the script, enter it into the cache
>            and run the CompiledScript
>        - if no: just have the ScriptEngine read and run the
>            script.

ok

> This can already be easily used for our own JSP Script Engine. It may
> probably also be used for the Groovy ScriptEngine, since it supports
> script precompilation. For our own Rhino ScriptEngine we would just add
> support for this and be done.

Sound great!
-Bertrand