Skip to content
Permalink
Browse files
[Truffle] Store object_id as a dynamic object field so it doesn't tak…
…e up any space unless it's set.
  • Loading branch information
chrisseaton committed Dec 3, 2014
1 parent 7a21fc4 commit e5247d3f847f00de54a148bb3a420dc489349993
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.runtime;

public class InternalName {

private final String name;

public InternalName(String name) {
this.name = name;
}

@Override
public String toString() {
return String.format("internal(%s)", name);
}

}
@@ -55,7 +55,9 @@ public Map<String,Object> getInstanceVariables(RubyBasicObject receiver) {
Map<String, Object> vars = new LinkedHashMap<>();
List<Property> properties = shape.getPropertyList();
for (Property property : properties) {
vars.put((String) property.getKey(), property.get(receiver.getDynamicObject(), false));
if (property.getKey() != RubyBasicObject.OBJECT_ID_IDENTIFIER) {
vars.put((String) property.getKey(), property.get(receiver.getDynamicObject(), false));
}
}
return vars;
}
@@ -15,8 +15,10 @@
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.object.Layout;
import com.oracle.truffle.api.object.Property;
import com.oracle.truffle.api.object.Shape;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.runtime.InternalName;
import org.jruby.truffle.runtime.ModuleOperations;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.RubyOperations;
@@ -30,6 +32,8 @@
*/
public class RubyBasicObject {

public static final InternalName OBJECT_ID_IDENTIFIER = new InternalName("object_id");

public static Layout LAYOUT = Layout.createLayout(Layout.INT_TO_LONG);

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

protected long objectID = -1;

private boolean frozen = false;

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

@CompilerDirectives.TruffleBoundary
public long getObjectID() {
if (objectID == -1) {
objectID = getContext().getNextObjectID();
// TODO(CS): we should specialise on reading this in the #object_id method and anywhere else it's used
Property property = dynamicObject.getShape().getProperty(OBJECT_ID_IDENTIFIER);

if (property != null) {
return (long) property.get(dynamicObject, false);
}

final long objectID = getContext().getNextObjectID();
dynamicObject.define(OBJECT_ID_IDENTIFIER, objectID, 0);
return objectID;
}

0 comments on commit e5247d3

Please sign in to comment.