Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance fixes #19

Merged
merged 4 commits into from
Sep 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions .fasterer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
speedups:
parallel_assignment: false
rescue_vs_respond_to: true
module_eval: true
shuffle_first_vs_sample: true
for_loop_vs_each: true
each_with_index_vs_while: false
map_flatten_vs_flat_map: true
reverse_each_vs_reverse_each: true
select_first_vs_detect: true
sort_vs_sort_by: true
fetch_with_argument_vs_block: true
keys_each_vs_each_key: true
hash_merge_bang_vs_hash_brackets: true
block_vs_symbol_to_proc: true
proc_call_vs_yield: true
gsub_vs_tr: true
select_last_vs_reverse_detect: true
getter_vs_attr_reader: false
setter_vs_attr_writer: false

exclude_paths:
- 'vendor/**/*.rb'
- 'spec/**/*.rb'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
/spec.pdf
/pkg/
/.rbx/
/.bundle/
Gemfile.lock
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ gem 'jsonlint', git: "git://github.com/dougbarth/jsonlint.git", platform
group :development do
gem 'rdf-turtle', git: "git://github.com/ruby-rdf/rdf-turtle.git", branch: "develop"
gem 'rdf-trig', git: "git://github.com/ruby-rdf/rdf-trig.git", branch: "develop"
gem 'fasterer'
end

group :development, :test do
Expand Down
10 changes: 5 additions & 5 deletions lib/json/ld/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class API
# @yieldparam [API]
# @raise [JsonLdError]
def initialize(input, context, options = {}, &block)
@options = {compactArrays: true, rename_bnodes: true}.merge(options)
@options = {compactArrays: true, rename_bnodes: true}.merge!(options)
@options[:validate] = true if @options[:processingMode] == "json-ld-1.0"
@options[:documentLoader] ||= self.class.method(:documentLoader)
@namer = options[:unique_bnodes] ? BlankNodeUniqer.new : (@options[:rename_bnodes] ? BlankNodeNamer.new("b") : BlankNodeMapper.new)
Expand All @@ -98,7 +98,7 @@ def initialize(input, context, options = {}, &block)
@value = case input
when Array, Hash then input.dup
when IO, StringIO
@options = {base: input.base_uri}.merge(@options) if input.respond_to?(:base_uri)
@options = {base: input.base_uri}.merge!(@options) if input.respond_to?(:base_uri)

# if input impelements #links, attempt to get a contextUrl from that link
content_type = input.respond_to?(:content_type) ? input.content_type : "application/json"
Expand All @@ -113,7 +113,7 @@ def initialize(input, context, options = {}, &block)
when String
remote_doc = @options[:documentLoader].call(input, @options)

@options = {base: remote_doc.documentUrl}.merge(@options)
@options = {base: remote_doc.documentUrl}.merge!(@options)
context_ref = remote_doc.contextUrl

case remote_doc.document
Expand Down Expand Up @@ -322,7 +322,7 @@ def self.frame(input, frame, options = {})
requireAll: true,
omitDefault: false,
documentLoader: method(:documentLoader)
}.merge(options)
}.merge!(options)

framing_state = {
graphs: {'@default' => {}, '@merged' => {}},
Expand Down Expand Up @@ -471,7 +471,7 @@ def self.toRdf(input, options = {}, &block)
# @return [Object, Hash]
# If a block is given, the result of evaluating the block is returned, otherwise, the expanded JSON-LD document
def self.fromRdf(input, options = {}, &block)
options = {useNativeTypes: false}.merge(options)
options = {useNativeTypes: false}.merge!(options)
result = nil

API.new(nil, nil, options) do |api|
Expand Down
2 changes: 1 addition & 1 deletion lib/json/ld/compact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def compact(element, property = nil)
inside_reverse = property == '@reverse'
result = {}

element.keys.each do |expanded_property|
element.each_key do |expanded_property|
expanded_value = element[expanded_property]
debug("") {"#{expanded_property}: #{expanded_value.inspect}"}

Expand Down
4 changes: 2 additions & 2 deletions lib/json/ld/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def parse(local_context, remote_contexts = [])
defined = {}
# For each key-value pair in context invoke the Create Term Definition subalgorithm, passing result for active context, context for local context, key, and defined
depth do
context.keys.each do |key|
context.each_key do |key|
result.create_term_definition(context, key, defined)
end
end
Expand Down Expand Up @@ -990,7 +990,7 @@ def compact_iri(iri, options = {})
# @raise [RDF::ReaderError] if the iri cannot be expanded
# @see http://json-ld.org/spec/latest/json-ld-api/#value-expansion
def expand_value(property, value, options = {})
options = {useNativeTypes: false}.merge(options)
options = {useNativeTypes: false}.merge!(options)
depth(options) do
debug("expand_value") {"property: #{property.inspect}, value: #{value.inspect}"}

Expand Down
2 changes: 1 addition & 1 deletion lib/json/ld/expand.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Expand
# Ensure output objects have keys ordered properly
# @return [Array, Hash]
def expand(input, active_property, context, options = {})
options = {ordered: true}.merge(options)
options = {ordered: true}.merge!(options)
debug("expand") {"input: #{input.inspect}, active_property: #{active_property.inspect}, context: #{context.inspect}"}
result = case input
when Array
Expand Down
2 changes: 1 addition & 1 deletion lib/json/ld/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def update_obj(obj, reference_map)
stub: true
)
else
obj.keys.each do |k|
obj.each_key do |k|
obj[k] = update_obj(obj[k], reference_map)
end
obj
Expand Down
13 changes: 6 additions & 7 deletions lib/json/ld/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Utils
# @return [Boolean]
def node?(value)
value.is_a?(Hash) &&
(value.keys & %w(@value @list @set)).empty? &&
!(value.has_key?('@value') || value.has_key?('@list') || value.has_key?('@set')) &&
(value.length > 1 || !value.has_key?('@id'))
end

Expand Down Expand Up @@ -118,7 +118,7 @@ def compare_values(v1, v2)
# true to allow duplicates, false not to (uses
# a simple shallow comparison of subject ID or value).
def add_value(subject, property, value, options = {})
options = {property_is_array: false, allow_duplicate: true}.merge(options)
options = {property_is_array: false, allow_duplicate: true}.merge!(options)

if value.is_a?(Array)
subject[property] = [] if value.empty? && options[:property_is_array]
Expand Down Expand Up @@ -211,14 +211,13 @@ def merge_compacted_value(hash, key, value)
# Add debug event to debug array, if specified
#
# param [String] message
# yieldreturn [String] appended to message, to allow for lazy-evaulation of message
# yieldreturn [String] appended to message, to allow for lazy-evaluation of message
def debug(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
return unless ::JSON::LD.debug? || @options[:debug]
depth = options[:depth] || @depth || 0
depth = @depth || 0
list = args
list << yield if block_given?
message = " " * depth * 2 + (list.empty? ? "" : list.join(": "))
message = " " * depth * 2 + list.join(": ")
case @options[:debug]
when Array
@options[:debug] << message
Expand Down Expand Up @@ -308,4 +307,4 @@ def get_sym(old = "")
end
end
end
end
end