|
| 1 | +# Liquid Compiled Template Optimization Log |
| 2 | + |
| 3 | +This document tracks optimizations made to the compiled Liquid template engine. |
| 4 | +Each entry shows before/after code and measured impact. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## Baseline Measurement |
| 9 | + |
| 10 | +**Date:** 2024-12-31 |
| 11 | +**Commit:** (pending profiler implementation) |
| 12 | + |
| 13 | +### Current State |
| 14 | + |
| 15 | +The compiled template engine generates Ruby code from Liquid templates. |
| 16 | +Before optimizations, here's a sample of generated code for a simple loop: |
| 17 | + |
| 18 | +```ruby |
| 19 | +# Template: {% for product in products %}{{ forloop.index }}: {{ product.name }}{% endfor %} |
| 20 | + |
| 21 | +->(assigns, __context__, __external__) do |
| 22 | + __output__ = +"" |
| 23 | + |
| 24 | + __coll1__ = assigns["products"] |
| 25 | + __coll1__ = __coll1__.to_a if __coll1__.is_a?(Range) |
| 26 | + __len3__ = __coll1__.respond_to?(:length) ? __coll1__.length : 0 |
| 27 | + __idx2__ = 0 |
| 28 | + catch(:__loop__break__) do |
| 29 | + (__coll1__.respond_to?(:each) ? __coll1__ : []).each do |__item__| |
| 30 | + catch(:__loop__continue__) do |
| 31 | + assigns["product"] = __item__ |
| 32 | + assigns['forloop'] = { |
| 33 | + 'name' => "product-products", |
| 34 | + 'length' => __len3__, |
| 35 | + 'index' => __idx2__ + 1, |
| 36 | + 'index0' => __idx2__, |
| 37 | + 'rindex' => __len3__ - __idx2__, |
| 38 | + 'rindex0' => __len3__ - __idx2__ - 1, |
| 39 | + 'first' => __idx2__ == 0, |
| 40 | + 'last' => __idx2__ == __len3__ - 1, |
| 41 | + } |
| 42 | + __output__ << LR.output(LR.lookup(assigns["forloop"], "index", __context__)) |
| 43 | + __output__ << ": " |
| 44 | + __output__ << LR.output(LR.lookup(assigns["product"], "name", __context__)) |
| 45 | + end |
| 46 | + __idx2__ += 1 |
| 47 | + end |
| 48 | + end |
| 49 | + assigns.delete("product") |
| 50 | + assigns.delete('forloop') |
| 51 | + |
| 52 | + __output__ |
| 53 | +end |
| 54 | +``` |
| 55 | + |
| 56 | +### Issues Identified |
| 57 | + |
| 58 | +1. **catch/throw overhead** - Used even when no break/continue in loop |
| 59 | +2. **Hash allocation per iteration** - 8 key/value pairs computed every time |
| 60 | +3. **respond_to? checks** - Redundant after type is known |
| 61 | +4. **LR.lookup for forloop** - Unnecessary indirection for known hash |
| 62 | +5. **String literals not frozen** - Allocates on each render |
| 63 | +6. **Output buffer grows dynamically** - No pre-allocation |
| 64 | + |
| 65 | +--- |
| 66 | + |
| 67 | +## Optimization Log |
| 68 | + |
| 69 | +<!-- Entries will be added here as optimizations are implemented --> |
| 70 | + |
0 commit comments