From b6cc568c6cf6cf6b17f8735ded8ebf4f586928a1 Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Wed, 23 Apr 2008 20:05:12 +0000 Subject: [PATCH] Refactor LoadState to improve loading speed. --- src/core/org/luaj/vm/LoadState.java | 72 +++++++++++++++-------------- version.properties | 2 +- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/core/org/luaj/vm/LoadState.java b/src/core/org/luaj/vm/LoadState.java index f394bf5d..592d3cfa 100644 --- a/src/core/org/luaj/vm/LoadState.java +++ b/src/core/org/luaj/vm/LoadState.java @@ -76,22 +76,38 @@ public class LoadState { /** The VM doing the loading */ LuaState L; - int loadByte() throws IOException { - return is.readUnsignedByte(); - } - + /** Read buffer */ + private byte[] buf = new byte[4]; + + private static int[] EMPTY_INT_ARRAY = {}; + int loadInt() throws IOException { - if ( this.luacLittleEndian ) { - int a = is.readUnsignedByte(); - int b = is.readUnsignedByte(); - int c = is.readUnsignedByte(); - int d = is.readUnsignedByte(); - return (d << 24) | (c << 16) | (b << 8) | a; - } else { - return is.readInt(); - } + is.readFully(buf,0,4); + return luacLittleEndian? + (buf[3] << 24) | ((0xff & buf[2]) << 16) | ((0xff & buf[1]) << 8) | (0xff & buf[0]): + (buf[0] << 24) | ((0xff & buf[1]) << 16) | ((0xff & buf[2]) << 8) | (0xff & buf[3]); } + 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