CARVIEW |
Select Language
HTTP/2 200
x-amz-id-2: dphARL4kG5obSpTNCQ6mM2xT/PZ6h8qymLyUhJz9qE/i8YiFEwcR5gmuvrnmV4LT7fcds/m0nCo=
x-amz-request-id: H665ZQJ2S2NMQWMQ
last-modified: Thu, 22 May 2025 20:41:09 GMT
etag: "4ed545184fdb8592900941632fa8cc07"
x-amz-server-side-encryption: AES256
server: AmazonS3
content-encoding: gzip
via: 1.1 varnish, 1.1 varnish
content-type: text/plain; charset=utf-8
accept-ranges: bytes
age: 0
date: Thu, 24 Jul 2025 06:00:09 GMT
x-served-by: cache-tyo11920-TYO, cache-bom-vanm7210053-BOM
x-cache: MISS, MISS
x-cache-hits: 0, 0
x-timer: S1753336810.579317,VS0,VE301
vary: Accept-Encoding
content-length: 1285
From: "jhawthorn (John Hawthorn) via ruby-core"
Date: 2025-05-22T20:34:43+00:00
Subject: [ruby-core:122238] [Ruby Bug#21362] Namespace: Inline method caches poisoned with builtins
Issue #21362 has been reported by jhawthorn (John Hawthorn).
----------------------------------------
Bug #21362: Namespace: Inline method caches poisoned with builtins
https://bugs.ruby-lang.org/issues/21362
* Author: jhawthorn (John Hawthorn)
* Status: Open
* Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
``` ruby
File.write("/tmp/ntest.rb", <<~'RUBY')
class Integer
def succ = self + 2
end
module Test
def self.run = 10.times.to_a
end
RUBY
module Test
def self.run = 10.times.to_a
end
ns = Namespace.new
ns.require("/tmp/ntest.rb")
p namespaced: ns::Test.run
p main: Test.run
```
```
RUBY_NAMESPACE=1 ruby test_namespace_succ.rb
ruby: warning: Namespace is experimental, and the behavior may change in the future!
See doc/namespace.md for known issues, etc.
{namespaced: [0, 2, 4, 6, 8]}
{main: [0, 2, 4, 6, 8]}
```
What's happening here is that we have a number of "builtin" Ruby files which are loaded during the VM boot. The instruction sequences and method definitions from these files end up as part of the "root" namespace. However this iseq can still include inline caches, and in this example we see the cache poisoned by being run in a namespace with succ redefined. I was surprised by this, I expected it to call using the "root" namespace.
It's definitely a strange edge-case to redefine `succ`, but the namespace semantics here are important because how this is defined impacts how useful builtins can be:
- If builtins inherit their namespace from the caller, then they cannot use inline caches (or inline caches must always check for namespace validity) and also cannot be JIT compiled (unless the JIT also checks for namespace validity, and given the complexity of rb_current_namespace, it would be very undesirable). This is very bad as converting C code to builtin Ruby has been important for getting better performance.
- If builtins make calls using the "root" namespace, inline caching and the JIT should work (actually there may be advantages to both since the namespace is mostly immutable). However this will limit what we are able to move from C to Ruby, as it wouldn't be possible to have the same semantics as C methods (where the caller's namespace is used).
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/