Skip to content

Commit

Permalink
[Truffle] Store object_id as a dynamic object field so it doesn't tak…
Browse files Browse the repository at this point in the history
…e up any space unless it's set.
  • Loading branch information
chrisseaton committed Dec 3, 2014
1 parent 7a21fc4 commit e5247d3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
25 changes: 25 additions & 0 deletions core/src/main/java/org/jruby/truffle/runtime/InternalName.java
@@ -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);
}

}
Expand Up @@ -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;
}
Expand Down
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit e5247d3

Please sign in to comment.