Refactor LoadState to improve loading speed.

This commit is contained in:
James Roseborough
2008-04-23 20:05:12 +00:00
parent d2fe6ccc52
commit b6cc568c6c
2 changed files with 39 additions and 35 deletions

View File

@@ -76,22 +76,38 @@ public class LoadState {
/** The VM doing the loading */ /** The VM doing the loading */
LuaState L; LuaState L;
int loadByte() throws IOException { /** Read buffer */
return is.readUnsignedByte(); private byte[] buf = new byte[4];
}
private static int[] EMPTY_INT_ARRAY = {};
int loadInt() throws IOException { int loadInt() throws IOException {
if ( this.luacLittleEndian ) { is.readFully(buf,0,4);
int a = is.readUnsignedByte(); return luacLittleEndian?
int b = is.readUnsignedByte(); (buf[3] << 24) | ((0xff & buf[2]) << 16) | ((0xff & buf[1]) << 8) | (0xff & buf[0]):
int c = is.readUnsignedByte(); (buf[0] << 24) | ((0xff & buf[1]) << 16) | ((0xff & buf[2]) << 8) | (0xff & buf[3]);
int d = is.readUnsignedByte();
return (d << 24) | (c << 16) | (b << 8) | a;
} else {
return is.readInt();
}
} }
int[] loadIntArray() throws IOException {
int n = loadInt();
if ( n == 0 )
return EMPTY_INT_ARRAY;
// read all data at once
int m = n << 2;
if ( buf.length < m )
buf = new byte[m];
is.readFully(buf,0,m);
int[] array = new int[n];
for ( int i=0, j=0; i<n; ++i, j+=4 )
array[i] = luacLittleEndian?
(buf[j+3] << 24) | ((0xff & buf[j+2]) << 16) | ((0xff & buf[j+1]) << 8) | (0xff & buf[j+0]):
(buf[j+0] << 24) | ((0xff & buf[j+1]) << 16) | ((0xff & buf[j+2]) << 8) | (0xff & buf[j+3]);
return array;
}
long loadInt64() throws IOException { long loadInt64() throws IOException {
int a,b; int a,b;
if ( this.luacLittleEndian ) { if ( this.luacLittleEndian ) {
@@ -109,7 +125,7 @@ public class LoadState {
if ( size == 0 ) if ( size == 0 )
return null; return null;
byte[] bytes = new byte[size]; byte[] bytes = new byte[size];
is.readFully( bytes ); is.readFully( bytes, 0, size );
return new LString( bytes, 0, bytes.length - 1 ); return new LString( bytes, 0, bytes.length - 1 );
} }
@@ -143,24 +159,16 @@ public class LoadState {
} }
} }
public void loadCode( LPrototype f ) throws IOException {
int n = loadInt();
int[] code = new int[n];
for ( int i=0; i<n; i++ )
code[i] = loadInt();
f.code = code;
}
void loadConstants(LPrototype f) throws IOException { void loadConstants(LPrototype f) throws IOException {
int n = loadInt(); int n = loadInt();
LValue[] values = new LValue[n]; LValue[] values = new LValue[n];
for ( int i=0; i<n; i++ ) { for ( int i=0; i<n; i++ ) {
switch ( loadByte() ) { switch ( is.readUnsignedByte() ) {
case Lua.LUA_TNIL: case Lua.LUA_TNIL:
values[i] = LNil.NIL; values[i] = LNil.NIL;
break; break;
case Lua.LUA_TBOOLEAN: case Lua.LUA_TBOOLEAN:
values[i] = (0 != loadByte()? LBoolean.TRUE: LBoolean.FALSE); values[i] = (0 != is.readUnsignedByte()? LBoolean.TRUE: LBoolean.FALSE);
break; break;
case Lua.LUA_TNUMBER: case Lua.LUA_TNUMBER:
values[i] = loadNumber(); values[i] = loadNumber();
@@ -182,12 +190,8 @@ public class LoadState {
} }
void loadDebug( LPrototype f ) throws IOException { void loadDebug( LPrototype f ) throws IOException {
f.lineinfo = loadIntArray();
int n = loadInt(); int n = loadInt();
f.lineinfo = new int[n];
for ( int i=0; i<n; i++ )
f.lineinfo[i] = loadInt();
n = loadInt();
f.locvars = new LocVars[n]; f.locvars = new LocVars[n];
for ( int i=0; i<n; i++ ) { for ( int i=0; i<n; i++ ) {
LString varname = loadString(); LString varname = loadString();
@@ -211,11 +215,11 @@ public class LoadState {
f.source = p; f.source = p;
f.linedefined = loadInt(); f.linedefined = loadInt();
f.lastlinedefined = loadInt(); f.lastlinedefined = loadInt();
f.nups = loadByte(); f.nups = is.readUnsignedByte();
f.numparams = loadByte(); f.numparams = is.readUnsignedByte();
f.is_vararg = (0 != loadByte()); f.is_vararg = (0 != is.readUnsignedByte());
f.maxstacksize = loadByte(); f.maxstacksize = is.readUnsignedByte();
loadCode(f); f.code = loadIntArray();
loadConstants(f); loadConstants(f);
loadDebug(f); loadDebug(f);

View File

@@ -1 +1 @@
version: 0.27 version: 0.28