Project

General

Profile

Actions

Feature #17111

open

Improve performance of Net::HTTPHeader#set_form by 40%

Added by tonytonyjan (Weihang Jian) about 4 years ago. Updated about 4 years ago.

Status:
Assigned
Target version:
-
[ruby-core:99530]

Description

diff

diff --git a/lib/net/http/header.rb b/lib/net/http/header.rb
index a8901e7..3f1a008 100644
--- a/lib/net/http/header.rb
+++ b/lib/net/http/header.rb
@@ -475,9 +475,8 @@ def set_form(params, enctype='application/x-www-form-urlencoded', formopt={})
     @body = nil
     @body_stream = nil
     @form_option = formopt
-    case enctype
-    when /\Aapplication\/x-www-form-urlencoded\z/i,
-      /\Amultipart\/form-data\z/i
+    case enctype.downcase
+    when 'application/x-www-form-urlencoded', 'multipart/form-data'
       self.content_type = enctype
     else
       raise ArgumentError, "invalid enctype: #{enctype}"

benchmark

require 'benchmark'
require 'net/http'

module Net::HTTPHeader
  def set_form2(params, enctype = 'application/x-www-form-urlencoded', formopt = {})
    @body_data = params
    @body = nil
    @body_stream = nil
    @form_option = formopt
    case enctype.downcase
    when 'application/x-www-form-urlencoded', 'multipart/form-data'
      self.content_type = enctype
    else
      raise ArgumentError, "invalid enctype: #{enctype}"
    end
  end
end

n = 500_000
request = Net::HTTP::Post.new('/')
Benchmark.bm do |x|
  GC.disable
  x.report { n.times { request.set_form [] } }
  x.report { n.times { request.set_form2 [] } }
end
       user     system      total        real
   0.777054   0.101768   0.878822 (  0.880472)
   0.539860   0.088957   0.628817 (  0.630178)

I don't see any test for #set_form in test/net/http/test_httpheader.rb, let me know if I need to add more tests, thanks!


Files

patch-1.diff (621 Bytes) patch-1.diff tonytonyjan (Weihang Jian), 08/10/2020 04:04 AM

Updated by mame (Yusuke Endoh) about 4 years ago

  • Status changed from Open to Assigned
  • Assignee set to naruse (Yui NARUSE)
Actions

Also available in: Atom PDF

Like0
Like0