Shopify / Shopify/liquid

Serializing a compiled Template using Marshal.dump fails after version 5.6.0

Open
#1,913 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Report
Dominant language
Ruby
Stars
11.9k
Forks
1.5k
Avg merge
17h 55m
Merged PRs (30d)
3

Description

Hello there,

Previous to release 5.6.0, up to 5.5.1, this code was possible (tested using ruby >= 3.2):

ct = Liquid::Template.parse(some_valid_liquid_string)

Marshal.dump(ct)  # this can be saved to memcache or disk

but with version >= 5.6.0, it fails with the error:

Marshal.dump: can't dump anonymous class #<Class:0x0000000153633cc0>  (TypeError)

By my investigation, this comes from the attribute @strainer_template which is now instantiated as an anonymous class, as part of the Environment instance.

I saw #760 conversation about the ability to cache a compiled template and as I understand it wasn't a main focus, it was something done in/before 2016 during hack days, but it worked up until 5.6.0, and it is als mentioned in the wiki as a strategy: https://github.com/Shopify/liquid/wiki/Liquid-for-Programmers#first-steps

The parse step creates a fully compiled template which can be re-used as often as you like. You can store it in memory or in a cache for faster rendering later.

Was this change intentional? Or is there any other way to cache (serialize) the compiled template?

Thanks!

PS: I did not do any elaborate benchmarks (yet) about compiling on each request vs fetching from cache, but IMO the way I see it is about moving work from IO (memcache) to CPU (re-compiling the template) on each request. If I could choose, I'd like the IO work better :-)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Liquid::Template.parse and the Environment instance, then inspect how @strainer_template is instantiated in version 5.6.0. Reproduce the Ruby Marshal.dump failure with a compiled template and compare it with 5.5.1. Done means establishing whether compiled templates can still be serialized or documenting the supported caching alternative.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.