From 62cda2bc490498ffbad795f3fbc0487dbe39eef0 Mon Sep 17 00:00:00 2001 From: Ian Farmer Date: Wed, 21 Nov 2007 02:21:35 +0000 Subject: [PATCH] Change upvalues to point to the LuaState instead of directly to the stack. This change has two benefits: * Reference to LuaState's stack does not need to be adjusted when the stack needs to grow. * One less heap object for a closed upvalue. --- src/core/org/luaj/vm/LuaState.java | 4 ++-- src/core/org/luaj/vm/UpVal.java | 24 +++++++++++++++--------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/core/org/luaj/vm/LuaState.java b/src/core/org/luaj/vm/LuaState.java index 641125bb..49aa0ed0 100644 --- a/src/core/org/luaj/vm/LuaState.java +++ b/src/core/org/luaj/vm/LuaState.java @@ -875,14 +875,14 @@ public class LuaState extends Lua { int i; for ( i = this.upvals.size() - 1; i >= 0; --i ) { up = (UpVal) this.upvals.elementAt( i ); - if ( up.stack == this.stack && up.position == target ) { + if ( up.state == this && up.position == target ) { return up; } else if ( up.position < target ) { break; } } - up = new UpVal( upValName, this.stack, target ); + up = new UpVal( upValName, this, target ); this.upvals.insertElementAt( up, i + 1 ); return up; } diff --git a/src/core/org/luaj/vm/UpVal.java b/src/core/org/luaj/vm/UpVal.java index 42b6cc27..cb15a315 100644 --- a/src/core/org/luaj/vm/UpVal.java +++ b/src/core/org/luaj/vm/UpVal.java @@ -25,12 +25,13 @@ package org.luaj.vm; public class UpVal { private LString name; - public LValue[] stack; - public int position; + LuaState state; + int position; + LValue value; - public UpVal( LString string, LValue[] stack, int i ) { + public UpVal( LString string, LuaState state, int i ) { this.name = string; - this.stack = stack; + this.state = state; this.position = i; } @@ -39,18 +40,23 @@ public class UpVal { } public LValue getValue() { - return stack[ position ]; + if ( state == null ) + return value; + else + return state.stack[ position ]; } public void setValue( LValue value ) { - stack[ position ] = value; + if ( state == null ) + this.value = value; + else + state.stack[ position ] = value; } public boolean close( int limit ) { if ( position >= limit ) { - final LValue v = stack[ position ]; - stack = new LValue[] { v }; - position = 0; + value = state.stack[ position ]; + state = null; return true; } else { return false;