fviktor
2011-07-17 00:07:10 UTC
Hi,
I think You are the most interested audience for my latest product, a
working and tested template compiler for Genshi:
http://code.google.com/p/genshi-compiler/
License: MIT
I know that there's prior art in this area like Kajiki. There have
been some discussion and some good suggestions about this topic here
as well. I have been trying to read through myself on them recently.
My story which led me to the development of this package in short:
I had a maintainable, but somewhat complex Genshi template which
rendered a rather complicated HTML document from an equally complex
tree of Python objects. I could not get it nicer, it is all legacy
code. It worked fine for a few object, but started to be insanely slow
for 100-200 such object laid out in a 4-6 level deep tree structure.
It took ~600ms on a pretty fast Core i7 machine just to render the
already loaded template. It was unbearable on a production server.
Yes, it contains recursive template functions, it was the cleanest way
to develop this kind of template.
So I had three choices:
* Rewrite it in pure Python (or Cython) to get better speed, but it
would mean loosing most of the maintainability and the possibility of
generating broken markup. It would also pose a great risk of breaking
functionality, which is quite hard to test, since the generated HTML
is part of a user interface...
* Rewrite the whole thing in a fast text template language, which has
an existing mechanism to compile the template to Python source or byte
code. It would risk the generation of broken markup as well.
* Trying to compile it to pure Python. It could also be used for my
other templates. There are lots of them...
I don't use too much of Genshi's more dynamic features, like py:match.
(I tried to build my template that way in the past, but the rendering
became too slow and it wasn't much more maintainable than before.) I
don't use token filters for other than language translation (Babel),
which could be incorporated otherwise. I'm using xi:include, but it
could be replaced by simply importing another compiled template module
and calling a compiled template function directly.
So I decided to take the red pill and write such a compiler. The first
version completed in ~20 hours, but I wasn't satisfied with the
results. It worked, but the code was somewhat fragile and hard to
extend. So I completely refactored and extended it in another ~40
hours, which led to reasonable results. Now it has good unit test
coverage and hope to receive more features in the near future.
i18n:msg and i18n in general will be implemented soon, since I need
that for most of my other templates.
xi:include might be implemented by introducing a mapping from template
file names (as given in href) and the template modules generated
(fully qualified import names).
token stream support seems to be doable as well (e.g. the compiled
template would generate a compatible token stream), but it is out of
my scope, currently. It would lower performance, but would allow for
using the existing filters.
To be honest, py:match seems to be hopeless. I have ideas to implement
that kind of functionality, but my best bet is that it won't be any
faster that way than running the whole thing through Genshi.
Cython output (decorating the generated code with type declarations)
is also planned, since that could give us another 2-3x performance
gain making it potentially 100-150x faster than Genshi. I know, it is
close to a dream, but might be achieved for the most time critical
parts.
Output modules for other programming languages, like Standard C++,
Java or C# are also possible, making Genshi a more universal template
format. Expressions might not be fully compatible, however. Loop
constructs might need to be translated, etc. But having such support
would make it possible to prototype something in Python, then reuse
the template as is for the final solution on another language.
Please don't keep your ideas in secret. :)
Contributions and donations are also welcome.
Viktor Ferenczi
***@ferenczi.eu
freelance software consultant
http://www.linkedin.com/in/viktorferenczi
http://careers.stackoverflow.com/viktor-ferenczi
I think You are the most interested audience for my latest product, a
working and tested template compiler for Genshi:
http://code.google.com/p/genshi-compiler/
License: MIT
I know that there's prior art in this area like Kajiki. There have
been some discussion and some good suggestions about this topic here
as well. I have been trying to read through myself on them recently.
My story which led me to the development of this package in short:
I had a maintainable, but somewhat complex Genshi template which
rendered a rather complicated HTML document from an equally complex
tree of Python objects. I could not get it nicer, it is all legacy
code. It worked fine for a few object, but started to be insanely slow
for 100-200 such object laid out in a 4-6 level deep tree structure.
It took ~600ms on a pretty fast Core i7 machine just to render the
already loaded template. It was unbearable on a production server.
Yes, it contains recursive template functions, it was the cleanest way
to develop this kind of template.
So I had three choices:
* Rewrite it in pure Python (or Cython) to get better speed, but it
would mean loosing most of the maintainability and the possibility of
generating broken markup. It would also pose a great risk of breaking
functionality, which is quite hard to test, since the generated HTML
is part of a user interface...
* Rewrite the whole thing in a fast text template language, which has
an existing mechanism to compile the template to Python source or byte
code. It would risk the generation of broken markup as well.
* Trying to compile it to pure Python. It could also be used for my
other templates. There are lots of them...
I don't use too much of Genshi's more dynamic features, like py:match.
(I tried to build my template that way in the past, but the rendering
became too slow and it wasn't much more maintainable than before.) I
don't use token filters for other than language translation (Babel),
which could be incorporated otherwise. I'm using xi:include, but it
could be replaced by simply importing another compiled template module
and calling a compiled template function directly.
So I decided to take the red pill and write such a compiler. The first
version completed in ~20 hours, but I wasn't satisfied with the
results. It worked, but the code was somewhat fragile and hard to
extend. So I completely refactored and extended it in another ~40
hours, which led to reasonable results. Now it has good unit test
coverage and hope to receive more features in the near future.
i18n:msg and i18n in general will be implemented soon, since I need
that for most of my other templates.
xi:include might be implemented by introducing a mapping from template
file names (as given in href) and the template modules generated
(fully qualified import names).
token stream support seems to be doable as well (e.g. the compiled
template would generate a compatible token stream), but it is out of
my scope, currently. It would lower performance, but would allow for
using the existing filters.
To be honest, py:match seems to be hopeless. I have ideas to implement
that kind of functionality, but my best bet is that it won't be any
faster that way than running the whole thing through Genshi.
Cython output (decorating the generated code with type declarations)
is also planned, since that could give us another 2-3x performance
gain making it potentially 100-150x faster than Genshi. I know, it is
close to a dream, but might be achieved for the most time critical
parts.
Output modules for other programming languages, like Standard C++,
Java or C# are also possible, making Genshi a more universal template
format. Expressions might not be fully compatible, however. Loop
constructs might need to be translated, etc. But having such support
would make it possible to prototype something in Python, then reuse
the template as is for the final solution on another language.
Please don't keep your ideas in secret. :)
Contributions and donations are also welcome.
Viktor Ferenczi
***@ferenczi.eu
freelance software consultant
http://www.linkedin.com/in/viktorferenczi
http://careers.stackoverflow.com/viktor-ferenczi
--
You received this message because you are subscribed to the Google Groups "Genshi" group.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to genshi+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/genshi?hl=en.
You received this message because you are subscribed to the Google Groups "Genshi" group.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to genshi+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/genshi?hl=en.