Pull request: Reduce allocations per LuaClosure call #76
This commit is contained in:
@@ -1,28 +1,31 @@
|
|||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (c) 2009 Luaj.org. All rights reserved.
|
* Copyright (c) 2009 Luaj.org. All rights reserved.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
*
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
*
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
package org.luaj.vm2;
|
package org.luaj.vm2;
|
||||||
|
|
||||||
import org.luaj.vm2.libs.DebugLib.CallFrame;
|
import org.luaj.vm2.libs.DebugLib.CallFrame;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extension of {@link LuaFunction} which executes lua bytecode.
|
* Extension of {@link LuaFunction} which executes lua bytecode.
|
||||||
* <p>
|
* <p>
|
||||||
@@ -129,43 +132,97 @@ public class LuaClosure extends LuaFunction {
|
|||||||
return "function: " + p.toString();
|
return "function: " + p.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private List<LuaValue[]> stackPool = new ArrayList<>();
|
||||||
private LuaValue[] getNewStack() {
|
private LuaValue[] getNewStack() {
|
||||||
|
if (stackPool.isEmpty()) {
|
||||||
|
return getNewStackRaw();
|
||||||
|
} else {
|
||||||
|
return stackPool.remove(stackPool.size() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private LuaValue[] getNewStackRaw() {
|
||||||
int max = p.maxstacksize;
|
int max = p.maxstacksize;
|
||||||
LuaValue[] stack = new LuaValue[max];
|
LuaValue[] stack = new LuaValue[max];
|
||||||
System.arraycopy(NILS, 0, stack, 0, max);
|
System.arraycopy(NILS, 0, stack, 0, max);
|
||||||
return stack;
|
return stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void releaseStack(LuaValue[] stack) {
|
||||||
|
System.arraycopy(NILS, 0, stack, 0, stack.length);
|
||||||
|
stackPool.add(stack);
|
||||||
|
}
|
||||||
|
|
||||||
public final LuaValue call() {
|
public final LuaValue call() {
|
||||||
LuaValue[] stack = getNewStack();
|
LuaValue[] stack = getNewStack();
|
||||||
return execute(stack,NONE).arg1();
|
LuaValue result = execute(stack,NONE).arg1();
|
||||||
|
releaseStack(stack);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final LuaValue call(LuaValue arg) {
|
public final LuaValue call(LuaValue arg) {
|
||||||
LuaValue[] stack = getNewStack();
|
LuaValue[] stack = getNewStack();
|
||||||
|
LuaValue result;
|
||||||
switch ( p.numparams ) {
|
switch ( p.numparams ) {
|
||||||
default: stack[0]=arg; return execute(stack,NONE).arg1();
|
default:
|
||||||
case 0: return execute(stack,arg).arg1();
|
stack[0]=arg;
|
||||||
|
result = execute(stack,NONE).arg1();
|
||||||
|
break;
|
||||||
|
case 0:
|
||||||
|
result = execute(stack,arg).arg1();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
releaseStack(stack);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final LuaValue call(LuaValue arg1, LuaValue arg2) {
|
public final LuaValue call(LuaValue arg1, LuaValue arg2) {
|
||||||
LuaValue[] stack = getNewStack();
|
LuaValue[] stack = getNewStack();
|
||||||
|
LuaValue result;
|
||||||
switch ( p.numparams ) {
|
switch ( p.numparams ) {
|
||||||
default: stack[0]=arg1; stack[1]=arg2; return execute(stack,NONE).arg1();
|
default:
|
||||||
case 1: stack[0]=arg1; return execute(stack,arg2).arg1();
|
stack[0]=arg1;
|
||||||
case 0: return execute(stack,p.is_vararg!=0? varargsOf(arg1,arg2): NONE).arg1();
|
stack[1]=arg2;
|
||||||
|
result = execute(stack,NONE).arg1();
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
stack[0]=arg1;
|
||||||
|
result = execute(stack,arg2).arg1();
|
||||||
|
break;
|
||||||
|
case 0:
|
||||||
|
result = execute(stack,p.is_vararg!=0 ? varargsOf(arg1,arg2) : NONE).arg1();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
releaseStack(stack);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3) {
|
public final LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3) {
|
||||||
LuaValue[] stack = getNewStack();
|
LuaValue[] stack = getNewStack();
|
||||||
|
LuaValue result;
|
||||||
switch ( p.numparams ) {
|
switch ( p.numparams ) {
|
||||||
default: stack[0]=arg1; stack[1]=arg2; stack[2]=arg3; return execute(stack,NONE).arg1();
|
default:
|
||||||
case 2: stack[0]=arg1; stack[1]=arg2; return execute(stack,arg3).arg1();
|
stack[0]=arg1;
|
||||||
case 1: stack[0]=arg1; return execute(stack,p.is_vararg!=0? varargsOf(arg2,arg3): NONE).arg1();
|
stack[1]=arg2;
|
||||||
case 0: return execute(stack,p.is_vararg!=0? varargsOf(arg1,arg2,arg3): NONE).arg1();
|
stack[2]=arg3;
|
||||||
|
result = execute(stack,NONE).arg1();
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
stack[0]=arg1;
|
||||||
|
stack[1]=arg2;
|
||||||
|
result = execute(stack,arg3).arg1();
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
stack[0]=arg1;
|
||||||
|
result = execute(stack,p.is_vararg!=0 ? varargsOf(arg2,arg3) : NONE).arg1();
|
||||||
|
break;
|
||||||
|
case 0:
|
||||||
|
result = execute(stack,p.is_vararg!=0 ? varargsOf(arg1,arg2,arg3) : NONE).arg1();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
releaseStack(stack);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final Varargs invoke(Varargs varargs) {
|
public final Varargs invoke(Varargs varargs) {
|
||||||
@@ -176,7 +233,11 @@ public class LuaClosure extends LuaFunction {
|
|||||||
LuaValue[] stack = getNewStack();
|
LuaValue[] stack = getNewStack();
|
||||||
for ( int i=0; i<p.numparams; i++ )
|
for ( int i=0; i<p.numparams; i++ )
|
||||||
stack[i] = varargs.arg(i+1);
|
stack[i] = varargs.arg(i+1);
|
||||||
return execute(stack,p.is_vararg!=0? varargs.subargs(p.numparams+1): NONE);
|
Varargs result = execute(stack,p.is_vararg!=0 ? varargs.subargs(p.numparams+1) : NONE);
|
||||||
|
if (result instanceof LuaValue) {
|
||||||
|
releaseStack(stack);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Varargs execute( LuaValue[] stack, Varargs varargs ) {
|
protected Varargs execute( LuaValue[] stack, Varargs varargs ) {
|
||||||
@@ -502,10 +563,10 @@ public class LuaClosure extends LuaFunction {
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
case Lua.OP_EXTRAARG:
|
case Lua.OP_EXTRAARG:
|
||||||
throw new IllegalArgumentException("Uexecutable opcode: OP_EXTRAARG");
|
throw new java.lang.IllegalArgumentException("Uexecutable opcode: OP_EXTRAARG");
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new IllegalArgumentException("Illegal opcode: " + (i & 0x3f));
|
throw new java.lang.IllegalArgumentException("Illegal opcode: " + (i & 0x3f));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch ( LuaError le ) {
|
} catch ( LuaError le ) {
|
||||||
|
|||||||
Reference in New Issue
Block a user