$30 off During Our Annual Pro Sale. View Details »

Help Collisions, Isolate the Worlds

Help Collisions, Isolate the Worlds

The talk about https://bugs.ruby-lang.org/issues/19744
Why and how we will (not) introduce namespaces in Ruby
松江Ruby会議10

Satoshi Tagomori

September 16, 2023
Tweet

More Decks by Satoshi Tagomori

Other Decks in Programming

Transcript

  1. Satoshi Tagomori a.k.a., @tagomoris
    দߐRubyձٞ10 - Sep 16, 2023
    Help Collisions,
    Isolate the Worlds
    Names, Modules, Versions, and NameSpaces

    View Slide

  2. Satoshi Tagomori (@tagomoris)
    Independent developer

    Technical Consultant

    OSS Maintainer

    • Fluentd

    • MessagePack / msgpack-ruby

    • Norikra, Woothee, …
    tagomoris

    View Slide

  3. https://www.
    fl
    ickr.com/photos/takkanm/3978417669
    Asakusa.rb

    View Slide

  4. “Namespace on read“


    https://bugs.ruby-lang.org/issues/19744

    View Slide

  5. Collisions
    Destroy Developers’ Experience

    View Slide

  6. Collisions
    Names, Modules, Versions
    Name Collisions:

    No one can use a name in two ways.

    Module Collisions:

    Modules can be modified, globally, from anywhere.

    Version Collisions:

    Two different versions of a module can’t be loaded.

    View Slide

  7. Name Collisions
    No one can use a name in two ways.
    • A structured namespace:

    • Foo::Bar::Baz

    • Major scenario:

    • Top-level “Configuration” in gems

    • Typical RoR classes vs Ruby, Gems, etc (like “Guild”)

    • Two apps in a process (in the context of “Modular monolith”)

    View Slide

  8. Module Collisions
    Modules can be modi
    fi
    ed, globally, from anywhere.
    • A single Module/Class instance

    • Oj.default_options

    View Slide

  9. Version Collisions
    Two di
    ff
    erent versions of a library can’t be loaded.
    • Dependency hell

    • Module A depends on C ver 1.5.x

    • Module B depends on C ver 1.6.x

    • The App depends on A and B

    View Slide

  10. Avoid Collisions

    View Slide

  11. Isolate Worlds

    View Slide

  12. NameSpaces
    In other words: “Multiverse Ruby”
    https://rubykaigi.org/2023/presentations/shioyama.html#day2

    View Slide

  13. NameSpaces in Practice
    N languages, N ways
    • Library space

    • Global NameSpace (+ Version)

    • Local names only

    • Names in code

    • Library name

    • Local alias

    • Class loader

    View Slide

  14. NameSpaces in languages
    Many ways, Many hells…
    • JavaScript/TypeScript (npm)

    • Local names + Local Aliases

    • Java (classes w/ FQDN)

    • Global NameSpace + Library name

    • Class loaders (oops….)

    • Kotlin

    • Global NameSpace + Local Alias

    • Python (pip)

    • Global NameSpace (+ Local Alias)

    • Go

    • Local names + Local Aliases

    View Slide

  15. Ruby
    Many Libraries in a Global NameSpace

    View Slide

  16. Libraries and NameSpaces
    Libraries can’t be re-de
    fi
    ned
    • Once a library lives globally, it can’t be locally

    • Ruby has a huge library set - RubyGems

    View Slide

  17. NameSpace Requirements
    What is it?
    • One native extension libraries of two versions at a time

    • Same name, Di
    ff
    erent (native) code, Di
    ff
    erent access path (local name)

    • Isolate libraries in speci
    fi
    ed namespaces at the required time (“on read”)
    Ruby Process
    LibraryX


    version: a.b.c


    “x.so”
    LibraryX


    version: x.y.z


    “x.so”
    ns1::X ns2::X

    View Slide

  18. NameSpace Requirements
    What is it?
    • One native extension libraries of two versions at a time

    • Same name, Di
    ff
    erent (native) code, Di
    ff
    erent access path (local name)

    • Isolate libraries in speci
    fi
    ed namespaces at the required time (“on read”)
    Ruby Process
    LibraryX


    version: a.b.c


    “x.so”
    LibraryX


    version: x.y.z


    “x.so”
    ns1::X X

    View Slide

  19. My Proposal: module NameSpace < Module
    Respects load(
    fi
    lename, wrap)
    • The wrap of load(filename, wrap):

    • works as a simple namespace

    • Introduce a sub-class of Module: NameSpace

    • works like the wrap of load

    • has instance methods #require, #load

    • Dependencies will be required/loaded in the namespace recursively

    View Slide

  20. Let’s go!
    DEMO

    View Slide

  21. 🎉👏🍻
    “Namespace on read PoC”

    https://github.com/tagomoris/ruby/pull/1

    View Slide

  22. Hard Things, TODOs, Designs, …
    “How This Language Should Be?”
    • Hard Things: Native extension MAY need to be updated, in case

    • Native extensions using others’ C-functions

    https://github.com/tagomoris/sequel_pg/pull/1

    • TODOs: Upgrade path SHOULD be de
    fi
    ned

    • How can we mark a gem is namespace-ready?

    • How can we determine the namespace of a library? (RubyGems/Bundler)

    • Designs

    • What’s the namespace in Ruby?

    • Problems around the top-level names in namespaces

    View Slide

  23. Top-level Names in NameSpaces
    Should it be global? Or local?
    • “ns1::X” - A top-level name “X” de
    fi
    ned in a namespace “ns1”

    • It MUST be so, to isolate “ns1::X” from the “X”

    • “X” in the code isolated in “ns1”

    • It should be “ns1::X”, of course

    • But many gems add methods on “String” by “class String”…

    • “::X” in the code isolated in “ns1”

    • Should it be “ns1::X”? or the “X”?

    • “class ::String” should be possible

    View Slide

  24. The TODOs
    I need discussions
    • To Go? Or No Go?

    • In this way? Or another way? Or NO WAY?

    • Which way to go?

    • How can we break codes IN NAMESPACES? (Not globally)

    View Slide

  25. I NEED
    DISCUSSIONS
    For further developments.
    Thank you!

    View Slide