Share
Embed
Namespace on read
Ruby Association Grant Project "Namespace on read" Report at Ruby Association Activity Report 2024
Satoshi Tagomori
July 21, 2024
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
32 slides
-
Satoshi Tagomori (tagomoris) 2024/07/19 Ruby Association Activity Report Namespace on
Read Ruby Association Grant Project Report -
@tagomoris Satoshi Tagomori Maintainer/Founder: OSS: Fluentd, MessagePack, Norikra, Woothee,
… Event: ISUCON Service: Pathtraq -
ϓϩδΣΫτৄࡉ͓Αͼఔ 2023/10 ʙ 2024/03 • 2023/10/25ɿ ϓϩδΣΫτ࠾௨ ϝϯλʔܾఆ(·ͭͱΏ͖ͻΖ͞Μ) • 2023/10/26
ʙ 2023/03/20ɿϓϩδΣΫτਐߦ • ػೳৄࡉͷݕ౼ • ֦ுϥΠϒϥϦͷγϯϘϧղܾ༻ C API Ճ (Ruby 3.3) • ࣮ํ๏ͷݕ౼͓Αͼ࣮ • 2024/01/11ɿ தؒใࠂఏग़ • 2024/03/20ɿ ࠷ऴใࠂఏग़ -
ϓϩδΣΫτ֓ཁ Namespace on read Rubyʹɺ௨ৗͷάϩʔόϧͳ֊Խ໊લۭؒΛͭϞδϡʔϧͷϥΠϒϥϦΛɺಡΈࠐΈ࣌ʹن ఆͨ͠ԾతͳτοϓϨϕϧ໊લۭؒʹಡΈࠐΉػೳΛՃ͢Δ͜ͱΛతͱͯ͠ύον։ൃΛߦ ͏ɻ ͜ͷ໊લۭؒʹಡΈࠐ·ΕͨϥΠϒϥϦάϩʔόϧͳ໊લۭؒͱಠཱͨ͠ଘࡏͱͳΔͨΊɺ໊ લিಥͷճආɺڞ༗͞ΕͨϞδϡʔϧɾΫϥεͷૢ࡞ɾมߋʹؔ͢Δڝ߹ͷճආɺͳΒͼʹকདྷత ʹΞϓϦέʔγϣϯ͕ґଘ͢ΔϥΠϒϥϦؒͰͷόʔδϣϯڝ߹ͷճආͷ࣮ݱΛՄೳʹ͢Δͱظ
͍ͯ͠Δɻ https://www.ruby.or.jp/ja/news/20231031 https://bugs.ruby-lang.org/issues/19744 https://gist.github.com/tagomoris/559717c6c195f833cd15e08d4010e814 -
Namespace Separating apps/libs into isolated spaces • Load apps/libs in
a space • Hide changes from apps/libs in a namespace to other spaces • Run methods de fi ned in a space with de fi nitions in the space -
Before Namespace: Global Only All classes/modules are shared in the
entire Ruby process Ruby Process Application Code App::Func User Library Code DB::Client (v2) Library Code ActiveSupport (v7) -
Collision: 2 versions of 1 library cause errors Ruby Process
Library Code DB::Client (v3) 😵 Application Code App::Func User Library Code DB::Client (v2) Library Code ActiveSupport (v7) Before Namespace: Global Only -
Ruby Process Namespace Load apps/libs in a space Namespace Application
Code App::Func User Library Code DB::Client (v2) Library Code ActiveSupport (v7) -
Ruby Process Namespace Hide changes from apps/libs in a namespace
to other spaces Namespace Application Code App::Func User Library Code DB::Client (v2) Library Code ActiveSupport (v7) Library Code DB::Client (v3) 😀 -
Ruby Process Namespace Run methods de fi ned in a
space with de fi nitions in the space Namespace Application Code App::Func User Library Code DB::Client (v2) Library Code ActiveSupport (v7) Namespace Library Code DB::Client (v3) Namespace Application Code App::Func2 User Library Code ActiveSupport (v6) Application Code call call call -
ػೳৄࡉͷݕ౼ ݴޠػೳͱͯ͠ͷNamespaceͷઃܭ • ϝϯλʔ(·ͭͱ͞Μ)ͱٞ • NamespaceͱԿ͔? ԿͰ͋Δ͖͔? ԿͳΒ࣮ݱՄೳ͔? • ҆қʹͰ͖ͦ͏ͳ࣮ҊʹҾ͖ͣΒΕͳ͍Α͏ʹҙ͢Δඞཁ͕͋ͬͨ
• ػೳɿ Մೳੑͱ࣮ͯ͠Ͱ͖ͦ͏ͳػೳΛ࠷େݶϦετΞοϓ • ࣮ݱՄೳੑͦͷޙͰ୳Δ • είʔϓͱAPIɿ ࣮༻έʔεΛఆͯ͠Ͳ͏͋Δ͖͔Λߟ͑Δ -
ػೳৄࡉͷݕ౼ ࣮͖͢ػೳ • require/load .rb • require/load .so • autoload
• eval • proc, lambda • ΫϥεϞδϡʔϧͷఆٛ • ࠐΈΫϥεͷมߋ • include/prepend • τοϓϨϕϧϝιουఆٛ • άϩʔόϧมͷมߋɺՃ • ఆͷมߋɺՃ • ΫϥεΠϯελϯεมͷมߋɺՃ • ΫϥεมͷมߋɺՃ • ಛҟϝιουͷՃ -
ػೳৄࡉͷݕ౼ είʔϓͱAPI • NamespaceఆٛͱಡΈࠐΈͷൣғ • ϑΝΠϧ୯ҐͰͷఆٛ? ϒϩοΫͳͲଞͷέʔε? • ݱࡏͷNamespaceϨΩγΧϧʹܾఆ? ͬͱಈతʹ?
• Namespaceͷఆٛํ๏ͱӅṭείʔϓ • NamespaceࢠؔΛ࡞Δ? ࢠؔͷؒͰ෦ఆ͕ٛࢀরͰ͖Δ? • Namespaceͷ࡞ͱಡΈࠐΈͷAPI • Namespace.newɺNamespace#requireɿ ϨϕϧAPI? • Ϣʔβ͚ͷߴϨϕϧAPIͲͷΑ͏ͳͷ͔? -
ػೳৄࡉͷݕ౼ ߴϨϕϧΠϯλʔϑΣΠεͷఏҊɿPackage API • Namespace on readΛϕʔεʹͨ͠ΑΓ͍Ϣʔβ͚API • ΑΓ୯७ͳAPIͰϝϦοτΛड͚ΒΕΔΑ͏ʹ͢Δ pkg
= require_package('my-awesome-gem') MyAwesomeGem = pkg.export(:MyAwesomeGem) MyAwesomeGem.call_method() • Namespace on read͕Ϛʔδ͞ΕͨΒݕ౼Λܧଓ -
• ֦ுϥΠϒϥϦͷγϯϘϧղܾ • ಉ໊γϯϘϧΛ࣋ͭϥΠϒϥϦ͕͋Γ͏Δ • ಉϥΠϒϥϦͷҟόʔδϣϯಡΈࠐΈ • ಉҰ .so ΛෳճಡΈࠐΈ
• dlopen࣌ͷRTLD_LOCALϑϥάࢦఆ • ैདྷͱҧ͏ํ๏ͰγϯϘϧղܾ • ଞͷ֦ுϥΠϒϥϦʹґଘ͢Δ߹ͷΈ • rb_ext_resolve_symbol(fname, symname) Ruby w/ Namespace ֦ுϥΠϒϥϦͷγϯϘϧղܾ γϯϘϧղܾ༻ C API ͷՃ (Ruby 3.3) Ruby w/o Namespace db_driver_v1.so db_conn_server() query_client.so db_conn_server() db_driver_v1.so db_conn_server() query_client.so db_conn_server() db_driver_v2.so db_conn_server() ❌ rb_ext_resolve_symbol(“db_driver_v2”, “db_conn_server”) -
Namespaceͷ࣮ Namespaceͷσʔλߏ: Namespace ͱ rb_namespace_t • Namespace • ModuleͷαϒΫϥε •
Namespace#require Namespace#load • rb_namespace_t (෦࣮ɺstruct) • LOAD_PATH, LOADED_FEATURES, etc ns = Namespace.new ns.require(‘my_awesome’) ns::MyAwesome.call() -
Namespaceͷ࣮ ྫ1: Class/Moduleͷఆٛ • NamespaceͰͷClassఆٛ • MyAwesomeΛns::MyAwesomeʹ • .rbͷ߹ •
load_wrapper() ͕ղܾ • .soͷ߹ # my_awesome.rb class MyAwesome; end # my_awesome.c void Init_my_awesome(void) { rb_define_class(“MyAwesome”, rb_cObject) } # app.rb ns = Namespace.new ns.require(‘my_awesome’) ns::MyAwesome.call() -
(no NS) Namespaceͷ࣮ ྫ1: Class/Moduleͷఆٛ • NamespaceͰͷClassఆٛ • MyAwesomeΛns::MyAwesomeʹ •
.rbͷ߹ • load_wrapper() ͕ղܾ (1) • .soͷ߹ (2) require(“my_awesome.rb”) class MyAwesome; end defineclass .rb C func ISeq vm_find_or_create_class_by_id() rb_define_class_id() declare_under() vm_define_class() vm_declare_class() (1) ns.require(“my_awesome.rb”) class MyAwesome; end load_wrapper() defineclass vm_find_or_create_class_by_id() rb_define_class_id() declare_under() vm_define_class() vm_declare_class() ns:: ns:: ns:: ns:: -
Namespaceͷ࣮ ྫ1: Class/Moduleͷఆٛ • NamespaceͰͷClassఆٛ • MyAwesomeΛns::MyAwesomeʹ • .rbͷ߹ •
load_wrapper() ͕ղܾ (1) • .soͷ߹ (2) .rb C func ISeq (2) ns.require(“my_awesome.rb”) rb_define_class() rb_define_class_id_under() (no NS) require(“my_awesome.rb”) rb_define_class() ns:: -
ػೳৄࡉͷݕ౼ ࣮͖͢ػೳ (revisit) • require/load .rb • require/load .so •
autoload • eval • proc, lambda • ΫϥεϞδϡʔϧͷఆٛ • ࠐΈΫϥεͷมߋ • include/prepend • τοϓϨϕϧϝιουఆٛ • άϩʔόϧมͷมߋɺՃ • ఆͷมߋɺՃ • ΫϥεΠϯελϯεมͷมߋɺՃ • ΫϥεมͷมߋɺՃ • ಛҟϝιουͷՃ -
Namespaceͷఆٛ ྫ2: ࠐΈΫϥεఆٛͷมߋ • class String ʹΑΔӨڹൣғͷݶఆ • NamespaceͰͷΈ༗ޮʹ͍ͨ͠ •
Namespaceͷ֎Ͱແޮʹ͍ͨ͠ • ಛఆͷൣғͰͷΈ༗ޮͳϝιου → Re fi nementsͰ? # my_awesome.rb class String def blank?; false; end end class MyAwesome def self.call; “yay”.blank?; end end # app.rb ns = Namespace.new ns.require(‘my_awesome’) ns::MyAwesome.call() #=> false “yay”.blank? # NoMethodError -
Namespaceͷఆٛ ྫ2: ࠐΈΫϥεఆٛͷมߋ: Implicit Re fi nements # app.rb ns
= Namespace.new ns.require(‘my_awesome’) ns::MyAwesome.call() “yay”.blank? # NoMethodError # my_awesome.rb class String def blank?; false; end end class MyAwesome def self.call = “yay”.blank? end # my_awesome.rb using ns.refiner class String def blank?; false; end end class MyAwesome def self.call = “yay”.blank? end • Namespace෦ʹϞδϡʔϧ(re fi ner)ΛͬͯImplicit Re fi nementsʹ༻͍Δ • NamespaceͰධՁ͞ΕΔ .rb ·ͣ ҉ʹusing͞ΕΔ ಈ࡞͕ಁաతʹ ॻ͖͑ΒΕΔ෦ -
Namespaceͷఆٛ ྫ2: ࠐΈΫϥεఆٛͷมߋ: Implicit Re fi nements # app.rb ns
= Namespace.new ns.require(‘my_awesome’) ns::MyAwesome.call() “yay”.blank? # NoMethodError rb false; end me ll = “yay”.blank? # my_awesome.rb using ns.refiner module ns.refiner refine String do def blank?; false; end end end class MyAwesome def self.call = “yay”.blank? end # my_awesome.rb using ns.refiner class String def blank?; false; end end class MyAwesome def self.call = “yay”.blank? end • class StringΛ module re fi ner; re fi ne String ʹॻ͖͑ͯ Re fi nementʹͳΔΑ͏ಡΈସ͑Δ ಈ࡞͕ಁաతʹ ॻ͖͑ΒΕΔ෦ -
Namespaceͷఆٛ ྫ2: ࠐΈΫϥεఆٛͷมߋ: Implicit Re fi nements # app.rb ns
= Namespace.new ns.require(‘my_awesome’) ns::MyAwesome.call() “yay”.blank? # NoMethodError # my_awesome.rb using ns.refiner module ns.refiner refine String do def blank?; false; end end end using ns.refiner class MyAwesome def self.call = “yay”.blank? end # my_awesome.rb using ns.refiner module ns.refiner refine String do def blank?; false; end end end class MyAwesome def self.call = “yay”.blank? end y_awesome.rb ng ns.refiner ss String ef blank?; false; end ss MyAwesome ef self.call = “yay”.blank? • ߋ৽͞ΕͨRe fi nementΛ༗ޮʹ͢ΔͨΊ ͏͍ͪͲusing͢Δ ಈ࡞͕ಁաతʹ ॻ͖͑ΒΕΔ෦ -
Namespaceͷ࣮ Implicit Re fi nementsʹΑΔ࣮ߦ࣌ఆٛߋ৽ • ϝιουՃ → Implicit Re
fi nementsͰ࣮ݱ • ఆՃ → re fi nerϞδϡʔϧʹఆΛՃͯ͠ࢀরՄೳʹ • τοϓϨϕϧϝιουఆٛ → ObjectΛre fi ne͢Δ͜ͱͰରԠՄೳ -
ػೳৄࡉͷݕ౼ ࣮͖͢ػೳ (re-revisit) • require/load .rb • require/load .so •
autoload • eval • proc, lambda • ΫϥεϞδϡʔϧͷఆٛ • ࠐΈΫϥεͷมߋ • include/prepend • τοϓϨϕϧϝιουఆٛ • άϩʔόϧมͷมߋɺՃ • ఆͷมߋɺՃ • ΫϥεΠϯελϯεมͷมߋɺՃ • ΫϥεมͷมߋɺՃ • ಛҟϝιουͷՃ ϓϩδΣΫτͰ࣮ݱ͞Εͨػೳ -
Namespaceͷ࣮ Implicit Re fi nements࣮ͷ੍ͱཁվળ • include/prepend ͕࣮ݱͰ͖ͳ͍ • Ϋϥεͷಛҟϝιουఆٛͷ࣮ݱʹແཧ͕͋Δ
• ఆٛมߋͷίʔυͱݺͼग़͠Λߦ͏ίʔυͷॱংΛٯసͤ͞ΒΕͳ͍ -
ϓϩδΣΫτͷՌ Namespace on read • ࣮ݱ͖͢ػೳఏڙ͞ΕΔ͖ΠϯλʔϑΣΠεͳͲΛݕ౼ͨ͠ • ࣄલʹ࣮ݱ͓͖ͯ͘͠C APIʹ͍ͭͯRuby 3.3ͷಋೖʹޭͨ͠
• ͔ͳΓͷػೳΛ࣮ࡍʹ࣮ݱͨ͠ • ͍ͬΆ͏ɺRe fi nementsϕʔεͰͷ࣮ͷݶք໌Β͔ʹͳͬͨ -
Namespaceͷ࣮ 4݄Ҏ߱ɺRubyKaigi 2024Λӽ͑ͯ • https://speakerdeck.com/tagomoris/namespace-what-and-why • Re fi nementsϕʔεͷ࣮Λഁغ •
classext copyͱ͍͏ख๏Ͱͷ࣮ʹํΛస (RubyKaigi 2024ͷτʔΫ༰Λࢀর) • test-all ͕ɺ10 failures/errors ͱ͍͏͋ͨΓ·Ͱ࣮͞Εͭͭ͋Δ (2024/07/18) • Implicit Re fi nements ෦తʹ෮׆ͷػӡߴ·Δ -
Special Thanks to Matz Thank you for listening! The Ruby
Designer & The Mentor of this project