Skip to content

Commit f82c2a7

Browse files
committed
Implement specializations for immutable ruby objects for ObjectSpace methods
1 parent 209ceb5 commit f82c2a7

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Compatibility:
2121
* Disable most `nokogiri` C extension patches when system libraries are not being used (#2693, @aardvark179).
2222
* Implement `rb_gc_mark_maybe` and `rb_global_variable` to ensure `VALUE` stay live in C extensions (@aardvark179).
2323
* Implement `rb_imemo_tmpbuf` allocation for `ripper` (@aardvark179).
24+
* Implement specializations for immutable ruby objects for ObjectSpace methods (@bjfish).
2425

2526
Performance:
2627

spec/ruby/library/objectspace/trace_object_allocations_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,22 @@
128128
ObjectSpace.trace_object_allocations_stop
129129
end
130130
end
131+
132+
it "returns nil for class_path, generation, method_id, sourcefile, and sourceline for immutable objects" do
133+
ObjectSpace.trace_object_allocations_start
134+
begin
135+
one = nil
136+
two = 42
137+
three = :foo
138+
[one, two, three].each do |i|
139+
ObjectSpace.allocation_class_path(i).should == nil
140+
ObjectSpace.allocation_generation(i).should == nil
141+
ObjectSpace.allocation_method_id(i).should == nil
142+
ObjectSpace.allocation_sourcefile(i).should == nil
143+
ObjectSpace.allocation_sourceline(i).should == nil
144+
end
145+
ensure
146+
ObjectSpace.trace_object_allocations_stop
147+
end
148+
end
131149
end

spec/tags/library/objectspace/trace_object_allocations_tags.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ slow:ObjectSpace.trace_object_allocations can be nested with more _stop than _st
1111
slow:ObjectSpace.trace_object_allocations records info for allocation_class_path
1212
slow:ObjectSpace.trace_object_allocations records info for allocation_method_id
1313
slow:ObjectSpace.trace_object_allocations can be used without a block using trace_object_allocations_start and _stop
14+
slow:ObjectSpace.trace_object_allocations returns nil for class_path, generation, method_id, sourcefile, and sourceline for immutable objects

src/main/java/org/truffleruby/stdlib/ObjSpaceNodes.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ protected Object allocationInfo(RubyDynamicObject object,
158158
}
159159
}
160160
}
161+
162+
@Fallback
163+
protected Object allocationInfoImmutable(Object object) {
164+
return nil;
165+
}
161166
}
162167

163168
@Primitive(name = "allocation_generation")
@@ -172,6 +177,11 @@ protected Object allocationInfo(RubyDynamicObject object) {
172177
return trace.gcGeneration;
173178
}
174179
}
180+
181+
@Fallback
182+
protected Object allocationGenerationImmutable(Object object) {
183+
return nil;
184+
}
175185
}
176186

177187
@Primitive(name = "allocation_method_id")
@@ -193,6 +203,11 @@ protected Object allocationInfo(RubyDynamicObject object) {
193203
}
194204
}
195205
}
206+
207+
@Fallback
208+
protected Object allocationMethodIdImmutable(Object object) {
209+
return nil;
210+
}
196211
}
197212

198213
@Primitive(name = "allocation_sourcefile")
@@ -209,6 +224,11 @@ protected Object allocationInfo(RubyDynamicObject object,
209224
return makeStringNode.executeMake(sourcePath, Encodings.UTF_8, CodeRange.CR_UNKNOWN);
210225
}
211226
}
227+
228+
@Fallback
229+
protected Object allocationSourcefileImmutable(Object object) {
230+
return nil;
231+
}
212232
}
213233

214234
@Primitive(name = "allocation_sourceline")
@@ -223,6 +243,11 @@ protected Object allocationInfo(RubyDynamicObject object) {
223243
return trace.allocatingSourceSection.getStartLine();
224244
}
225245
}
246+
247+
@Fallback
248+
protected Object allocationSourcelineImmutable(Object object) {
249+
return nil;
250+
}
226251
}
227252

228253
@TruffleBoundary

0 commit comments

Comments
 (0)