Skip to content

Commit

Permalink
[Truffle] Add a CheckLayoutNode to cache the Shape when doing isRuby<…
Browse files Browse the repository at this point in the history
…Type> checks.

* Just an identity or a Shape check instead of (3 reads + instanceof).
  The instanceof folds away if the Shape is compilation constant
  as then the ObjecctType is compilation constant as well.
eregon committed May 13, 2016
1 parent a142e0b commit 72a1908
Showing 3 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@

import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.object.DynamicObjectFactory;
import com.oracle.truffle.api.object.ObjectType;
import com.oracle.truffle.api.object.dsl.Layout;
import org.jruby.truffle.core.basicobject.BasicObjectLayout;

@@ -23,6 +24,7 @@ DynamicObjectFactory createModuleShape(DynamicObject logicalClass,
DynamicObject createModule(DynamicObjectFactory factory,
ModuleFields fields);

boolean isModule(ObjectType objectType);
boolean isModule(DynamicObject object);
boolean isModule(Object object);

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2016 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.language;

import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.ImportStatic;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.object.ObjectType;
import com.oracle.truffle.api.object.Shape;
import org.jruby.truffle.Layouts;
import org.jruby.truffle.language.CheckLayoutNodeFactory.GetObjectTypeNodeGen;
import org.jruby.truffle.language.objects.ShapeCachingGuards;

public class CheckLayoutNode extends RubyBaseNode {

@Child GetObjectTypeNode getObjectTypeNode = GetObjectTypeNodeGen.create(null);

public boolean isArray(DynamicObject object) {
return Layouts.ARRAY.isArray(getObjectTypeNode.executeGetObjectType(object));
}

public boolean isModule(DynamicObject object) {
return Layouts.MODULE.isModule(getObjectTypeNode.executeGetObjectType(object));
}

@NodeChild("object")
@ImportStatic(ShapeCachingGuards.class)
public static abstract class GetObjectTypeNode extends RubyNode {

public abstract ObjectType executeGetObjectType(DynamicObject object);

@Specialization(
guards = { "object == cachedObject", "cachedShape.isLeaf()" },
assumptions = "cachedShape.getLeafAssumption()",
limit = "getLimit()")
ObjectType cachedLeafShapeGetObjectType(DynamicObject object,
@Cached("object") DynamicObject cachedObject,
@Cached("cachedObject.getShape()") Shape cachedShape) {
return cachedShape.getObjectType();
}

@Specialization(
guards = "object.getShape() == cachedShape",
limit = "getLimit()")
ObjectType cachedShapeGetObjectType(DynamicObject object,
@Cached("object.getShape()") Shape cachedShape) {
return cachedShape.getObjectType();
}

@Specialization(guards = "updateShape(object)")
ObjectType updateShapeAndRetry(DynamicObject object) {
return executeGetObjectType(object);
}

@Specialization(contains = { "cachedLeafShapeGetObjectType", "cachedShapeGetObjectType", "updateShapeAndRetry" })
ObjectType uncachedGetObjectType(DynamicObject object) {
return object.getShape().getObjectType();
}

protected int getLimit() {
return getContext().getOptions().IS_A_CACHE;
}

}

}
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@
import org.jruby.truffle.Layouts;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.module.ModuleOperations;
import org.jruby.truffle.language.CheckLayoutNode;
import org.jruby.truffle.language.LexicalScope;
import org.jruby.truffle.language.RubyConstant;
import org.jruby.truffle.language.RubyNode;
@@ -41,6 +42,8 @@ public abstract class LookupConstantNode extends RubyNode {
private final boolean ignoreVisibility;
private final boolean lookInObject;

@Child CheckLayoutNode checkLayoutNode = new CheckLayoutNode();

public LookupConstantNode(RubyContext context, SourceSection sourceSection, boolean ignoreVisibility, boolean lookInObject) {
super(context, sourceSection);
this.ignoreVisibility = ignoreVisibility;
@@ -99,6 +102,10 @@ protected boolean guardName(String name, String cachedName, ConditionProfile sam
}
}

protected boolean isRubyModule(DynamicObject module) {
return checkLayoutNode.isModule(module);
}

protected RubyConstant doLookup(DynamicObject module, String name) {
if (lookInObject) {
return ModuleOperations.lookupConstantAndObject(getContext(), module, name);

3 comments on commit 72a1908

@eregon
Copy link
Member Author

@eregon eregon commented on 72a1908 May 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jruby/truffle @thomaswue @woess This is the cache for ObjectType checks, pretty nice overall!

@chrisseaton
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, but how do you scale this up to use it? We have tons of isRubyX checks. Do we need to write @Cached parameters to get a node for each one? Add a node to every RubyBaseNode? Something else?

@eregon
Copy link
Member Author

@eregon eregon commented on 72a1908 May 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can start by adding it where it's performance sensitive.
Not all core methods need these checks.

Another idea if we want a simple monomorphic cache on arguments shapes would be to profile the Shape in the per-argument profile when it is a DynamicObject. This should allow the ObjectType to fold but needs to be verified (it would not work for this constant lookup case since this is not a call with profiles).

Please sign in to comment.