|
You may have noticed that Jostraca can
be a bit slow. This tutorial will help you to speed up the code
generation process, by showing you how to configure and use Jostraca
more effectively. The main reason for performance problems comes from
the cross-language support built into Jostraca. While this allows you
to write templates using many different programming languages, it
also means that Jostraca has to rely on external programs to actually
do the code generation.
Generating versus Compiling
Jostraca generates code by first
creating a CodeWriter program, and then by executing
the CodeWriter program. After the very first time that
you generate from a template using Jostraca, the CodeWriter
exists. So later, if the template has not changed, you don't need to
recreate and recompile the CodeWriter program.
Instead, you can just run it directly.
To do this, use the -C
command line option to Jostraca. This option turns template
compilation off. Now, Jostraca does not try to create the
CodeWriter, instead it just executes the existing one.
Avoiding the compilation phase can save a lot time, especially for
compiled languages like Java and C, where compilation can be time
consuming.
This option is most useful when you are
merely changing your meta data and the template remains unchanged.
Interpreting versus Compiling
If you are using a compiled language
such as Java or C for the template script (the code inside the
<% and %>
markers), then the CodeWriter program first has to be
compiled before it can be used. Compilation is usually done by
calling an external program. Launching external programs is always
going to cause a delay.
Given this problem, consider using an
interpreted language for your template script. Using Perl, Python,
Ruby, REBOL, Jython or similar can greatly increase code generation speed. These
languages also have robust text processing abilities that make them
more effective as scripting language. They also have large libraries
of support code to help you access XML resources (if you store your
meta data using XML). Of course, sometimes they are not appropriate,
but particularly for smaller generation tasks they can be very
effective.
Compile Internally
Jostraca versions 0.4 and later always attempt to
compile Java templates internally by creating an instance of com.sun.tools.javac.Main.
This class is provided with the standard Java JDK from Sun and is located in the rt.jar
jar file. Include rt.jar in your CLASSPATH and Jostraca will use it automatically.
Because a separate javac process is not created, compilation is much faster.
Use a Bigger Hammer
If you have to use a compiled language,
use the fastest compiler you can find. For Java, this means you
should use the jikes
compiler from IBM. To do this, rename the file local.conf.sample
in the conf folder of the Jostraca distribution, to local.conf
and uncomment the lines:
#main.ExternalCompiler = jikes
#main.process.Compiler = org.jostraca.process.ExternalJavaCompiler
by removing the#
symbol. You'll need to make sure that the path to the jikes
executable is in your PATH
environment variable (or just put in the full path to jikes). For
other languages, you can use the same technique, but you'll have to
determine the fastest compiler yourself.
|