Share
Embed
`params` deep dive
meguro.rb #19 https://megurorb.connpass.com/event/100401/
izumin5210
September 27, 2018
Transcript
18 slides
-
‣ Wantedly People ‣ Web Engineer (serverside & frontend) ‣
Gopher & Rubyist izumin5210 -
def index q = params[:query] # ... end puts params.class
#=> ActionController::Parameter -
[10] pry(main)> cd SamplesController.new [11] pry(#<SamplesController>):1> show-source params From: /.../actionpack-5.2.1/lib/action_controller/metal/strong_parameters.rb
@ line 1075: Owner: ActionController::StrongParameters Visibility: public Number of lines: 3 def params @_params ||= Parameters.new(request.parameters) end -
[4] pry(main)> cd ActionController::Parameters [5] pry(ActionController::Parameters):1> show-source initialize From: /.../actionpack-5.2.1/lib/action_controller/metal/strong_parameters.rb
@ line 235: Owner: ActionController::Parameters Visibility: private Number of lines: 4 def initialize(parameters = {}) @parameters = parameters.with_indifferent_access @permitted = self.class.permit_all_parameters end -
[4] pry(main)> cd ActionController::Parameters [5] pry(ActionController::Parameters):1> show-source initialize From: /.../actionpack-5.2.1/lib/action_controller/metal/strong_parameters.rb
@ line 235: Owner: ActionController::Parameters Visibility: private Number of lines: 4 def initialize(parameters = {}) @parameters = parameters.with_indifferent_access @permitted = self.class.permit_all_parameters end -
[4] pry(main)> cd ActionController::Parameters [5] pry(ActionController::Parameters):1> show-source initialize From: /.../actionpack-5.2.1/lib/action_controller/metal/strong_parameters.rb
@ line 235: Owner: ActionController::Parameters Visibility: private Number of lines: 4 def initialize(parameters = {}) @parameters = parameters.with_indifferent_access @permitted = self.class.permit_all_parameters end ActiveSupport::HashWithIndifferentAccess LFZ͕4USJOHͰ4ZNCPMͰ͍͍ײ͡ʹͯ͘͠ΕΔ͍ͭ͢͝ -
[8] pry(main)> cd Hash [9] pry(Hash):1> show-source with_indifferent_access From: /.../activesupport-5.2.1/lib/active_support/core_ext/hash/indifferent_access.rb
@ line 9: Owner: Hash Visibility: public Number of lines: 3 def with_indifferent_access ActiveSupport::HashWithIndifferentAccess.new(self) end -
[15] pry(main)> cd ActiveSupport::HashWithIndifferentAccess [16] pry(ActiveSupport::HashWithIndifferentAccess):1> show-source initialize From: /.../activesupport-5.2.1/lib/active_support/hash_with_indifferent_access.rb
@ line 66: Owner: ActiveSupport::HashWithIndifferentAccess Visibility: private Number of lines: 12 def initialize(constructor = {}) if constructor.respond_to?(:to_hash) super() update(constructor) hash = constructor.to_hash self.default = hash.default if hash.default self.default_proc = hash.default_proc if hash.default_proc else super(constructor) end end -
[22] pry(ActiveSupport::HashWithIndifferentAccess):1> show-source convert_value From: /.../activesupport-5.2.1/lib/active_support/hash_with_indifferent_access.rb @ line 352: Owner:
ActiveSupport::HashWithIndifferentAccess Visibility: private Number of lines: 16 def convert_value(value, options = {}) # :doc: if value.is_a? Hash if options[:for] == :to_hash value.to_hash else value.nested_under_indifferent_access end elsif value.is_a?(Array) if options[:for] != :assignment || value.frozen? value = value.dup end value.map! { |e| convert_value(e, options) } else value end end -
[22] pry(ActiveSupport::HashWithIndifferentAccess):1> show-source convert_value From: /.../activesupport-5.2.1/lib/active_support/hash_with_indifferent_access.rb @ line 352: Owner:
ActiveSupport::HashWithIndifferentAccess Visibility: private Number of lines: 16 def convert_value(value, options = {}) # :doc: if value.is_a? Hash if options[:for] == :to_hash value.to_hash else value.nested_under_indifferent_access end elsif value.is_a?(Array) if options[:for] != :assignment || value.frozen? value = value.dup end value.map! { |e| convert_value(e, options) } else value end end )BTIΛͯ͢ᢞΊͯɼΩʔͷมͱ͔͢Δ AQBSBNTAͷཁૉΛݟΔ͚ͩͰɼύϥϝλશମΛࠪ͢Δ -
:) % be stackprof tmp/stackprof/* --limit 15 ================================== Mode: wall(1000)
Samples: 27791 (6.40% miss rate) GC: 5610 (20.19%) ================================== TOTAL (pct) SAMPLES (pct) FRAME 7839 (28.2%) 7839 (28.2%) Puma::Single#run 5610 (20.2%) 5610 (20.2%) (garbage collection) 6919 (24.9%) 5264 (18.9%) ActiveSupport::HashWithIndifferentAccess#convert_value 6299 (22.7%) 1818 (6.5%) ActiveSupport::HashWithIndifferentAccess#to_hash 3466 (12.5%) 1728 (6.2%) ActionController::LogSubscriber#start_processing 1879 (6.8%) 925 (3.3%) ActionDispatch::Http::ParameterFilter::CompiledFilter#call 879 (3.2%) 878 (3.2%) ActiveSupport::ToJsonWithActiveSupportEncoder#to_json 1452 (5.2%) 574 (2.1%) ActiveSupport::JSON::Encoding::JSONGemEncoder::EscapedString#to_json 1216 (4.4%) 518 (1.9%) ActionDispatch::Request::Utils::ParamEncoder.normalize_encode_params 510 (1.8%) 460 (1.7%) ActiveSupport::JSON::Encoding::JSONGemEncoder#jsonify 506 (1.8%) 446 (1.6%) Hash#as_json 701 (2.5%) 352 (1.3%) ActiveSupport::HashWithIndifferentAccess#update 317 (1.1%) 317 (1.1%) ActiveSupport::JSON::Encoding::JSONGemEncoder::EscapedString#to_s 267 (1.0%) 267 (1.0%) ActiveSupport::HashWithIndifferentAccess#convert_key 954 (3.4%) 253 (0.9%) ActiveSupport::HashWithIndifferentAccess#[]= -
:) % be stackprof tmp/stackprof/* --limit 15 ================================== Mode: wall(1000)
Samples: 27791 (6.40% miss rate) GC: 5610 (20.19%) ================================== TOTAL (pct) SAMPLES (pct) FRAME 7839 (28.2%) 7839 (28.2%) Puma::Single#run 5610 (20.2%) 5610 (20.2%) (garbage collection) 6919 (24.9%) 5264 (18.9%) ActiveSupport::HashWithIndifferentAccess#convert_value 6299 (22.7%) 1818 (6.5%) ActiveSupport::HashWithIndifferentAccess#to_hash 3466 (12.5%) 1728 (6.2%) ActionController::LogSubscriber#start_processing 1879 (6.8%) 925 (3.3%) ActionDispatch::Http::ParameterFilter::CompiledFilter#call 879 (3.2%) 878 (3.2%) ActiveSupport::ToJsonWithActiveSupportEncoder#to_json 1452 (5.2%) 574 (2.1%) ActiveSupport::JSON::Encoding::JSONGemEncoder::EscapedString#to_json 1216 (4.4%) 518 (1.9%) ActionDispatch::Request::Utils::ParamEncoder.normalize_encode_params 510 (1.8%) 460 (1.7%) ActiveSupport::JSON::Encoding::JSONGemEncoder#jsonify 506 (1.8%) 446 (1.6%) Hash#as_json 701 (2.5%) 352 (1.3%) ActiveSupport::HashWithIndifferentAccess#update 317 (1.1%) 317 (1.1%) ActiveSupport::JSON::Encoding::JSONGemEncoder::EscapedString#to_s 267 (1.0%) 267 (1.0%) ActiveSupport::HashWithIndifferentAccess#convert_key 954 (3.4%) 253 (0.9%) ActiveSupport::HashWithIndifferentAccess#[]= +40/͕ʢߏతʹʣͰ͔͍ͱʜ -
[1] pry(main)> cd ActionController::Parameters [2] pry(ActionController::Parameters):1> show-source permit From: /.../actionpack-5.2.1/lib/action_controller/metal/strong_parameters.rb
@ line 528: Owner: ActionController::Parameters Visibility: public Number of lines: 16 def permit(*filters) params = self.class.new filters.flatten.each do |filter| case filter when Symbol, String permitted_scalar_filter(params, filter) when Hash hash_filter(params, filter) end end unpermitted_parameters!(params) if self.class.action_on_unpermitted_parameters -
[1] pry(main)> cd ActionController::Parameters [2] pry(ActionController::Parameters):1> show-source permit From: /.../actionpack-5.2.1/lib/action_controller/metal/strong_parameters.rb
@ line 528: Owner: ActionController::Parameters Visibility: public Number of lines: 16 def permit(*filters) params = self.class.new filters.flatten.each do |filter| case filter when Symbol, String permitted_scalar_filter(params, filter) when Hash hash_filter(params, filter) end end unpermitted_parameters!(params) if self.class.action_on_unpermitted_parameters 4USPOH1BSBNFUFSTͷQFSNJU EFFQʹ୳ࡧ͞ΕΔ