After the Refactor: A Path to Faster Rendering with Liquid-C

Not every feature of open-source template language Liquid has been ported over to Liquid-C, such as the If tag and For Tag. I started to look into how to compile Liquid’s If tag from Liquid-C, a process that would improve parsing time.

Michael Go
5 min readbeginner
--
View Original

Overview

The article discusses the refactoring of Liquid-C, an extension of Liquid designed to improve rendering performance by compiling Liquid templates into bytecode for a virtual machine. It highlights the changes made to the instruction structure, the benefits and drawbacks of the refactor, and the future potential for implementing additional features like If and For tags.

What You'll Learn

1

How to compile Liquid templates into Liquid-C VM bytecode

2

Why removing the constant pointer can improve Liquid-C's rendering capabilities

3

When to consider refactoring for performance improvements in Liquid-C

Prerequisites & Requirements

  • Understanding of Liquid and Liquid-C concepts
  • Familiarity with virtual machines and bytecode compilation(optional)

Key Questions Answered

What is Liquid-C and how does it improve Liquid's performance?
Liquid-C is an extension of Liquid that enhances parsing performance by compiling Liquid templates into bytecode for a virtual machine. It was introduced to accelerate rendering by evaluating Liquid AST nodes and has since evolved to include a VM for compilation.
What changes were made to Liquid-C's instruction structure?
The refactor removed the constant pointer from Liquid-C's instruction structure, allowing for more efficient instruction pointer movement. Instead of using a constant pointer, the new structure stores the index of constants in a hash table, reducing the size of the constants array.
What are the benefits and drawbacks of the Liquid-C refactor?
The refactor reduced the constants array size from 157 to 104 objects, which helps in optimizing memory usage. However, it has not significantly improved rendering speed and has slightly increased memory usage and parsing time.
What future improvements are planned for Liquid-C?
Future improvements for Liquid-C include the introduction of new operations like GOTO and JMP, which will facilitate the implementation of If and For tags, ultimately enhancing rendering speed.

Key Statistics & Figures

Size of constants array before refactor
157 objects
This was the size of the constants array for the performance/tests/dropify/cart.liquid template.
Size of constants array after refactor
104 objects
This reduction in size indicates improved memory efficiency post-refactor.

Technologies & Tools

Template Engine
Liquid
Used for rendering templates in Shopify.
Extension
Liquid-c
Enhances Liquid's performance by compiling templates into bytecode.

Key Actionable Insights

1
Consider refactoring your Liquid-C implementation to remove the constant pointer for better performance.
This change can lead to more efficient instruction handling and is a necessary step for implementing additional features like If and For tags.
2
Monitor the memory usage and parsing times after refactoring to assess the impact of changes.
While the refactor aims to improve performance, it's essential to evaluate whether the benefits outweigh the increased memory usage and parsing times.
3
Engage with the Liquid-C community to share insights and improvements.
As Liquid-C is open source, collaboration can lead to faster advancements and optimizations that benefit all users.

Common Pitfalls

1
Assuming that refactoring will always lead to performance improvements.
The article highlights that despite the refactor, rendering speed did not drastically improve, which emphasizes the need for careful evaluation of changes.

Related Concepts

Liquid Template Engine
Virtual Machines
Bytecode Compilation