diff --git a/fluid-bench/benchmark.rb b/fluid-bench/benchmark.rb new file mode 100644 index 00000000..ec682b89 --- /dev/null +++ b/fluid-bench/benchmark.rb @@ -0,0 +1,34 @@ +# https://github.com/evanphx/benchmark-ips +require 'liquid' +#require 'liquid/c' +require 'benchmark/ips' + +require_relative '../lib/liquid/compile' + +require_relative 'shop_filter' +require_relative 'money_filter' + +# Each database table is a hash +require_relative 'database' +tables = Database.tables + +Liquid::Template.register_filter(MoneyFilter) +Liquid::Template.register_filter(ShopFilter) +@template = Liquid::Template.parse(File.read("product.liquid")) + +context = Liquid::Context.new([tables, {}], {}, {}, false, Liquid::ResourceLimits.new(Liquid::Template.default_resource_limits)) + +require 'stackprof' + +results = StackProf.run(raw: true, out: 'stackprof.dump') do + 500_000.times do + @template.render(context) + end +end + +Benchmark.ips do |x| + x.report("render") { @template.render(context) } + + # Compare the iterations per second of the various reports + x.compare! +end diff --git a/fluid-bench/database.rb b/fluid-bench/database.rb new file mode 100644 index 00000000..f8f7eb15 --- /dev/null +++ b/fluid-bench/database.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +require 'yaml' +require 'stackprof' + +module Database + DATABASE_FILE_PATH = "#{__dir__}/vision.database.yml" + + # Load the standard vision toolkit database and re-arrage it to be simply exportable + # to liquid as assigns. All this is based on Shopify + def self.tables + @tables ||= begin + db = + if YAML.respond_to?(:unsafe_load_file) # Only Psych 4+ can use unsafe_load_file + # unsafe_load_file is needed for YAML references + YAML.unsafe_load_file(DATABASE_FILE_PATH, symbolize_names: true) + else + YAML.load_file(DATABASE_FILE_PATH, symbolize_names: true) + end + + # From vision source + db[:products].each do |product| + collections = db[:collections].find_all do |collection| + collection[:products].any? { |p| p[:id].to_i == product[:id].to_i } + end + product[:collections] = collections + end + + # key the tables by handles, as this is how liquid expects it. + db = db.each_with_object({}) do |(key, values), assigns| + assigns[key] = values.each_with_object({}) do |v, h| + h[v[:handle]] = v + end + end + + # Some standard direct accessors so that the specialized templates + # render correctly + db[:collection] = db[:collections].values.first + db[:product] = db[:products].values.first + db[:blog] = db[:blogs].values.first + db[:article] = db[:blog][:articles].first + + db[:cart] = { + :total_price => db[:line_items].values.inject(0) { |sum, item| sum + item[:line_price] * item[:quantity] }, + :item_count => db[:line_items].values.inject(0) { |sum, item| sum + item[:quantity] }, + :items => db[:line_items].values, + } + + db + end + end +end + +if __FILE__ == $PROGRAM_NAME + p(Database.tables[:collections][:frontpage].keys) + # p Database.tables[:blog][:articles] +end diff --git a/fluid-bench/money_filter.rb b/fluid-bench/money_filter.rb new file mode 100644 index 00000000..b0135e3e --- /dev/null +++ b/fluid-bench/money_filter.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module MoneyFilter + def money_with_currency(money) + return '' if money.nil? + format("$ %.2f USD", money / 100.0) + end + + def money(money) + return '' if money.nil? + format("$ %.2f", money / 100.0) + end + + private + + def currency + ShopDrop.new.currency + end +end diff --git a/fluid-bench/product.liquid b/fluid-bench/product.liquid new file mode 100644 index 00000000..f0d24f2f --- /dev/null +++ b/fluid-bench/product.liquid @@ -0,0 +1,36 @@ +
+ {% for image in product.images %}{% if forloop.first %}
+ {{ product.title | escape }} +
{% else %} +
+ {{ product.title | escape }} +
{% endif %}{% endfor %} +
+
+

{{ product.title }}

+ {{ product.description }} + + {% if product.available %} +
+ +
+
+ + +
+ + +
+ {% else %} +

This product is temporarily unavailable

+ {% endif %} + +
+ Continue Shopping
+ Browse more {{ product.type | link_to_type }} or additional {{ product.vendor | link_to_vendor }} products. +
+
diff --git a/fluid-bench/shop_filter.rb b/fluid-bench/shop_filter.rb new file mode 100644 index 00000000..3558c6fc --- /dev/null +++ b/fluid-bench/shop_filter.rb @@ -0,0 +1,106 @@ +# frozen_string_literal: true + +module ShopFilter + def asset_url(input) + "/files/1/[shop_id]/[shop_id]/assets/#{input}" + end + + def global_asset_url(input) + "/global/#{input}" + end + + def shopify_asset_url(input) + "/shopify/#{input}" + end + + def script_tag(url) + %() + end + + def stylesheet_tag(url, media = "all") + %() + end + + def link_to(link, url, title = "") + %(#{link}) + end + + def img_tag(url, alt = "") + %(#{alt}) + end + + def link_to_vendor(vendor) + if vendor + link_to(vendor, url_for_vendor(vendor), vendor) + else + 'Unknown Vendor' + end + end + + def link_to_type(type) + if type + link_to(type, url_for_type(type), type) + else + 'Unknown Vendor' + end + end + + def url_for_vendor(vendor_title) + "/collections/#{to_handle(vendor_title)}" + end + + def url_for_type(type_title) + "/collections/#{to_handle(type_title)}" + end + + def product_img_url(url, style = 'small') + unless url =~ %r{\Aproducts/([\w\-\_]+)\.(\w{2,4})} + raise ArgumentError, 'filter "size" can only be called on product images' + end + + case style + when 'original' + '/files/shops/random_number/' + url + when 'grande', 'large', 'medium', 'compact', 'small', 'thumb', 'icon' + "/files/shops/random_number/products/#{Regexp.last_match(1)}_#{style}.#{Regexp.last_match(2)}" + else + raise ArgumentError, 'valid parameters for filter "size" are: original, grande, large, medium, compact, small, thumb and icon ' + end + end + + def default_pagination(paginate) + html = [] + html << %(#{link_to(paginate['previous']['title'], paginate['previous']['url'])}) if paginate['previous'] + + paginate['parts'].each do |part| + html << if part['is_link'] + %(#{link_to(part['title'], part['url'])}) + elsif part['title'].to_i == paginate['current_page'].to_i + %(#{part['title']}) + else + %(#{part['title']}) + end + end + + html << %(#{link_to(paginate['next']['title'], paginate['next']['url'])}) if paginate['next'] + html.join(' ') + end + + # Accepts a number, and two words - one for singular, one for plural + # Returns the singular word if input equals 1, otherwise plural + def pluralize(input, singular, plural) + input == 1 ? singular : plural + end + + private + + def to_handle(str) + result = str.dup + result.downcase! + result.delete!("'\"()[]") + result.gsub!(/\W+/, '-') + result.gsub!(/-+\z/, '') if result[-1] == '-' + result.gsub!(/\A-+/, '') if result[0] == '-' + result + end +end diff --git a/fluid-bench/stackprof.dump b/fluid-bench/stackprof.dump new file mode 100644 index 00000000..65a94e1f Binary files /dev/null and b/fluid-bench/stackprof.dump differ diff --git a/fluid-bench/vision.database.yml b/fluid-bench/vision.database.yml new file mode 100644 index 00000000..9f38c304 --- /dev/null +++ b/fluid-bench/vision.database.yml @@ -0,0 +1,945 @@ +# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +# Variants +# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + +product_variants: + - &product-1-var-1 + id: 1 + title: 151cm / Normal + price: 19900 + weight: 1000 + compare_at_price: 49900 + available: true + inventory_quantity: 5 + option1: 151cm + option2: Normal + option3: + - &product-1-var-2 + id: 2 + title: 155cm / Normal + price: 31900 + weight: 1000 + compare_at_price: 50900 + available: true + inventory_quantity: 2 + option1: 155cm + option2: Normal + option3: + - &product-2-var-1 + id: 3 + title: 162cm + price: 29900 + weight: 1000 + compare_at_price: 52900 + available: true + inventory_quantity: 3 + option1: 162cm + option2: + option3: + - &product-3-var-1 + id: 4 + title: 159cm + price: 19900 + weight: 1000 + compare_at_price: + available: true + inventory_quantity: 4 + option1: 159cm + option2: + option3: + - &product-4-var-1 + id: 5 + title: 159cm + price: 19900 + weight: 1000 + compare_at_price: 32900 + available: true + inventory_quantity: 6 + option1: 159cm + option2: + option3: + - &product-1-var-3 + id: 6 + title: 158cm / Wide + price: 23900 + weight: 1000 + compare_at_price: 99900 + available: false + inventory_quantity: 0 + option1: 158cm + option2: Wide + option3: + - &product-3-var-2 + id: 7 + title: 162cm + price: 19900 + weight: 1000 + compare_at_price: + available: false + inventory_quantity: 0 + option1: 162cm + option2: + option3: + - &product-3-var-3 + id: 8 + title: 165cm + price: 22900 + weight: 1000 + compare_at_price: + available: true + inventory_quantity: 4 + option1: 165cm + option2: + option3: + - &product-5-var-1 + id: 9 + title: black / 42 + price: 11900 + weight: 500 + compare_at_price: 22900 + available: true + inventory_quantity: 1 + option1: black + option2: 42 + option3: + - &product-5-var-2 + id: 10 + title: beige / 42 + price: 11900 + weight: 500 + compare_at_price: 22900 + available: true + inventory_quantity: 3 + option1: beige + option2: 42 + option3: + - &product-5-var-3 + id: 11 + title: white / 42 + price: 13900 + weight: 500 + compare_at_price: 24900 + available: true + inventory_quantity: 1 + option1: white + option2: 42 + option3: + - &product-5-var-4 + id: 12 + title: black / 44 + price: 11900 + weight: 500 + compare_at_price: 22900 + available: true + inventory_quantity: 2 + option1: black + option2: 44 + option3: + - &product-5-var-5 + id: 13 + title: beige / 44 + price: 11900 + weight: 500 + compare_at_price: 22900 + available: false + inventory_quantity: 0 + option1: beige + option2: 44 + option3: + - &product-5-var-6 + id: 14 + title: white / 44 + price: 13900 + weight: 500 + compare_at_price: 24900 + available: false + inventory_quantity: 0 + option1: white + option2: 44 + option3: + - &product-6-var-1 + id: 15 + title: red + price: 2179500 + weight: 200000 + compare_at_price: + available: true + inventory_quantity: 0 + option1: red + option2: + option3: + - &product-7-var-1 + id: 16 + title: black / small + price: 1900 + weight: 200 + compare_at_price: + available: true + inventory_quantity: 20 + option1: black + option2: small + option3: + - &product-7-var-2 + id: 17 + title: black / medium + price: 1900 + weight: 200 + compare_at_price: + available: false + inventory_quantity: 0 + option1: black + option2: medium + option3: + - &product-7-var-3 + id: 18 + title: black / large + price: 1900 + weight: 200 + compare_at_price: + available: true + inventory_quantity: 10 + option1: black + option2: large + option3: + - &product-7-var-4 + id: 19 + title: black / extra large + price: 1900 + weight: 200 + compare_at_price: + available: false + inventory_quantity: 0 + option1: black + option2: extra large + option3: + - &product-8-var-1 + id: 20 + title: brown / small + price: 5900 + weight: 400 + compare_at_price: 6900 + available: true + inventory_quantity: 5 + option1: brown + option2: small + option3: + - &product-8-var-2 + id: 21 + title: brown / medium + price: 5900 + weight: 400 + compare_at_price: 6900 + available: false + inventory_quantity: 0 + option1: brown + option2: medium + option3: + - &product-8-var-3 + id: 22 + title: brown / large + price: 5900 + weight: 400 + compare_at_price: 6900 + available: true + inventory_quantity: 10 + option1: brown + option2: large + option3: + - &product-8-var-4 + id: 23 + title: black / small + price: 5900 + weight: 400 + compare_at_price: 6900 + available: true + inventory_quantity: 10 + option1: black + option2: small + option3: + - &product-8-var-5 + id: 24 + title: black / medium + price: 5900 + weight: 400 + compare_at_price: 6900 + available: true + inventory_quantity: 10 + option1: black + option2: medium + option3: + - &product-8-var-6 + id: 25 + title: black / large + price: 5900 + weight: 400 + compare_at_price: 6900 + available: false + inventory_quantity: 0 + option1: black + option2: large + option3: + - &product-9-var-1 + id: 26 + title: Body Only + price: 499995 + weight: 2000 + compare_at_price: + available: true + inventory_quantity: 3 + option1: Body Only + option2: + option3: + - &product-9-var-2 + id: 27 + title: Kit with 18-55mm VR lens + price: 523995 + weight: 2000 + compare_at_price: + available: true + inventory_quantity: 2 + option1: Kit with 18-55mm VR lens + option2: + option3: + - &product-9-var-3 + id: 28 + title: Kit with 18-200 VR lens + price: 552500 + weight: 2000 + compare_at_price: + available: true + inventory_quantity: 3 + option1: Kit with 18-200 VR lens + option2: + option3: + +# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +# Products +# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + +products: + - &product-1 + id: 1 + title: Arbor Draft + handle: arbor-draft + type: Snowboards + vendor: Arbor + price: 23900 + price_max: 31900 + price_min: 23900 + price_varies: true + available: true + tags: + - season2005 + - pro + - intermediate + - wooden + - freestyle + options: + - Length + - Style + compare_at_price: 49900 + compare_at_price_max: 50900 + compare_at_price_min: 49900 + compare_at_price_varies: true + url: /products/arbor-draft + featured_image: products/arbor_draft.jpg + images: + - products/arbor_draft.jpg + description: + The Arbor Draft snowboard wouldn't exist if Polynesians hadn't figured out how to surf hundreds of years ago. But the Draft does exist, and it's here to bring your urban and park riding to a new level. The board's freaky Tiki design pays homage to culture that inspired snowboarding. It's designed to spin with ease, land smoothly, lock hook-free onto rails, and take the abuse of a pavement pounding or twelve. The Draft will pop off kickers with authority and carve solidly across the pipe. The Draft features targeted Koa wood die cuts inlayed into the deck that enhance the flex pattern. Now bow down to riding's ancestors. + variants: + - *product-1-var-1 + - *product-1-var-2 + - *product-1-var-3 + - &product-2 + id: 2 + title: Arbor Element + handle: arbor-element + type: Snowboards + vendor: Arbor + price: 29900 + price_max: 29900 + price_min: 29900 + price_varies: false + available: true + tags: + - season2005 + - pro + - wooden + - freestyle + options: + - Length + compare_at_price: 52900 + compare_at_price_max: 52900 + compare_at_price_min: 52900 + compare_at_price_varies: false + url: /products/arbor-element + featured_image: products/element58.jpg + images: + - products/element58.jpg + description: + The Element is a technically advanced all-mountain board for riders who readily transition from one terrain, snow condition, or riding style to another. Its balanced design provides the versatility needed for the true ride-it-all experience. The Element is exceedingly lively, freely initiates, and holds a tight edge at speed. Its structural real-wood topsheet is made with book-matched Koa. + variants: + - *product-2-var-1 + + - &product-3 + id: 3 + title: Comic ~ Pastel + handle: comic-pastel + type: Snowboards + vendor: Technine + price: 19900 + price_max: 22900 + price_min: 19900 + tags: + - season2006 + - beginner + - intermediate + - freestyle + - purple + options: + - Length + price_varies: true + available: true + compare_at_price: + compare_at_price_max: 0 + compare_at_price_min: 0 + compare_at_price_varies: false + url: /products/comic-pastel + featured_image: products/technine1.jpg + images: + - products/technine1.jpg + - products/technine2.jpg + - products/technine_detail.jpg + description: + 2005 Technine Comic Series Description The Comic series was developed to be the ultimate progressive freestyle board in the Technine line. Dependable edge control and a perfect flex pattern for jumping in the park or out of bounds. Landins and progression will come easy with this board and it will help your riding progress to the next level. Street rails, park jibs, backcountry booters and park jumps, this board will do it all. + variants: + - *product-3-var-1 + - *product-3-var-2 + - *product-3-var-3 + + - &product-4 + id: 4 + title: Comic ~ Orange + handle: comic-orange + type: Snowboards + vendor: Technine + price: 19900 + price_max: 19900 + price_min: 19900 + price_varies: false + available: true + tags: + - season2006 + - beginner + - intermediate + - freestyle + - orange + options: + - Length + compare_at_price: 32900 + compare_at_price_max: 32900 + compare_at_price_min: 32900 + compare_at_price_varies: false + url: /products/comic-orange + featured_image: products/technine3.jpg + images: + - products/technine3.jpg + - products/technine4.jpg + description: + 2005 Technine Comic Series Description The Comic series was developed to be the ultimate progressive freestyle board in the Technine line. Dependable edge control and a perfect flex pattern for jumping in the park or out of bounds. Landins and progression will come easy with this board and it will help your riding progress to the next level. Street rails, park jibs, backcountry booters and park jumps, this board will do it all. + variants: + - *product-4-var-1 + + - &product-5 + id: 5 + title: Burton Boots + handle: burton-boots + type: Boots + vendor: Burton + price: 11900 + price_max: 11900 + price_min: 11900 + price_varies: false + available: true + tags: + - season2006 + - beginner + - intermediate + - boots + options: + - Color + - Shoe Size + compare_at_price: 22900 + compare_at_price_max: 22900 + compare_at_price_min: 22900 + compare_at_price_varies: false + url: /products/burton-boots + featured_image: products/burton.jpg + images: + - products/burton.jpg + description: + The Burton boots are particularly well on snowboards. The very best thing about them is that the according picture is cubic. This makes testing in a Vision testing environment very easy. + variants: + - *product-5-var-1 + - *product-5-var-2 + - *product-5-var-3 + - *product-5-var-4 + - *product-5-var-5 + - *product-5-var-6 + + - &product-6 + id: 6 + title: Superbike 1198 S + handle: superbike + type: Superbike + vendor: Ducati + price: 2179500 + price_max: 2179500 + price_min: 2179500 + price_varies: false + available: true + tags: + - ducati + - superbike + - bike + - street + - racing + - performance + options: + - Color + compare_at_price: + compare_at_price_max: 0 + compare_at_price_min: 0 + compare_at_price_varies: false + url: /products/superbike + featured_image: products/ducati.jpg + images: + - products/ducati.jpg + description: +

‘S’ PERFORMANCE

+

Producing 170hp (125kW) and with a dry weight of just 169kg (372.6lb), the new 1198 S now incorporates more World Superbike technology than ever before by taking the 1198 motor and adding top-of-the-range suspension, lightweight chassis components and a true racing-style traction control system designed for road use.

+

The high performance, fully adjustable 43mm Öhlins forks, which sport low friction titanium nitride-treated fork sliders, respond effortlessly to every imperfection in the tarmac. Beyond their advanced engineering solutions, one of the most important characteristics of Öhlins forks is their ability to communicate the condition and quality of the tyre-to-road contact patch, a feature that puts every rider in superior control. The suspension set-up at the rear is complemented with a fully adjustable Öhlins rear shock equipped with a ride enhancing top-out spring and mounted to a single-sided swingarm for outstanding drive and traction. The front-to-rear Öhlins package is completed with a control-enhancing adjustable steering damper.

+ variants: + - *product-6-var-1 + + - &product-7 + id: 7 + title: Shopify Shirt + handle: shopify-shirt + type: Shirt + vendor: Shopify + price: 1900 + price_max: 1900 + price_min: 1900 + price_varies: false + available: true + tags: + - shopify + - shirt + - apparel + - tshirt + - clothing + options: + - Color + - Size + compare_at_price: + compare_at_price_max: 0 + compare_at_price_min: 0 + compare_at_price_varies: false + url: /products/shopify-shirt + featured_image: products/shopify_shirt.png + images: + - products/shopify_shirt.png + description: +

High Quality Shopify Shirt. Wear your e-commerce solution with pride and attract attention anywhere you go.

+

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

+ variants: + - *product-7-var-1 + - *product-7-var-2 + - *product-7-var-3 + - *product-7-var-4 + + - &product-8 + id: 8 + title: Hooded Sweater + handle: hooded-sweater + type: Sweater + vendor: Stormtech + price: 5900 + price_max: 5900 + price_min: 5900 + price_varies: false + available: true + tags: + - sweater + - hooded + - apparel + - clothing + options: + - Color + - Size + compare_at_price: 6900 + compare_at_price_max: 6900 + compare_at_price_min: 6900 + compare_at_price_varies: false + url: /products/hooded-sweater + featured_image: products/hooded-sweater.jpg + images: + - products/hooded-sweater.jpg + - products/hooded-sweater-b.jpg + description: +

Extra comfortable zip up sweater. Durable quality, ideal for any outdoor activities.

+

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

+ variants: + - *product-8-var-1 + - *product-8-var-2 + - *product-8-var-3 + - *product-8-var-4 + - *product-8-var-5 + - *product-8-var-6 + + - &product-9 + id: 9 + title: D3 Digital SLR Camera + handle: d3 + type: SLR + vendor: Nikon + price: 499995 + price_max: 552500 + price_min: 499995 + price_varies: true + available: true + tags: + - camera + - slr + - nikon + - professional + options: + - Bundle + compare_at_price: + compare_at_price_max: 0 + compare_at_price_min: 0 + compare_at_price_varies: false + url: /products/d3 + featured_image: products/d3.jpg + images: + - products/d3.jpg + - products/d3_2.jpg + - products/d3_3.jpg + description: +

Flagship pro D-SLR with a 12.1-MP FX-format CMOS sensor, blazing 9 fps shooting at full FX resolution and low-noise performance up to 6400 ISO.

+

Nikon's original 12.1-megapixel FX-format (23.9 x 36mm) CMOS sensor: Couple Nikon's exclusive digital image processing system with the 12.1-megapixel FX-format and you'll get breathtakingly rich images while also reducing noise to unprecedented levels with even higher ISOs.

+

Continuous shooting at up to 9 frames per second: At full FX resolution and up to 11fps in the DX crop mode, the D3 offers uncompromised shooting speeds for fast-action and sports photography.

+ variants: + - *product-9-var-1 + - *product-9-var-2 + - *product-9-var-3 + + +# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +# Line Items +# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + +line_items: + - &line_item-1 + id: 1 + title: 'Arbor Draft' + subtitle: '151cm' + price: 29900 + line_price: 29900 + quantity: 1 + variant: *product-1-var-1 + product: *product-1 + + - &line_item-2 + id: 2 + title: 'Comic ~ Orange' + subtitle: '159cm' + price: 19900 + line_price: 39800 + quantity: 2 + variant: *product-4-var-1 + product: *product-4 + +# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +# Link Lists +# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +links: + - &link-1 + id: 1 + title: Our Sale + url: /collections/sale + - &link-2 + id: 2 + title: Arbor Stuff + url: /collections/arbor + - &link-3 + id: 3 + title: All our Snowboards + url: /collections/snowboards + - &link-4 + id: 4 + title: Powered by Shopify + url: 'http://shopify.com' + - &link-5 + id: 5 + title: About Us + url: /pages/about-us + - &link-6 + id: 6 + title: Policies + url: /pages/shipping + - &link-7 + id: 7 + title: Contact Us + url: /pages/contact + - &link-8 + id: 8 + title: Our blog + url: /blogs/bigcheese-blog + - &link-9 + id: 9 + title: New Boots + url: /products/burton-boots + - &link-10 + id: 10 + title: Paginated Sale + url: /collections/paginated-sale + - &link-11 + id: 11 + title: Our Paginated blog + url: /blogs/paginated-blog + - &link-12 + id: 12 + title: Catalog + url: /collections/all + + + +# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +# Link Lists +# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + +link_lists: + - &link-list-1 + id: 1 + title: 'Main Menu' + handle: 'main-menu' + links: + - *link-12 + - *link-5 + - *link-7 + - *link-8 + - &link-list-2 + id: 1 + title: 'Footer Menu' + handle: 'footer' + links: + - *link-5 + - *link-6 + +# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +# Collections +# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + +collections: + - &collection-1 + id: 1 + title: Frontpage + handle: frontpage + url: /collections/frontpage + products: + - *product-7 + - *product-8 + - *product-9 + + - &collection-2 + id: 2 + title: Arbor + handle: arbor + url: /collections/arbor + products: + - *product-1 + - *product-2 + + - &collection-3 + id: 3 + title: Snowboards + handle: snowboards + url: /collections/snowboards + description: +

This is a description for my Snowboards collection.

+ products: + - *product-1 + - *product-2 + - *product-3 + - *product-4 + + - &collection-4 + id: 4 + title: Items On Sale + handle: sale + url: /collections/sale + products: + - *product-1 + + - &collection-5 + id: 5 + title: Paginated Sale + handle: 'paginated-sale' + url: '/collections/paginated-sale' + products: + - *product-1 + - *product-2 + - *product-3 + - *product-4 + products_count: 210 + + - &collection-6 + id: 6 + title: All products + handle: 'all' + url: '/collections/all' + products: + - *product-7 + - *product-8 + - *product-9 + - *product-6 + - *product-1 + - *product-2 + - *product-3 + - *product-4 + - *product-5 + + +# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +# Pages and Blogs +# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +pages: + - &page-2 + id: 1 + title: Contact Us + handle: contact + url: /pages/contact + author: Tobi + content: + "

You can contact us via phone under (555) 567-2222.

+

Our retail store is located at Rue d'Avignon 32, Avignon (Provence).

+

Opening Hours:
Monday through Friday: 9am - 6pm
Saturday: 10am - 3pm
Sunday: closed

" + created_at: 2005-04-04 12:00 + + - &page-3 + id: 2 + title: About Us + handle: about-us + url: /pages/about-us + author: Tobi + content: + "

Our company was founded in 1894 and we are since operating out of Avignon from the beautiful Provence.

+

We offer the highest quality products and are proud to serve our customers to their heart's content.

" + created_at: 2005-04-04 12:00 + + - &page-4 + id: 3 + title: Shopping Cart + handle: shopping-cart + url: /pages/shopping-cart + author: Tobi + content: "" + created_at: 2005-04-04 12:00 + + - &page-5 + id: 4 + title: Shipping and Handling + handle: shipping + url: /pages/shipping + author: Tobi + content:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

+ created_at: 2005-04-04 12:00 + + - &page-6 + id: 5 + title: Frontpage + handle: frontpage + url: /pages/frontpage + author: Tobi + content:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

+ created_at: 2005-04-04 12:00 + +blogs: + - id: 1 + handle: news + title: News + url: /blogs/news + articles: + - id: 3 + title: 'Welcome to the new Foo Shop' + author: Daniel + content:

Welcome to your Shopify store! The jaded Pixel crew is really glad you decided to take Shopify for a spin.

To help you get you started with Shopify, here are a couple of tips regarding what you see on this page.

The text you see here is an article. To edit this article, create new articles or create new pages you can go to the Blogs & Pages tab of the administration menu.

The Shopify t-shirt above is a product and selling products is what Shopify is all about. To edit this product, or create new products you can go to the Products Tab in of the administration menu.

While you're looking around be sure to check out the Collections and Navigations tabs and soon you will be well on your way to populating your site.

And of course don't forget to browse the theme gallery to pick a new look for your shop!

Shopify is in beta
If you would like to make comments or suggestions please visit us in the Shopify Forums or drop us an email.

+ created_at: 2005-04-04 16:00 + - id: 4 + title: 'Breaking News: Restock on all sales products' + author: Tobi + content: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + created_at: 2005-04-04 12:00 + articles_count: 2 + + - id: 2 + handle: bigcheese-blog + title: Bigcheese blog + url: /blogs/bigcheese-blog + articles: + - id: 1 + title: 'One thing you probably did not know yet...' + author: Justin + content: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + created_at: 2005-04-04 16:00 + comments: + - + id: 1 + author: John Smith + email: john@smith.com + content: Wow...great article man. + status: published + created_at: 2009-01-01 12:00 + updated_at: 2009-02-01 12:00 + url: "" + - + id: 2 + author: John Jones + email: john@jones.com + content: I really enjoyed this article. And I love your shop! It's awesome. Shopify rocks! + status: published + created_at: 2009-03-01 12:00 + updated_at: 2009-02-01 12:00 + url: "http://somesite.com/" + - id: 2 + title: Fascinating + author: Tobi + content: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + created_at: 2005-04-06 12:00 + comments: + articles_count: 2 + comments_enabled?: true + comment_post_url: "" + comments_count: 2 + moderated?: true + + - id: 3 + handle: paginated-blog + title: Paginated blog + url: /blogs/paginated-blog + articles: + - id: 6 + title: 'One thing you probably did not know yet...' + author: Justin + content: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + created_at: 2005-04-04 16:00 + + - id: 7 + title: Fascinating + author: Tobi + content: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + created_at: 2005-04-06 12:00 + articles_count: 200 diff --git a/lib/liquid/compile.rb b/lib/liquid/compile.rb index e712e5c5..816488e0 100644 --- a/lib/liquid/compile.rb +++ b/lib/liquid/compile.rb @@ -1,12 +1,14 @@ # frozen_string_literal: true require 'pry-byebug' +require 'set' module Liquid class Compiler def initialize @ruby = +"" @nodes = {} + @declared = Set.new(['__l_product']) end def <<(line) @@ -17,33 +19,51 @@ module Liquid "__l_#{name}" end + def declare(name) + @declared << name + end + + def declared?(name) + @declared.member?(name) + end + def to_proc - puts @ruby if ENV['SHOW_RUBY'] == '1' - RubyVM::InstructionSequence.compile(<<~RUBY).eval.call(@nodes) + show_ruby = ENV["SHOW_RUBY"] && ENV["SHOW_RUBY"].to_i || 0 + STDERR.puts @ruby if show_ruby >= 1 + res = RubyVM::InstructionSequence.compile(<<~RUBY).eval.call(@nodes) ->(__nodes) { - ->(__context, __output) { + ->(__context, __output, __l_product) { + __scope = __context.environments.first #{@ruby} __output } } RUBY + + STDERR.puts RubyVM::InstructionSequence.disasm(res) if show_ruby >= 2 + res end - # Compiles any thing that can be returned by Expression/QuotedFragment def compile_expr(node) - case node - when Liquid::VariableLookup - @nodes[node.object_id] = node + if node.respond_to?(:compile_expr) node.compile_expr(self) - when Liquid::RangeLookup - @nodes[node.object_id] = node - node.compile_expr(self) - when Range # returned by RangeLookup when range contains only literals - node.inspect - when Integer, Float, nil, true, false, '' - node.inspect else - raise ArgumentError, "cannot compile node #{node.inspect}" + case node + when Range # returned by RangeLookup when range contains only literals + node.inspect + when Integer, Float, nil, true, false, String + node.inspect + else + raise ArgumentError, "cannot compile node #{node.inspect}" + end + end + end + + def output(node) + self << if node.is_a?(String) + "__output << #{node.inspect}\n" + else + "__output << #{compile_expr(node)}.to_s\n" end end @@ -67,38 +87,6 @@ module Liquid RUBY end - def slice_collection_expr(collection_name, from_name, to_name) - <<~RUBY.strip - (begin - if (#{from_name} != 0 || !#{to_name}.nil?) && #{collection_name}.respond_to?(:load_slice) - #{collection_name}.load_slice(#{from_name}, #{to_name}) - else - segments = [] - index = 0 - if #{collection_name}.is_a?(String) - #{collection_name}.empty? ? [] : [collection] - elsif !#{collection_name}.respond_to?(:each) - [] - else - #{collection_name}.each do |item| - if #{to_name} && #{to_name} <= index - break - end - - if #{from_name} && #{from_name} <= index - segments << item - end - - index += 1 - end - - segments - end - end - end) - RUBY - end - def compile(node) if node.instance_of?(String) self << "__output << #{node.inspect}\n" @@ -113,7 +101,7 @@ module Liquid nil end catch_errors(line_number, show_message: !node.blank?) do - self << "__nodes[#{node.object_id}].render_to_output_buffer(__context, __output)\n" + self << "__nodes[#{node.object_id}].render_to_output_buffer(__context, __output) # #{node.inspect} \n" end end end @@ -135,13 +123,12 @@ module Liquid self << "__output << __error_message\n" if show_message self << "end\nend\n" end - end class BlockBody def render_to_output_buffer(context, output) raise "Tried to render uncompiled block" unless @compiled - @compiled.call(context, output) + @compiled.call(context, output, context.environments[0][:product]) end def compile_top_level @@ -169,16 +156,88 @@ module Liquid class VariableLookup def compile_expr(compiler) - compiler.var_name(@name) + # HACK + if @name == "forloop" && @lookups == ["first"] + return "forloop_first" + end + + var_name = compiler.var_name(@name) + root = if compiler.declared?(var_name) + var_name + else + "__scope[#{@name.inspect}]" + end + + @lookups.reduce(root) do |prev, lookup| + "#{prev}[:#{lookup}]" + end end end class Variable + FILTERS = { + "modulo" => ->(compiler, expr, args, kwargs) { + "(#{expr} % #{args[0]})" + }, + "product_img_url" => ->(compiler, expr, args, kwargs) { + style = args.fetch(0, 'small') + + rest = case style + when 'original' + "\"/files/shops/random_number/\#{url}\"" + when 'grande', 'large', 'medium', 'compact', 'small', 'thumb', 'icon' + "\"/files/shops/random_number/products/\#{$1}_#{style}.\#{$2}\"" + else + "raise ArgumentError, 'valid parameters for filter \"size\" are: original, grande, large, medium, compact, small, thumb and icon '" + end + + <<~RUBY.strip + (begin + if #{expr} =~ %r{\\Aproducts/([\\w\\-\\_]+)\\.(\\w{2,4})} + #{rest} + else + raise ArgumentError, 'filter \"size\" can only be called on product images' + end + end) + RUBY + }, + "escape" => ->(compiler, expr, args, kwargs) { + "(_t = #{expr}; CGI.escape(_t) unless _t)" + }, + "money" => ->(compiler, expr, args, kwargs) { + "(_m = #{expr}; _m.nil? ? '' : \"\#{(_m / 100.0).round(2)}\")" + }, + } + def compile(compiler) - compiler.catch_errors(@line_number, show_message: true) do - compiler << "__output << #{@name.compile_expr(compiler)}.to_s\n" + if const? + compiler.output(name) + else + compiler.catch_errors(@line_number, show_message: true) do + compiler.output(self) + end end end + + def const? + @filters.empty? && !name.respond_to?(:compile_expr) + end + + def compile_expr(compiler) + @filters.reduce(compiler.compile_expr(name)) do |expr, (name, args, kwargs)| + if FILTERS.key?(name) + FILTERS[name].call(compiler, expr, args, kwargs) + else + expr + end + end + end + end + + class Echo + def compile(compiler) + compiler.compile(variable) + end end class For @@ -206,11 +265,64 @@ module Liquid RUBY end + item_var = compiler.var_name(variable_name) compiler << <<~RUBY - collection.each do |#{compiler.var_name(variable_name)}| + forloop_first = true + for #{item_var} in collection RUBY + compiler.declare(item_var) compiler.compile(@for_block) + compiler << <<~RUBY + forloop_first = false + end + RUBY + + end + end + + class If + def compile(compiler) + compiler << "if #{compiler.compile_expr(blocks[0])}\n" + compiler.compile(blocks[0].attachment) + blocks.drop(1).each do |block| + if block.else? + compiler << "else\n" + else + compiler << "elsif #{block.compile_expr(compiler)}\n" + end + compiler.compile(block.attachment) + end compiler << "end\n" end end + + class Comment + def compile(_compiler); end + end + + class Condition + def compile_expr(compiler) + condition = if operator + left_expr = compiler.compile_expr(left) + right_expr = compiler.compile_expr(right) + expr = "((#{left_expr} #{operator} #{right_expr}) #{child_relation}" + else + left_expr = compiler.compile_expr(left) + right_expr = compiler.compile_expr(right) + expr = "#{left_expr}" + end + if child_condition + child_expr = compiler.compile_expr(child_condition) + condition << " #{child_expr}" + end + condition + end + end + + class Assign + def compile(compiler) + compliler.declare(@to) + compiler << "#{compiler.var_name(@to)} = #{compiler.compile_expr(@from)}\n" + end + end end diff --git a/performance/benchmark b/performance/benchmark index 9519db22..70f48771 100755 --- a/performance/benchmark +++ b/performance/benchmark @@ -1,8 +1,6 @@ #!/usr/bin/env ruby # frozen_string_literal: true - - unless ENV.key?("BUNDLE_BIN_PATH") exec("bundle", "exec", "ruby", __FILE__, *ARGV) end @@ -156,6 +154,7 @@ def print_columns(cols) end end +ITERS = 1 Benchmarks = Class.new do def define(_name, benchmark) @benchmark = benchmark @@ -167,27 +166,32 @@ Benchmarks = Class.new do parsed = Process.clock_gettime(Process::CLOCK_MONOTONIC, :microsecond) # warmup - 1000.times { @benchmark.render} + ITERS.times { @benchmark.render} warm = Process.clock_gettime(Process::CLOCK_MONOTONIC, :microsecond) res = nil - 1000.times { res = @benchmark.render } + ITERS.times { res = @benchmark.render } ran = Process.clock_gettime(Process::CLOCK_MONOTONIC, :microsecond) - puts res if res.is_a?(String) + if res.is_a?(String) + puts res + puts "Output digest: #{Digest::SHA2.hexdigest(res)}" + end if ENV['SHOW_RUBY'] == '1' STDERR.puts "note: SHOW_RUBY prevents gathering parse metrics" - STDERR.puts "render: #{(ran - warm) / 1000}µs" + STDERR.puts "render: #{(ran - warm) / ITERS}µs" else STDERR.puts "parse: #{parsed - start}µs" - STDERR.puts "render: #{(ran - warm) / 1000}µs" - STDERR.puts "total: #{(ran - warm) / 1000 + (parsed - start)}µs" + STDERR.puts "render: #{(ran - warm) / ITERS}µs" + STDERR.puts "total: #{(ran - warm) / ITERS + (parsed - start)}µs" end end end.new def execute + require 'digest' + if ARGV.count != 2 STDERR.puts "Usage: benchmark.rb record [output_path]" exit(1) diff --git a/performance/benchmarks/fizzbuzz_10000.rb b/performance/benchmarks/fizzbuzz_10000.rb index 14558a1c..08c302f2 100644 --- a/performance/benchmarks/fizzbuzz_10000.rb +++ b/performance/benchmarks/fizzbuzz_10000.rb @@ -1,7 +1,22 @@ # frozen_string_literal: true -Benchmarks.define('simple_loop', Class.new do - TEMPLATE = "{% for i in (1..1000) %}{{ i }}{% endfor %}" +Benchmarks.define('fizzbuzz_10000', Class.new do + TEMPLATE = <<~LIQUID + {% for i in (1..#{ENV['COUNT'].to_i}) %} + {% liquid + assign rem_3 = i | modulo: 3 + assign rem_5 = i | modulo: 5 + if rem_3 == 0 and rem_5 == 0 + echo "Fizzbuzz" + elsif rem_3 == 0 + echo "Fizz" + elsif rem_5 == 0 + echo "Buzz" + else + echo i + endif + %}{% endfor %} + LIQUID def compile @parsed = Liquid::Template.parse(TEMPLATE)