Package liqp
Interface RenderTransformer
public interface RenderTransformer
A RenderTransformer handles the conversion of objects to be returned by
Template.renderToObject() etc.
Implementations may optimize how objects are actually appended/serialized, and when exceptions are thrown in the case that the final result would be too long.
The default implementation,RenderTransformerDefaultImpl, simply appends strings to a
StringBuilder. While this will work in many cases, there is a chance that large strings are
created temporarily on the heap, increasing the chance of an out-of-memory situation, and a generally
high allocation activity (spikes in the memory allocation graph).
For example, the following liquid template results in 10012
StringBuilder.toString() conversions:
{% for l in (1..10)%}{% for k in (1..1000) %}
{% for i in (1..1000) %}Hello! {{ i }} world
{% endfor %}{% endfor %}{% endfor %}
With a custom implementation such as stringhold's StringHolderReaderTransformer,
the number of actual `toString` conversions can be as low as 1, and — depending on specific
implementation tricks (such as reusing instances of identical partial sequences) — a significantly
lower memory footprint can be observed (in this case, with stringhold, 7x less usage). If the
data is to be written to a Writer (instead of serializing to a complete string), an even lower
memory footprint can be achieved (35x less usage was observed).
- Author:
- Christian Kohlschütter
- See Also:
-
RenderTransformerDefaultImpl- stringhold
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceSomething that can append objects. -
Method Summary
Modifier and TypeMethodDescriptionnewObjectAppender(TemplateContext context, int estimatedNumberOfAppends) Creates a newRenderTransformer.ObjectAppender.Controllerfor the givenTemplateContext, optionally using the given estimate for the number of calls toRenderTransformer.ObjectAppender.append(Object).transformObject(TemplateContext context, Object obj) Transforms an object to a representation that is suitable to callObject.toString()on during the "render"-phase (Template.render()etc.).
-
Method Details
-
newObjectAppender
RenderTransformer.ObjectAppender.Controller newObjectAppender(TemplateContext context, int estimatedNumberOfAppends) Creates a newRenderTransformer.ObjectAppender.Controllerfor the givenTemplateContext, optionally using the given estimate for the number of calls toRenderTransformer.ObjectAppender.append(Object).- Parameters:
context- The template context.estimatedNumberOfAppends- The estimate number of calls toRenderTransformer.ObjectAppender.append(Object).- Returns:
- The appender controller instance.
-
transformObject
Transforms an object to a representation that is suitable to callObject.toString()on during the "render"-phase (Template.render()etc.).- Parameters:
context- The template context.obj- The object to transform.- Returns:
- The transformed object, or the object itself if no transformation is necessary.
-