1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
# Namespace - Ruby's in-process separation of Classes and Modules
Namespace is designed to provide separated spaces in a Ruby process, to isolate applications and libraries.
## Known issues
* Experimental warning is shown when ruby starts with `RUBY_NAMESPACE=1` (specify `-W:no-experimental` option to hide it)
* `bundle install` may fail
* `require 'active_support'` may fail
* A wrong current namespace detection happens sometimes in the root namespace
## TODOs
* Identify the CI failure cause and restore temporarily skipped tests (mmtk, test/ruby/test_allocation on i686)
* Reconstruct current/loading namespace management based on control frames
* Add the loaded namespace on iseq to check if another namespace tries running the iseq (add a field only when VM_CHECK_MODE?)
* Delete per-namespace extension files (.so) lazily or process exit
* Collect rb_classext_t entries for a namespace when the namespace is collected
* Allocate an rb_namespace_t entry as the root namespace at first, then construct the contents and wrap it as rb_cNamespace instance later (to eliminate root/builtin two namespaces situation)
* Assign its own TOPLEVEL_BINDING in namespaces
* Fix `warn` in namespaces to refer `$VERBOSE` in the namespace
* Make an internal data container `Namespace::Entry` invisible
* More test cases about `$LOAD_PATH` and `$LOADED_FEATURES`
* Return classpath and nesting without the namespace prefix in the namespace itself [#21316](https://bugs.ruby-lang.org/issues/21316), [#21318](https://bugs.ruby-lang.org/issues/21318)
## How to use
### Enabling namespace
First, an environment variable should be set at the ruby process bootup: `RUBY_NAMESPACE=1`.
The only valid value is `1` to enable namespace. Other values (or unset `RUBY_NAMESPACE`) means disabling namespace. And setting the value after Ruby program starts doesn't work.
### Using namespace
`Namespace` class is the entrypoint of namespaces.
```ruby
ns = Namespace.new
ns.require('something') # or require_relative, load
```
The required file (either .rb or .so/.dll/.bundle) is loaded in the namespace (`ns` here). The required/loaded files from `something` will be loaded in the namespace recursively.
```ruby
# something.rb
X = 1
class Something
def self.x = X
def x = ::X
end
```
Classes/modules, those methods and constants defined in the namespace can be accessed via `ns` object.
```ruby
p ns::Something.x # 1
X = 2
p X # 2
p ::X # 2
p ns::Something.x # 1
p ns::X # 1
```
Instance methods defined in the namespace also run with definitions in the namespace.
```ruby
s = ns::Something.new
p s.x # 1
```
## Specifications
### Namespace types
There are two namespace types:
* Root namespace
* User namespace
There is the root namespace, just a single namespace in a Ruby process. Ruby bootstrap runs in the root namespace, and all builtin classes/modules are defined in the root namespace. (See "Builtin classes and modules".)
User namespaces are to run user-written programs and libraries loaded from user programs. The user's main program (specified by the `ruby` command line argument) is executed in the "main" namespace, which is a user namespace automatically created at the end of Ruby's bootstrap, copied from the root namespace.
When `Namespace.new` is called, an "optional" namespace (a user, non-main namespace) is created, copied from the root namespace. All user namespaces are flat, copied from the root namespace.
### Namespace class and instances
`Namespace` is a top level class, as a subclass of `Module`, and `Namespace` instances are a kind of `Module`.
### Classes and modules defined in namespace
The classes and modules, newly defined in a namespace `ns`, are defined under `ns`. For example, if a class `A` is defined in `ns`, it is actually defined as `ns::A`.
In the namespace `ns`, `ns::A` can be referred to as `A` (and `::A`). From outside of `ns`, it can be referred to as `ns::A`.
The main namespace is exceptional. Top level classes and modules defined in the main namespace are just top level classes and modules.
### Classes and modules reopened in namespace
In namespaces, builtin classes/modules are visible and can be reopened. Those classes/modules can be reopened using `class` or `module` clauses, and class/module definitions can be changed.
The changed definitions are visible only in the namespace. In other namespaces, builtin classes/modules and those instances work without changed definitions.
```ruby
# in foo.rb
class String
BLANK_PATTERN = /\A\s*\z/
def blank?
self =~ BLANK_PATTERN
end
end
module Foo
def self.foo = "foo"
def self.foo_is_blank?
foo.blank?
end
end
Foo.foo.blank? #=> false
"foo".blank? #=> false
# in main.rb
ns = Namespace.new
ns.require('foo')
Foo.foo_is_blank? #=> false (#blank? called in ns)
Foo.foo.blank? # NoMethodError
"foo".blank? # NoMethodError
String::BLANK_PATTERN # NameError
```
The main namespace and `ns` are different namespaces, so monkey patches in main are also invisible in `ns`.
### Builtin classes and modules
In the namespace context, "builtin" classes and modules are classes and modules:
* Accessible without any `require` calls in user scripts
* Defined before any user program start running
* Including classes/modules loaded by `prelude.rb` (including RubyGems `Gem`, for example)
Hereafter, "builtin classes and modules" will be referred to as just "builtin classes".
### Builtin classes referred via namespace objects
Builtin classes in a namespace `ns` can be referred from other namespace. For example, `ns::String` is a valid reference, and `String` and `ns::String` are identical (`String == ns::String`, `String.object_id == ns::String.object_id`).
`ns::String`-like reference returns just a `String` in the current namespace, so its definition is `String` in the namespace, not in `ns`.
```ruby
# foo.rb
class String
def self.foo = "foo"
end
# main.rb
ns = Namespace.new
ns.require('foo')
ns::String.foo # NoMethodError
```
### Class instance variables, class variables, constants
Builtin classes can have different sets of class instance variables, class variables and constants between namespaces.
```ruby
# foo.rb
class Array
@v = "foo"
@@v = "_foo_"
V = "FOO"
end
Array.instance_variable_get(:@v) #=> "foo"
Array.class_variable_get(:@@v) #=> "_foo_"
Array.const_get(:V) #=> "FOO"
# main.rb
ns = Namespace.new
ns.require('foo')
Array.instance_variable_get(:@v) #=> nil
Array.class_variable_get(:@@v) # NameError
Array.const_get(:V) # NameError
```
### Global variables
In namespaces, changes on global variables are also isolated in the namespace. Changes on global variables in a namespace are visible/applied only in the namespace.
```ruby
# foo.rb
$foo = "foo"
$VERBOSE = nil
puts "This appears: '#{$foo}'"
# main.rb
p $foo #=> nil
p $VERBOSE #=> false
ns = Namespace.new
ns.require('foo') # "This appears: 'foo'"
p $foo #=> nil
p $VERBOSE #=> false
```
### Top level constants
Usually, top level constants are defined as constants of `Object`. In namespaces, top level constants are constants of `Object` in the namespace. And the namespace object `ns`'s constants are strictly equal to constants of `Object`.
```ruby
# foo.rb
FOO = 100
FOO #=> 100
Object::FOO #=> 100
# main.rb
ns = Namespace.new
ns.require('foo')
ns::FOO #=> 100
FOO # NameError
Object::FOO # NameError
```
### Top level methods
Top level methods are private instance methods of `Object`, in each namespace.
```ruby
# foo.rb
def yay = "foo"
class Foo
def self.say = yay
end
Foo.say #=> "foo"
yay #=> "foo"
# main.rb
ns = Namespace.new
ns.require('foo')
ns.Foo.say #=> "foo"
yay # NoMethodError
```
There is no way to expose top level methods in namespaces to another namespace.
(See "Expose top level methods as a method of the namespace object" in "Discussions" section below)
### Namespace scopes
Namespace works in file scope. One `.rb` file runs in a single namespace.
Once a file is loaded in a namespace `ns`, all methods/procs defined/created in the file run in `ns`.
## Implementation details
#### Object Shapes
Once builtin classes are copied and modified in namespaces, its instance variable management fallbacks from Object Shapes to a traditional iv table (st_table) because RClass stores the shape in its `flags`, not in `rb_classext_t`.
#### Size of RClass and rb_classext_t
Namespace requires to move some fields from RClass to `rb_classext_t`, then the size of RClass and `rb_classext_t` is now larger than `4 * RVALUE_SIZE`. It's against the expectation of [Variable Width Allocation](https://rubykaigi.org/2021-takeout/presentations/peterzhu2118.html).
Now the `STATIC_ASSERT` to check the size is commented-out. (See "Minimize the size of RClass and rb_classext_t" in "Discussion" section below)
#### ISeq inline method/constant cache
As described above in "Namespace scopes", an ".rb" file runs in a namespace. So method/constant resolution will be done in a namespace consistently.
That means ISeq inline caches work well even with namespaces. Otherwise, it's a bug.
#### Method call global cache (gccct)
`rb_funcall()` C function refers to the global cc cache table (gccct), and the cache key is calculated with the current namespace.
So, `rb_funcall()` calls have a performance penalty when namespace is enabled.
#### Current namespace and loading namespace
The current namespace is the namespace that the executing code is in. `Namespace.current` returns the current namespace object.
The loading namespace is an internally managed namespace to determine the namespace to load newly required/loaded files. For example, `ns` is the loading namespace when `ns.require("foo")` is called.
## Discussions
#### Namespace#inspect
Currently, `Namespace#inspect` returns values like `"#<Namespace:0x00000001083a5660>"`. This results in the very redundant and poorly visible classpath outside the namespace.
```ruby
# foo.rb
class C; end
# main.rb
ns = Namespace.new
ns.require('foo')
p ns::C # "#<Namespace:0x00000001083a5660>::C"
```
And currently, if a namespace is assigned to a constant `NS1`, the classpath output will be `NS1::C`. But the namespace object can be brought to another namespace and the constant `NS1` in the namespace is something different. So the constant-based classpath for namespace is not safe basically.
So we should find a better format to show namespaces. Options are:
* `NS1::C` (only when this namespace is created and assigned to NS1 in the current namespace)
* `#<Namespace:user:1083a5660>::C` (with namespace type and without preceding 0)
* or something else
#### Namespace#eval
Testing namespace features needs to create files to be loaded in namespaces. It's not easy nor casual.
If `Namespace` class has an instance method `#eval` to evaluate code in the namespace, it can be helpful.
#### More builtin methods written in Ruby
If namespace is enabled by default, builtin methods can be written in Ruby because it can't be overridden by users' monkey patches. Builtin Ruby methods can be JIT-ed, and it could bring performance reward.
#### Monkey patching methods called by builtin methods
Builtin methods sometimes call other builtin methods. For example, `Hash#map` calls `Hash#each` to retrieve entries to be mapped. Without namespace, Ruby users can overwrite `Hash#each` and expect the behavior change of `Hash#map` as a result.
But with namespaces, `Hash#map` runs in the root namespace. Ruby users can define `Hash#each` only in user namespaces, so users cannot change `Hash#map`'s behavior in this case. To achieve it, users should override both`Hash#map` and `Hash#each` (or only `Hash#map`).
It is a breaking change.
It's an option to change the behavior of methods in the root namespace to refer to definitions in user namespaces. But if we do so, that means we can't proceed with "More builtin methods written in Ruby".
#### Context of \$LOAD\_PATH and \$LOADED\_FEATURES
Global variables `$LOAD_PATH` and `$LOADED_FEATURES` control `require` method behaviors. So those namespaces are determined by the loading namespace instead of the current namespace.
This could potentially conflict with the user's expectations. We should find the solution.
#### Expose top level methods as a method of the namespace object
Currently, top level methods in namespaces are not accessible from outside of the namespace. But there might be a use case to call other namespace's top level methods.
#### Split root and builtin namespace
NOTE: "builtin" namespace is a different one from the "builtin" namespace in the current implementation
Currently, the single "root" namespace is the source of classext CoW. And also, the "root" namespace can load additional files after starting main script evaluation by calling methods which contain lines like `require "openssl"`.
That means, user namespaces can have different sets of definitions according to when it is created.
```
[root]
|
|----[main]
|
|(require "openssl" called in root)
|
|----[ns1] having OpenSSL
|
|(remove_const called for OpenSSL in root)
|
|----[ns2] without OpenSSL
```
This could cause unexpected behavior differences between user namespaces. It should NOT be a problem because user scripts which refer to `OpenSSL` should call `require "openssl"` by themselves.
But in the worst case, a script (without `require "openssl"`) runs well in `ns1`, but doesn't run in `ns2`. This situation looks like a "random failure" to users.
An option possible to prevent this situation is to have "root" and "builtin" namespaces.
* root
* The namespace for the Ruby process bootstrap, then the source of CoW
* After starting the main namespace, no code runs in this namespace
* builtin
* The namespace copied from the root namespace at the same time with "main"
* Methods and procs defined in the "root" namespace run in this namespace
* Classes and modules required will be loaded in this namespace
This design realizes a consistent source of namespace CoW.
#### Separate cc_tbl and callable_m_tbl, cvc_tbl for less classext CoW
The fields of `rb_classext_t` contains several cache(-like) data, `cc_tbl`(callcache table), `callable_m_tbl`(table of resolved complemented methods) and `cvc_tbl`(class variable cache table).
The classext CoW is triggered when the contents of `rb_classext_t` are changed, including `cc_tbl`, `callable_m_tbl`, and `cvc_tbl`. But those three tables are changed by just calling methods or referring class variables. So, currently, classext CoW is triggered much more times than the original expectation.
If we can move those three tables outside of `rb_classext_t`, the number of copied `rb_classext_t` will be much less than the current implementation.
#### Object Shapes per namespace
Now the classext CoW requires RClass and `rb_classext_t` to fallback its instance variable management from Object Shapes to the traditional `st_table`. It may have a performance penalty.
If we can apply Object Shapes on `rb_classext_t` instead of `RClass`, per-namespace classext can have its own shapes, and it may be able to avoid the performance penalty.
#### Minimize the size of RClass and rb_classext_t
As described in "Size of RClass and rb_classext_t" section above, the size of RClass and `rb_classext_t` is currently larger than `4 * RVALUE_SIZE` (`20 * VALUE_SIZE`). Now the size is `23 * VALUE_SIZE + 7 bits`.
The fields possibly removed from `rb_classext_t` are:
* `cc_tbl`, `callable_m_tbl`, `cvc_tbl` (See the section "Separate cc_tbl and callable_m_tbl, cvc_tbl for less classext CoW" above)
* `ns_super_subclasses`, `module_super_subclasses`
* `RCLASSEXT_SUBCLASSES(RCLASS_EXT_PRIME(RCLASSEXT_SUPER(klass)))->ns_subclasses` can replace it
* These fields are used only in GC, how's the actual performance benefit?
If we can move or remove those fields, the size satisfies the assertion (`<= 4 * RVALUE_SIZE`).
|