Skip to content

Commit a3c5591

Browse files
committed
Performance fixes
2 parents 7c3cdc7 + 6ca1f3f commit a3c5591

14 files changed

Lines changed: 115 additions & 70 deletions

File tree

.fasterer.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
speedups:
2+
parallel_assignment: false
3+
rescue_vs_respond_to: true
4+
module_eval: true
5+
shuffle_first_vs_sample: true
6+
for_loop_vs_each: true
7+
each_with_index_vs_while: false
8+
map_flatten_vs_flat_map: true
9+
reverse_each_vs_reverse_each: true
10+
select_first_vs_detect: true
11+
sort_vs_sort_by: true
12+
fetch_with_argument_vs_block: true
13+
keys_each_vs_each_key: true
14+
hash_merge_bang_vs_hash_brackets: true
15+
block_vs_symbol_to_proc: true
16+
proc_call_vs_yield: true
17+
gsub_vs_tr: true
18+
select_last_vs_reverse_detect: true
19+
getter_vs_attr_reader: false
20+
setter_vs_attr_writer: false
21+
22+
exclude_paths:
23+
- 'vendor/**/*.rb'
24+
- 'spec/**/*.rb'

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ end
3232
group :test do
3333
gem "rake"
3434
gem "equivalent-xml"
35+
gem 'fasterer'
3536
end
3637

3738
platforms :rbx do

lib/rdf/format.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def self.reader_symbols
205205
#
206206
# @return [Array<String>]
207207
def self.reader_types
208-
reader_symbols.map {|s| RDF::Format.for(s).content_type}.flatten.uniq
208+
reader_symbols.flat_map {|s| RDF::Format.for(s).content_type}.uniq
209209
end
210210

211211
##
@@ -231,7 +231,7 @@ def self.writer_symbols
231231
#
232232
# @return [Array<String>]
233233
def self.writer_types
234-
writer_symbols.map {|s| RDF::Format.for(s).content_type}.flatten.uniq
234+
writer_symbols.flat_map {|s| RDF::Format.for(s).content_type}.uniq
235235
end
236236

237237
##

lib/rdf/mixin/enumerable.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,10 @@ def has_triple?(triple)
218218
#
219219
# @return [void]
220220
# @see #enum_triple
221-
def each_triple(&block)
221+
def each_triple
222222
if block_given?
223223
each_statement do |statement|
224-
block.call(*statement.to_triple)
224+
yield *statement.to_triple
225225
end
226226
end
227227
enum_triple
@@ -281,10 +281,10 @@ def has_quad?(quad)
281281
#
282282
# @return [void]
283283
# @see #enum_quad
284-
def each_quad(&block)
284+
def each_quad
285285
if block_given?
286286
each_statement do |statement|
287-
block.call(*statement.to_quad)
287+
yield *statement.to_quad
288288
end
289289
end
290290
enum_quad
@@ -346,14 +346,14 @@ def has_subject?(value)
346346
#
347347
# @return [void]
348348
# @see #enum_subject
349-
def each_subject(&block)
349+
def each_subject
350350
if block_given?
351351
values = {}
352352
each_statement do |statement|
353353
value = statement.subject
354354
unless value.nil? || values.include?(value.to_s)
355355
values[value.to_s] = true
356-
block.call(value)
356+
yield value
357357
end
358358
end
359359
end
@@ -414,14 +414,14 @@ def has_predicate?(value)
414414
#
415415
# @return [void]
416416
# @see #enum_predicate
417-
def each_predicate(&block)
417+
def each_predicate
418418
if block_given?
419419
values = {}
420420
each_statement do |statement|
421421
value = statement.predicate
422422
unless value.nil? || values.include?(value.to_s)
423423
values[value.to_s] = true
424-
block.call(value)
424+
yield value
425425
end
426426
end
427427
end
@@ -482,14 +482,14 @@ def has_object?(value)
482482
#
483483
# @return [void]
484484
# @see #enum_object
485-
def each_object(&block) # FIXME: deduplication
485+
def each_object # FIXME: deduplication
486486
if block_given?
487487
values = {}
488488
each_statement do |statement|
489489
value = statement.object
490490
unless value.nil? || values.include?(value)
491491
values[value] = true
492-
block.call(value)
492+
yield value
493493
end
494494
end
495495
end
@@ -551,14 +551,14 @@ def has_context?(value)
551551
#
552552
# @return [void]
553553
# @see #enum_context
554-
def each_context(&block)
554+
def each_context
555555
if block_given?
556556
values = {}
557557
each_statement do |statement|
558558
value = statement.context
559559
unless value.nil? || values.include?(value)
560560
values[value] = true
561-
block.call(value)
561+
yield value
562562
end
563563
end
564564
end
@@ -595,11 +595,11 @@ def enum_context
595595
# @return [void]
596596
# @see #enum_graph
597597
# @since 0.1.9
598-
def each_graph(&block)
598+
def each_graph
599599
if block_given?
600-
block.call(RDF::Graph.new(nil, :data => self))
600+
yield RDF::Graph.new(nil, :data => self)
601601
each_context do |context|
602-
block.call(RDF::Graph.new(context, :data => self))
602+
yield RDF::Graph.new(context, :data => self)
603603
end
604604
end
605605
enum_graph

lib/rdf/model/graph.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,12 @@ def graphs
312312
# @private
313313
# @see RDF::Enumerable#each_graph
314314
# @since 0.2.0
315-
def each_graph(&block)
316-
block_given? ? block.call(self) : enum_graph
315+
def each_graph
316+
if block_given?
317+
yield self
318+
else
319+
enum_graph
320+
end
317321
end
318322
end
319323
end

lib/rdf/model/list.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -536,13 +536,13 @@ def slice_with_range(range)
536536
#
537537
# @return [RDF::Term]
538538
# @see http://ruby-doc.org/core-1.9/classes/Array.html#M000420
539-
def fetch(index, default = UNSET, &block)
539+
def fetch(index, default = UNSET)
540540
each.with_index do |v, i|
541541
return v if i == index
542542
end
543543

544544
case
545-
when block_given? then block.call(index)
545+
when block_given? then yield index
546546
when !default.eql?(UNSET) then default
547547
else raise IndexError, "index #{index} not in the list #{self.inspect}"
548548
end
@@ -753,16 +753,16 @@ def last_subject
753753
#
754754
# @return [Enumerator]
755755
# @see RDF::Enumerable#each
756-
def each_subject(&block)
756+
def each_subject
757757
return enum_subject unless block_given?
758758

759759
subject = self.subject
760-
block.call(subject)
760+
yield subject
761761

762762
loop do
763763
rest = graph.first_object(:subject => subject, :predicate => RDF.rest)
764764
break if rest.nil? || rest.eql?(RDF.nil)
765-
block.call(subject = rest)
765+
yield subject = rest
766766
end
767767
end
768768

@@ -776,12 +776,12 @@ def each_subject(&block)
776776
#
777777
# @return [Enumerator]
778778
# @see http://ruby-doc.org/core-1.9/classes/Enumerable.html
779-
def each(&block)
779+
def each
780780
return to_enum unless block_given?
781781

782782
each_subject do |subject|
783783
if value = graph.first_object(:subject => subject, :predicate => RDF.first)
784-
block.call(value) # FIXME
784+
yield value # FIXME
785785
end
786786
end
787787
end

lib/rdf/model/node.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ def eql?(other)
142142
# @return [Boolean]
143143
# @see http://www.w3.org/TR/rdf-sparql-query/#func-RDFterm-equal
144144
def ==(other)
145-
case other
146-
when Literal
145+
if other.is_a?(Literal)
147146
# If other is a Literal, reverse test to consolodate complex type checking logic
148147
other == self
149148
else

lib/rdf/model/statement.rb

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,16 @@ def self.from(statement, options = {})
7979
# @option options [RDF::Term] :context (nil)
8080
# @return [RDF::Statement]
8181
def initialize(subject = nil, predicate = nil, object = nil, options = {})
82-
case subject
83-
when Hash
84-
@options = subject.dup
85-
@subject = @options.delete(:subject)
86-
@predicate = @options.delete(:predicate)
87-
@object = @options.delete(:object)
88-
else
89-
@options = !options.empty? ? options.dup : {}
90-
@subject = subject
91-
@predicate = predicate
92-
@object = object
82+
if subject.is_a?(Hash)
83+
@options = Hash[subject] # faster subject.dup
84+
@subject = @options.delete(:subject)
85+
@predicate = @options.delete(:predicate)
86+
@object = @options.delete(:object)
87+
else
88+
@options = !options.empty? ? Hash[options] : {}
89+
@subject = subject
90+
@predicate = predicate
91+
@object = object
9392
end
9493
@id = @options.delete(:id) if @options.has_key?(:id)
9594
@context = @options.delete(:context)
@@ -100,18 +99,24 @@ def initialize(subject = nil, predicate = nil, object = nil, options = {})
10099
# @private
101100
def initialize!
102101
@context = Node.intern(@context) if @context.is_a?(Symbol)
103-
@subject = case @subject
104-
when nil then nil
105-
when Symbol then Node.intern(@subject)
106-
when Value then @subject.to_term
107-
else raise ArgumentError, "expected subject to be nil or a term, was #{@subject.inspect}"
102+
@subject = if @subject.is_a?(Value)
103+
@subject.to_term
104+
elsif @subject.is_a?(Symbol)
105+
Node.intern(@subject)
106+
elsif @subject.nil?
107+
nil
108+
else
109+
raise ArgumentError, "expected subject to be nil or a term, was #{@subject.inspect}"
108110
end
109111
@predicate = Node.intern(@predicate) if @predicate.is_a?(Symbol)
110-
@object = case @object
111-
when nil then nil
112-
when Symbol then Node.intern(@object)
113-
when Value then @object.to_term
114-
else Literal.new(@object)
112+
@object = if @object.is_a?(Value)
113+
@object.to_term
114+
elsif @object.is_a?(Symbol)
115+
Node.intern(@object)
116+
elsif @object.nil?
117+
nil
118+
else
119+
Literal.new(@object)
115120
end
116121
end
117122

lib/rdf/model/uri.rb

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -873,18 +873,18 @@ def parse(value)
873873
parts[:path] = (path.to_s.force_encoding(Encoding::UTF_8) unless path.empty?)
874874
parts[:query] = (query[1..-1].force_encoding(Encoding::UTF_8) if query)
875875
parts[:fragment] = (fragment[1..-1].force_encoding(Encoding::UTF_8) if fragment)
876-
877-
parts.each_key do |k|
878-
parts[k].force_encoding(Encoding::UTF_8) if parts[k].respond_to?(:encoding)
879-
end
880876
end
881877

882878
parts
883879
end
884880

885881
##
886882
# @return [String]
887-
def scheme; object.fetch(:scheme, nil); end
883+
def scheme
884+
object.fetch(:scheme) do
885+
nil
886+
end
887+
end
888888

889889
##
890890
# @param [String, #to_s] value
@@ -1014,7 +1014,11 @@ def normalized_port
10141014

10151015
##
10161016
# @return [String]
1017-
def path; object.fetch(:path, nil); end
1017+
def path
1018+
object.fetch(:path) do
1019+
nil
1020+
end
1021+
end
10181022

10191023
##
10201024
# @param [String, #to_s] value
@@ -1071,7 +1075,11 @@ def normalized_path
10711075

10721076
##
10731077
# @return [String]
1074-
def query; object.fetch(:query, nil); end
1078+
def query
1079+
object.fetch(:query) do
1080+
nil
1081+
end
1082+
end
10751083

10761084
##
10771085
# @param [String, #to_s] value
@@ -1091,7 +1099,11 @@ def normalized_query
10911099

10921100
##
10931101
# @return [String]
1094-
def fragment; object.fetch(:fragment, nil); end
1102+
def fragment
1103+
object.fetch(:fragment) do
1104+
nil
1105+
end
1106+
end
10951107

10961108
##
10971109
# @param [String, #to_s] value

lib/rdf/query.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ def execute(queryable, options = {}, &block)
322322

323323
old_solutions, @solutions = @solutions, Query::Solutions()
324324

325-
options[:bindings].keys.each do |variable|
325+
options[:bindings].each_key do |variable|
326326
if pattern.variables.include?(variable)
327327
unbound_solutions, old_solutions = old_solutions, Query::Solutions()
328328
options[:bindings][variable].each do |binding|

0 commit comments

Comments
 (0)