Skip to content

Commit e5247d3

Browse files
committed
[Truffle] Store object_id as a dynamic object field so it doesn't take up any space unless it's set.
1 parent 7a21fc4 commit e5247d3

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. This
3+
* code is released under a tri EPL/GPL/LGPL license. You can use it,
4+
* redistribute it and/or modify it under the terms of the:
5+
*
6+
* Eclipse Public License version 1.0
7+
* GNU General Public License version 2
8+
* GNU Lesser General Public License version 2.1
9+
*/
10+
package org.jruby.truffle.runtime;
11+
12+
public class InternalName {
13+
14+
private final String name;
15+
16+
public InternalName(String name) {
17+
this.name = name;
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return String.format("internal(%s)", name);
23+
}
24+
25+
}

core/src/main/java/org/jruby/truffle/runtime/RubyOperations.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ public Map<String,Object> getInstanceVariables(RubyBasicObject receiver) {
5555
Map<String, Object> vars = new LinkedHashMap<>();
5656
List<Property> properties = shape.getPropertyList();
5757
for (Property property : properties) {
58-
vars.put((String) property.getKey(), property.get(receiver.getDynamicObject(), false));
58+
if (property.getKey() != RubyBasicObject.OBJECT_ID_IDENTIFIER) {
59+
vars.put((String) property.getKey(), property.get(receiver.getDynamicObject(), false));
60+
}
5961
}
6062
return vars;
6163
}

core/src/main/java/org/jruby/truffle/runtime/core/RubyBasicObject.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
import com.oracle.truffle.api.nodes.Node;
1616
import com.oracle.truffle.api.object.DynamicObject;
1717
import com.oracle.truffle.api.object.Layout;
18+
import com.oracle.truffle.api.object.Property;
1819
import com.oracle.truffle.api.object.Shape;
1920
import org.jruby.truffle.nodes.RubyNode;
21+
import org.jruby.truffle.runtime.InternalName;
2022
import org.jruby.truffle.runtime.ModuleOperations;
2123
import org.jruby.truffle.runtime.RubyContext;
2224
import org.jruby.truffle.runtime.RubyOperations;
@@ -30,6 +32,8 @@
3032
*/
3133
public class RubyBasicObject {
3234

35+
public static final InternalName OBJECT_ID_IDENTIFIER = new InternalName("object_id");
36+
3337
public static Layout LAYOUT = Layout.createLayout(Layout.INT_TO_LONG);
3438

3539
private final DynamicObject dynamicObject;
@@ -39,8 +43,6 @@ public class RubyBasicObject {
3943
/** Either the singleton class if it exists or the logicalClass. */
4044
@CompilationFinal protected RubyClass metaClass;
4145

42-
protected long objectID = -1;
43-
4446
private boolean frozen = false;
4547

4648
public RubyBasicObject(RubyClass rubyClass) {
@@ -112,10 +114,15 @@ public void setInstanceVariable(String name, Object value) {
112114

113115
@CompilerDirectives.TruffleBoundary
114116
public long getObjectID() {
115-
if (objectID == -1) {
116-
objectID = getContext().getNextObjectID();
117+
// TODO(CS): we should specialise on reading this in the #object_id method and anywhere else it's used
118+
Property property = dynamicObject.getShape().getProperty(OBJECT_ID_IDENTIFIER);
119+
120+
if (property != null) {
121+
return (long) property.get(dynamicObject, false);
117122
}
118123

124+
final long objectID = getContext().getNextObjectID();
125+
dynamicObject.define(OBJECT_ID_IDENTIFIER, objectID, 0);
119126
return objectID;
120127
}
121128

0 commit comments

Comments
 (0)