Compare commits

...

2 Commits

Author SHA1 Message Date
Justin Li
350addf364 Run liquidtruffle from the performance runner 2019-08-23 16:11:00 -04:00
Justin Li
49488e3757 Add rake dump_portable_ast 2019-08-21 13:24:21 -04:00
6 changed files with 58 additions and 7 deletions

10
Gemfile
View File

@@ -9,9 +9,9 @@ group :benchmark, :test do
gem 'benchmark-ips'
gem 'memory_profiler'
install_if -> { RUBY_PLATFORM !~ /mingw|mswin|java/ } do
gem 'stackprof'
end
# install_if -> { RUBY_PLATFORM !~ /mingw|mswin|java/ } do
# gem 'stackprof'
# end
end
group :test do
@@ -21,3 +21,7 @@ group :test do
gem 'liquid-c', github: 'Shopify/liquid-c', ref: '9168659de45d6d576fce30c735f857e597fa26f6'
end
end
# gem "byebug", "~> 11.0"
gem "pry", "~> 0.12.2"

View File

@@ -100,3 +100,9 @@ end
task :console do
exec 'irb -I lib -r liquid'
end
task :dump_portable_ast, [:filename] do |_task, args|
require 'liquid/ast_to_portable_json'
ast = Liquid::Template.parse(File.read(args[:filename]))
puts(Liquid::ASTToPortableJSON.dump(ast))
end

View File

@@ -0,0 +1,27 @@
require 'yaml'
require 'json'
require 'liquid'
module Liquid
class ASTToPortableJSON
def self.dump(ast)
yaml = YAML.dump(ast)
yaml.gsub!(/---.*/, '')
yaml.gsub!(/!ruby\/object:(.+)\n(\s+)/, "\n\\2class_name: \\1\n\\2")
bare_hash_ast = YAML.load(yaml)
delete_parse_context(bare_hash_ast)
JSON.pretty_generate(bare_hash_ast)
end
def self.delete_parse_context(bare_hash_ast)
if bare_hash_ast.is_a?(Array)
bare_hash_ast.each { |v| delete_parse_context(v) }
elsif bare_hash_ast.is_a?(Hash)
bare_hash_ast.delete('parse_context')
bare_hash_ast.each { |k, v| delete_parse_context(v) }
end
end
end
end

View File

@@ -1,3 +1,4 @@
require 'liquid/ast_to_portable_json'
module Liquid
# Templates are central to liquid.
# Interpretating templates is a two step process. First you compile the
@@ -130,6 +131,8 @@ module Liquid
@line_numbers = options[:line_numbers] || @profiling
parse_context = options.is_a?(ParseContext) ? options : ParseContext.new(options)
@root = Document.parse(tokenize(source), parse_context)
@json = ASTToPortableJSON.dump(self)
@warnings = parse_context.warnings
self
end
@@ -198,6 +201,15 @@ module Liquid
context.add_filters(args.pop)
end
unless @truffle_context
environment = context.environments.first
@truffle_context = Polyglot.eval('liquid', @json)
environment.each do |key, value|
@truffle_context[key] = value
end
end
return @truffle_context[:render].call
# Retrying a render resets resource usage
context.resource_limits.reset

View File

@@ -1,3 +1,4 @@
require 'bundler/setup'
require 'benchmark/ips'
require_relative 'theme_runner'
@@ -5,14 +6,15 @@ Liquid::Template.error_mode = ARGV.first.to_sym if ARGV.first
profiler = ThemeRunner.new
Benchmark.ips do |x|
x.time = 10
x.warmup = 5
x.time = 60
x.warmup = 1
puts
puts "Running benchmark for #{x.time} seconds (with #{x.warmup} seconds warmup)."
puts
x.report("parse:") { profiler.compile }
profiler.compile
# x.report("parse:") { profiler.compile }
x.report("render:") { profiler.render }
x.report("parse & render:") { profiler.run }
end

View File

@@ -24,7 +24,7 @@ class ThemeRunner
# Initialize a new liquid ThemeRunner instance
# Will load all templates into memory, do this now so that we don't profile IO.
def initialize
@tests = Dir[__dir__ + '/tests/**/*.liquid'].collect do |test|
@tests = Dir[__dir__ + '/tests/ripen/product.liquid'].collect do |test|
next if File.basename(test) == 'theme.liquid'
theme_path = File.dirname(test) + '/theme.liquid'