liquid5-c 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +22 -0
  3. data/README.md +164 -0
  4. data/ext/liquid_c/block.c +970 -0
  5. data/ext/liquid_c/block.h +37 -0
  6. data/ext/liquid_c/c_buffer.c +48 -0
  7. data/ext/liquid_c/c_buffer.h +82 -0
  8. data/ext/liquid_c/context.c +262 -0
  9. data/ext/liquid_c/context.h +72 -0
  10. data/ext/liquid_c/document_body.c +181 -0
  11. data/ext/liquid_c/document_body.h +84 -0
  12. data/ext/liquid_c/expression.c +130 -0
  13. data/ext/liquid_c/expression.h +24 -0
  14. data/ext/liquid_c/extconf.rb +60 -0
  15. data/ext/liquid_c/intutil.h +36 -0
  16. data/ext/liquid_c/lexer.c +172 -0
  17. data/ext/liquid_c/lexer.h +61 -0
  18. data/ext/liquid_c/liquid.c +182 -0
  19. data/ext/liquid_c/liquid.h +112 -0
  20. data/ext/liquid_c/liquid_vm.c +907 -0
  21. data/ext/liquid_c/liquid_vm.h +35 -0
  22. data/ext/liquid_c/mri_compat.h +35 -0
  23. data/ext/liquid_c/native_parser.c +2360 -0
  24. data/ext/liquid_c/native_value.c +49 -0
  25. data/ext/liquid_c/parse_context.c +84 -0
  26. data/ext/liquid_c/parse_context.h +13 -0
  27. data/ext/liquid_c/parser.c +285 -0
  28. data/ext/liquid_c/parser.h +25 -0
  29. data/ext/liquid_c/raw.c +136 -0
  30. data/ext/liquid_c/raw.h +6 -0
  31. data/ext/liquid_c/resource_limits.c +459 -0
  32. data/ext/liquid_c/resource_limits.h +28 -0
  33. data/ext/liquid_c/stringutil.h +37 -0
  34. data/ext/liquid_c/tokenizer.c +326 -0
  35. data/ext/liquid_c/tokenizer.h +46 -0
  36. data/ext/liquid_c/usage.c +18 -0
  37. data/ext/liquid_c/usage.h +9 -0
  38. data/ext/liquid_c/variable.c +198 -0
  39. data/ext/liquid_c/variable.h +61 -0
  40. data/ext/liquid_c/variable_lookup.c +73 -0
  41. data/ext/liquid_c/variable_lookup.h +8 -0
  42. data/ext/liquid_c/vm_assembler.c +631 -0
  43. data/ext/liquid_c/vm_assembler.h +294 -0
  44. data/ext/liquid_c/vm_assembler_pool.c +117 -0
  45. data/ext/liquid_c/vm_assembler_pool.h +27 -0
  46. data/lib/liquid/c/compile_ext.rb +47 -0
  47. data/lib/liquid/c/native_compiler.rb +262 -0
  48. data/lib/liquid/c/native_parser.rb +261 -0
  49. data/lib/liquid/c/native_tags.rb +103 -0
  50. data/lib/liquid/c/native_values.rb +9 -0
  51. data/lib/liquid/c/version.rb +7 -0
  52. data/lib/liquid/c.rb +379 -0
  53. metadata +140 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a69a347ba8a222052fceb99748443908d8fa8c942f6aa7f8ca0c96644ef6695e
4
+ data.tar.gz: c8b2ac1efa7a98d4784d6f162f31a2308459cd0b8480f5ca482e43db156bcc6e
5
+ SHA512:
6
+ metadata.gz: fbfc706a5447a127c06da77bc47031b7c81622dc286b37bebf4c1de33a9c77e157013ba56633e44110af6447ddca6b3314b8b19569f15b685fe8c6c11cdedf60
7
+ data.tar.gz: cc0762143aac4603f77085a159446bd79f53589f8a256d6441baa345b20323dc7011c0fd9abf1c44517ac1353f22fd670f77b88a864e49664ac243bc8a9939c9
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Shopify
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,164 @@
1
+ # liquid5-c
2
+
3
+ This fork of Shopify's liquid-c accelerates parsing and rendering with the Liquid 5.14.0 Ruby API.
4
+ The native compiler depends on the internal layout of this Liquid release.
5
+
6
+ ## Installation
7
+
8
+ The gem builds a native extension and requires Ruby 3.0 or later, Ruby headers,
9
+ a C compiler and make. After the first release, add this line to your Gemfile.
10
+ Bundler installs Liquid 5.14.0 as a dependency.
11
+
12
+ ```ruby
13
+ gem "liquid5-c", "~> 5.0", require: "liquid/c"
14
+ ```
15
+
16
+ To install from the repository, use
17
+ `gem "liquid5-c", github: "VladimirIvanin/liquid-c", require: "liquid/c"`.
18
+ When switching from `liquid-c`, replace its Gemfile entry. Both packages
19
+ provide the same `liquid/c` entry point and native extension.
20
+
21
+ Run `bundle install` and load the extension before parsing templates.
22
+
23
+ ```ruby
24
+ require "liquid/c"
25
+
26
+ template = Liquid::Template.parse("Hello, {{ customer.name }}")
27
+ template.render!("customer" => { "name" => "Ada" }) # => "Hello, Ada"
28
+ ```
29
+
30
+ ## Publishing
31
+
32
+ Before the first release, sign in to RubyGems.org with the account that will
33
+ own the gem and create a [pending trusted publisher](https://rubygems.org/profile/oidc/pending_trusted_publishers).
34
+ Enter the following values.
35
+
36
+ | Field | Value |
37
+ | --- | --- |
38
+ | Gem name | `liquid5-c` |
39
+ | GitHub owner | `VladimirIvanin` |
40
+ | Repository | `liquid-c` |
41
+ | Workflow filename | `release.yml` |
42
+ | Environment | `release` |
43
+
44
+ Copy the numeric account ID shown on that page into the GitHub Actions
45
+ repository variable `RUBYGEMS_OWNER_ID` under Settings → Secrets and variables
46
+ → Actions → Variables. The workflow uses OIDC for authentication.
47
+
48
+ For each release, update `Liquid::C::VERSION` in `lib/liquid/c/version.rb`,
49
+ run `bundle lock` and merge the changes into `main`. Once CI passes, tag
50
+ the tested commit and push that tag. For the first release, run the following
51
+ from the release commit.
52
+
53
+ ```sh
54
+ git tag -a v5.0.0 -m "Release liquid5-c 5.0.0"
55
+ git push origin v5.0.0
56
+ ```
57
+
58
+ The Publish gem workflow checks the tag against the version, runs the unit
59
+ and compatibility suites, then builds and installs the gem in isolation.
60
+ After a successful native render it pushes that same package to RubyGems.org.
61
+
62
+ ## Compatibility
63
+
64
+ The C tokenizer takes UTF-8 input. Templates larger than 16,777,215 bytes
65
+ after UTF-8 conversion use the Ruby parser for that document. Custom tags
66
+ receive a tokenizer with a `shift` method. Profiling and the
67
+ `disable_liquid_c_nodes: true` parse option select Ruby nodes.
68
+ Set `Liquid::C.enabled` at application boot when choosing an engine for the
69
+ whole process.
70
+
71
+ The guarded native compiler for built-in tags requires MRI 3.2 and compatible
72
+ Liquid methods. A registered subclass of `Liquid::Include` keeps its own parse
73
+ and render behavior. Other Ruby versions can still use the C tokenizer, block
74
+ body and rendering VM. To inspect variable parser decisions for a template,
75
+ pass a hash as the `native_parser_stats:` parse option. The parser populates
76
+ it with counts such as `accepted` and `guard_cost`. Applications that require
77
+ the guarded parser can check `Liquid::C::NativeParser::NATIVE_GUARDS` at boot.
78
+
79
+ Whitespace trimming follows Liquid 5.14.0 by default. Templates that depend on
80
+ historical liquid-c whitespace trimming can opt in during parsing with
81
+ `bug_compatible_whitespace_trimming: true`.
82
+
83
+ ## Performance
84
+
85
+ The repository includes deterministic storefront workloads and expected output.
86
+ The example below compares plain Liquid 5.14.0 with this extension on the same
87
+ Ruby, with YJIT disabled. It uses the nested-include and filter-chain fixtures.
88
+
89
+ ```sh
90
+ mkdir -p tmp/bench
91
+ DEBUG=false bundle exec rake clean compile
92
+ BENCH_ENGINE=pure bundle exec ruby --disable-yjit -Ilib performance/measure.rb nested_include/small filter_chain/small > tmp/bench/pure.jsonl
93
+ bundle exec ruby --disable-yjit -Ilib performance/measure.rb nested_include/small filter_chain/small > tmp/bench/current.jsonl
94
+ bundle exec ruby performance/compare.rb tmp/bench/pure.jsonl tmp/bench/current.jsonl
95
+ ```
96
+
97
+ Each row reports the median time per operation over five samples. Parse builds
98
+ the root template. Render reuses a parsed root with a fresh partial cache.
99
+ Parse-and-render includes both operations. Speedup is the plain Ruby median
100
+ divided by the extension median. Values above one indicate a faster extension.
101
+ The runner checks each fixture's output.
102
+
103
+ On MRI 3.3.13 (x86-64 Linux, YJIT off), the two render fixtures gave the
104
+ following allocation counts, rounded to the nearest object per render. This Ruby
105
+ uses the C rendering VM with the MRI 3.2 guarded compiler disabled.
106
+
107
+ | Fixture | Plain Liquid | Liquid C |
108
+ | --- | ---: | ---: |
109
+ | `filter_chain/small` | 870 | 510 |
110
+ | `nested_include/small` | 800 | 855 |
111
+
112
+ The benchmark guide also covers
113
+ larger scenarios, Hash and Drop inputs, and cold and warm partial caches.
114
+ [Benchmark guide](performance/README.md)
115
+
116
+ ## Developing
117
+
118
+ Install the tools pinned in `mise.toml`, Ruby build prerequisites, a C compiler,
119
+ make and Valgrind (Linux). The development bundle uses Ruby 3.3.13.
120
+
121
+ ```sh
122
+ mise trust
123
+ mise install
124
+ mise run setup
125
+ mise run check
126
+ ```
127
+
128
+ Run `mise run test:insales` to check the current working tree on MRI 3.2.11,
129
+ the InSales production version. The task uses rsync to create an isolated build
130
+ under `tmp/`, verifies guarded native parsing is available, and runs the unit
131
+ tests and compatibility matrix. A failed run keeps its build and logs for
132
+ inspection. The default `mise run test` uses MRI 3.3.13.
133
+
134
+ On Linux, `bundle exec rake test:compatibility:sanitizers` builds the extension
135
+ with ASan and UBSan. For this check, build MRI with
136
+ `--with-setjmp-type=_setjmp` so ASan can track stack unwinding through Ruby
137
+ exceptions. The GC stress phase of the sanitizer runner also requires an
138
+ ASan-instrumented Ruby.
139
+
140
+ ### Extension compatibility tests
141
+
142
+ `bundle exec rake test:compatibility:extensions` checks monkey patches, custom
143
+ tags and blocks, filter overrides and reloading, decimal conversion, dynamic
144
+ drops, layered registers, error recovery, and overlapping renders in two threads.
145
+ The fixtures use synthetic data and require neither Rails nor an external app.
146
+ Collection scenarios include parsing string conditions in `where_exp` and
147
+ `find_exp`, nested filters inside `group_by_exp`, short circuit evaluation,
148
+ and restoring bindings after errors. Mutable mapping drops, cached nested
149
+ fragments, and clearing template assigns between payloads are also covered.
150
+ Language scenarios exercise patched expression parsing, runtime expression
151
+ objects, custom condition operators, component regions rendered through a
152
+ runtime-created `render` tag, and parent block rendering from a drop.
153
+
154
+ The suite is included in `bundle exec rake test`. It runs on pure Ruby and the
155
+ `c`, `no_vm`, and `disabled` configurations in all four parser modes. Each parsed
156
+ template checks its body class; patches installed before loading the extension
157
+ are tested in a separate process with a timeout.
158
+
159
+ ```sh
160
+ ENGINES=pure,c MODES=warn,strict bundle exec ruby test/compatibility/run.rb extensions
161
+ TEST_SEED=90210 bundle exec rake test:compatibility:extensions
162
+ ```
163
+
164
+ See the [compatibility corpus guide](test/fixtures/liquid_spec/README.md) for the pinned fixtures and reproduction commands.