Skip to content

Commit

Permalink
Double the speed for Utils.build_nested_query for handling large arrays
Browse files Browse the repository at this point in the history
The branch to take the prefix will be taken at most one time when
inside the loop, and this code is only called if the array is non-empty,
meaning it is called at least one time.  So it's safe to move it
outside the loop.

While here, change the probably wrong regexp use with $ to a
String#end_with? call.

I tried using the buffer-based approach used for build_multipart
in build_nested_query, but it did not provide a measurable speedup,
and made the method measurably slower for some inputs.
  • Loading branch information
jeremyevans committed Jun 20, 2022
1 parent 1ffd860 commit 551fd05
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rack/test/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def build_nested_query(value, prefix = nil)
if value.empty?
"#{prefix}[]="
else
prefix += "[]" unless unescape(prefix).end_with?('[]')
value.map do |v|
prefix = "#{prefix}[]" unless unescape(prefix) =~ /\[\]$/
build_nested_query(v, prefix.to_s)
end.join('&')
end
Expand Down

0 comments on commit 551fd05

Please sign in to comment.