Skip to content

Commit

Permalink
Optimize loading library into memory (jekyll#6910)
Browse files Browse the repository at this point in the history
Merge pull request 6910
  • Loading branch information
ashmaroli authored and jekyllbot committed Apr 10, 2018
1 parent ef027be commit 36fbcaa
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 27 deletions.
29 changes: 29 additions & 0 deletions benchmark/local-require
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'benchmark/ips'
require 'jekyll'
require 'json'

DATA = {"foo"=>"bar", "alpha"=>{"beta"=>"gamma"}, "lipsum"=>["lorem", "ipsum", "dolor"]}

def local_require
require 'json'
JSON.pretty_generate(DATA)
end

def global_require
JSON.pretty_generate(DATA)
end

def graceful_require
Jekyll::External.require_with_graceful_fail("json")
JSON.pretty_generate(DATA)
end

Benchmark.ips do |x|
x.report("local-require") { local_require }
x.report("global-require") { global_require }
x.report("graceful-require") { graceful_require }
x.compare!
end
5 changes: 4 additions & 1 deletion lib/jekyll.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ def require_all(path)
require "rubygems"

# stdlib
require "pathutil"
require "forwardable"
require "fileutils"
require "time"
require "English"
require "pathname"
require "logger"
require "set"
require "csv"
require "json"

# 3rd party
require "pathutil"
require "addressable/uri"
require "safe_yaml/load"
require "liquid"
require "kramdown"
Expand Down
2 changes: 0 additions & 2 deletions lib/jekyll/cleaner.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "set"

module Jekyll
# Handles the cleanup of a site's destination before it is built.
class Cleaner
Expand Down
2 changes: 0 additions & 2 deletions lib/jekyll/commands/doctor.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "addressable/uri"

module Jekyll
module Commands
class Doctor < Command
Expand Down
1 change: 0 additions & 1 deletion lib/jekyll/commands/serve/live_reload_reactor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require "json"
require "em-websocket"

require_relative "websockets"
Expand Down
3 changes: 0 additions & 3 deletions lib/jekyll/converters/markdown/kramdown_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ class KramdownParser
}.freeze

def initialize(config)
unless defined?(Kramdown)
Jekyll::External.require_with_graceful_fail "kramdown"
end
@main_fallback_highlighter = config["highlighter"] || "rouge"
@config = config["kramdown"] || {}
@highlighter = nil
Expand Down
2 changes: 0 additions & 2 deletions lib/jekyll/convertible.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "set"

# Convertible provides methods for converting a pagelike item
# from a certain type of markup into actual content
#
Expand Down
2 changes: 0 additions & 2 deletions lib/jekyll/drops/drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ def to_h
#
# Returns a pretty generation of the hash representation of the Drop.
def inspect
require "json"
JSON.pretty_generate to_h
end

Expand All @@ -155,7 +154,6 @@ def hash_for_json(*)
#
# Returns a JSON representation of the Drop in a String.
def to_json(state = nil)
require "json"
JSON.generate(hash_for_json(state), state)
end

Expand Down
1 change: 0 additions & 1 deletion lib/jekyll/drops/jekyll_drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def to_h
end

def to_json(state = nil)
require "json"
JSON.generate(to_h, state)
end
end
Expand Down
4 changes: 0 additions & 4 deletions lib/jekyll/filters.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# frozen_string_literal: true

require "addressable/uri"
require "json"
require "liquid"

require_all "jekyll/filters"

module Jekyll
Expand Down
2 changes: 0 additions & 2 deletions lib/jekyll/filters/url_filters.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "addressable/uri"

module Jekyll
module Filters
module URLFilters
Expand Down
2 changes: 0 additions & 2 deletions lib/jekyll/reader.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "csv"

module Jekyll
class Reader
attr_reader :site
Expand Down
2 changes: 0 additions & 2 deletions lib/jekyll/site.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "csv"

module Jekyll
class Site
attr_reader :source, :dest, :config
Expand Down
2 changes: 0 additions & 2 deletions lib/jekyll/url.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "addressable/uri"

# Public: Methods that generate a URL for a resource such as a Post or a Page.
#
# Examples
Expand Down
3 changes: 2 additions & 1 deletion lib/jekyll/utils/rouge.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# frozen_string_literal: true

Jekyll::External.require_with_graceful_fail("rouge")

module Jekyll
module Utils
module Rouge

def self.html_formatter(*args)
Jekyll::External.require_with_graceful_fail("rouge") unless defined?(::Rouge)
if old_api?
::Rouge::Formatters::HTML.new(*args)
else
Expand Down

0 comments on commit 36fbcaa

Please sign in to comment.