Skip to content

Commit

Permalink
Start moving shape reference into shaped objects
Browse files Browse the repository at this point in the history
In order to evolve the shape of objects as more instance variables
are discovered, we need to be able to access  multiple different
shaped instances with their own layouts. Previously, only one
shape could be associated with a given class, based on a static
inspection of all instance variable accesses in the class's
hierarchy. In order to keep stale-shaped objects functional, this
commit adds a shape reference to all shaped RubyObject subtypes.
With this we can allocate a first object using no instance vars
(falling back on the default varTable layout) and as instance vars
are encountered modify allocation to create wider object shapes.

The step here simply adds the shape reference to all shaped
objects; evolving that shape and updating the allocator will come
in future commits.
  • Loading branch information
headius committed Dec 8, 2022
1 parent ba72707 commit bbdc90b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 12 deletions.
26 changes: 15 additions & 11 deletions core/src/main/java/org/jruby/RubyBasicObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@ public static RubyClass getMetaClass(IRubyObject arg) {
return ((RubyBasicObject) arg).metaClass;
}

public VariableTableManager getShape() {
return metaClass.getVariableTableManager();
}

/** rb_singleton_class
*
* Note: this method is specialized for RubyFixnum, RubySymbol,
Expand Down Expand Up @@ -1045,7 +1049,7 @@ public IRubyObject id() {
* object is frozen.
*/
protected long getObjectId() {
return metaClass.getRealClass().getVariableTableManager().getObjectId(this);
return getShape().getObjectId(this);
}

/** rb_obj_inspect
Expand Down Expand Up @@ -1123,7 +1127,7 @@ private RubyString inspectObj(final Ruby runtime, RubyString part) {
final ThreadContext context = runtime.getCurrentContext();

boolean first = true;
for (Map.Entry<String, VariableAccessor> entry : metaClass.getVariableTableManager().getVariableAccessorsForRead().entrySet()) {
for (Map.Entry<String, VariableAccessor> entry : getShape().getVariableAccessorsForRead().entrySet()) {
Object value = entry.getValue().get(this);
if (!(value instanceof IRubyObject)) continue;
RubySymbol symbol = runtime.newSymbol(entry.getKey());
Expand Down Expand Up @@ -1281,15 +1285,15 @@ public Object getVariable(int index) {
public void setVariable(int index, Object value) {
ensureInstanceVariablesSettable();
if (index < 0) return;
metaClass.getVariableTableManager().setVariableInternal(this, index, value);
getShape().setVariableInternal(this, index, value);
}

public final Object getFFIHandle() {
return metaClass.getVariableTableManager().getFFIHandle(this);
return getShape().getFFIHandle(this);
}

public final void setFFIHandle(Object value) {
metaClass.getVariableTableManager().setFFIHandle(this, value);
getShape().setFFIHandle(this, value);
}

//
Expand All @@ -1303,7 +1307,7 @@ public final void setFFIHandle(Object value) {
*/
@Override
public boolean hasVariables() {
return metaClass.getVariableTableManager().hasVariables(this);
return getShape().hasVariables(this);
}

/**
Expand All @@ -1313,7 +1317,7 @@ public boolean hasVariables() {
* @return true if there are set instance variables, false otherwise
*/
protected boolean hasInstanceVariables() {
return metaClass.getVariableTableManager().hasInstanceVariables(this);
return getShape().hasInstanceVariables(this);
}

/**
Expand Down Expand Up @@ -1376,7 +1380,7 @@ protected Object variableTableStore(String name, Object value) {
* table, and returning the removed value.
*/
protected Object variableTableRemove(String name) {
return metaClass.getVariableTableManager().clearVariable(this, name);
return getShape().clearVariable(this, name);
}

/**
Expand Down Expand Up @@ -1447,7 +1451,7 @@ public Object removeInternalVariable(String name) {
*/
@Override
public void syncVariables(IRubyObject other) {
metaClass.getVariableTableManager().syncVariables(this, other);
getShape().syncVariables(this, other);
}

//
Expand Down Expand Up @@ -2956,7 +2960,7 @@ private void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject();
oos.writeUTF(metaClass.getName());

metaClass.getVariableTableManager().serializeVariables(this, oos);
getShape().serializeVariables(this, oos);
}

/**
Expand All @@ -2982,7 +2986,7 @@ private void readObject(ObjectInputStream ois) throws IOException, ClassNotFound
ois.defaultReadObject();
metaClass = (RubyClass)ruby.getClassFromPath(ois.readUTF());

metaClass.getVariableTableManager().deserializeVariables(this, ois);
getShape().deserializeVariables(this, ois);
}

/**
Expand Down
58 changes: 58 additions & 0 deletions core/src/main/java/org/jruby/RubyObjectShaped.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
***** BEGIN LICENSE BLOCK *****
* Version: EPL 2.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v20.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2001 Chad Fowler <chadfowler@chadfowler.com>
* Copyright (C) 2001 Alan Moore <alan_moore@gmx.net>
* Copyright (C) 2001-2002 Benoit Cerrina <b.cerrina@wanadoo.fr>
* Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
* Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
* Copyright (C) 2004-2006 Thomas E Enebo <enebo@acm.org>
* Copyright (C) 2004-2005 Charles O Nutter <headius@headius.com>
* Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
* Copyright (C) 2006 Ola Bini <ola.bini@ki.se>
* Copyright (C) 2006 Miguel Covarrubias <mlcovarrubias@gmail.com>
* Copyright (C) 2007 MenTaLguY <mental@rydia.net>
* Copyright (C) 2007 William N Dortch <bill.dortch@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/

package org.jruby;

import org.jruby.runtime.ivars.VariableTableManager;

public abstract class RubyObjectShaped extends RubyObject {
public RubyObjectShaped(Ruby runtime, RubyClass metaClass) {
super(runtime, metaClass);

variableTableManager = metaClass.getVariableTableManager();
}

@Override
public VariableTableManager getShape() {
return variableTableManager;
}

private final VariableTableManager variableTableManager;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyObject;
import org.jruby.RubyObjectShaped;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.builtin.IRubyObject;
Expand Down Expand Up @@ -157,7 +158,7 @@ public static ObjectAllocator specializeForVariables(RubyClass klass, Set<String

private static Class generateInternal(int size, final String clsPath) {
// ensure only one thread will attempt to generate and define the new class
final String baseName = p(RubyObject.class);
final String baseName = p(RubyObjectShaped.class);

final JiteClass jiteClass = new JiteClass(clsPath, baseName, new String[0]) {{
for (int i = 0; i < size; i++) {
Expand Down

0 comments on commit bbdc90b

Please sign in to comment.