Slide 1

Slide 1 text

meguro.rb #19 - @izumin5210 `params` deep dive

Slide 2

Slide 2 text

‣ Wantedly People ‣ Web Engineer (serverside & frontend) ‣ Gopher & Rubyist izumin5210

Slide 3

Slide 3 text

params 3VCZPO3BJMTͷ࿩Ͱ͢

Slide 4

Slide 4 text

def index q = params[:query] # ... end

Slide 5

Slide 5 text

def index q = params[:query] # ... end puts params.class #=> ActionController::Parameter

Slide 6

Slide 6 text

[10] pry(main)> cd SamplesController.new [11] pry(#):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

Slide 7

Slide 7 text

[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

Slide 8

Slide 8 text

[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

Slide 9

Slide 9 text

[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Ͱ΋͍͍ײ͡ʹͯ͘͠ΕΔ͍͢͝΍ͭ

Slide 10

Slide 10 text

[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

Slide 11

Slide 11 text

[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

Slide 12

Slide 12 text

[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

Slide 13

Slide 13 text

[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ͷཁૉΛݟΔ͚ͩͰ΋ɼύϥϝλશମΛ૸ࠪ͢Δ

Slide 14

Slide 14 text

:) % 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#[]=

Slide 15

Slide 15 text

:) % 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/͕ʢߏ଄తʹʣͰ͔͍ͱʜ

Slide 16

Slide 16 text

[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

Slide 17

Slide 17 text

[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ʹ୳ࡧ͞ΕΔ

Slide 18

Slide 18 text

QBSBNTͷ͝ར༻͸ܭըతʹ ྫྷ੩ʹߟ͑ͯίϨ͕ϘτϧωοΫʹͳΔ͜ͱ͸΄΅ͳ͍͸ͣ
 ͕ɼແ༻ͳXJUI@JOEJGGFSFOU@BDDFTT͸ݮΒ͠·͠ΐ͏