In Formula cache Ptg[] instead of constantly (un)serializing it from the byte encoding

This commit is contained in:
Travis Burtrum 2017-05-23 15:32:56 -04:00
parent d472f88a42
commit 31ba09082b
1 changed files with 9 additions and 5 deletions

View File

@ -39,15 +39,17 @@ public class Formula {
//Arbitrarily set. May need to increase. //Arbitrarily set. May need to increase.
private static final int MAX_ENCODED_LEN = 100000; private static final int MAX_ENCODED_LEN = 100000;
private static final Formula EMPTY = new Formula(new byte[0], 0); private static final Formula EMPTY = new Formula(new byte[0], 0, new Ptg[0]);
/** immutable */ /** immutable */
private final byte[] _byteEncoding; private final byte[] _byteEncoding;
private final int _encodedTokenLen; private final int _encodedTokenLen;
private Ptg[] _cache;
private Formula(byte[] byteEncoding, int encodedTokenLen) { private Formula(byte[] byteEncoding, int encodedTokenLen, Ptg[] cache) {
_byteEncoding = byteEncoding.clone(); _byteEncoding = byteEncoding.clone();
_encodedTokenLen = encodedTokenLen; _encodedTokenLen = encodedTokenLen;
_cache = cache;
// TODO - this seems to occur when IntersectionPtg is present // TODO - this seems to occur when IntersectionPtg is present
// This example file "IntersectionPtg.xls" // This example file "IntersectionPtg.xls"
@ -74,12 +76,14 @@ public class Formula {
public static Formula read(int encodedTokenLen, LittleEndianInput in, int totalEncodedLen) { public static Formula read(int encodedTokenLen, LittleEndianInput in, int totalEncodedLen) {
byte[] byteEncoding = IOUtils.safelyAllocate(totalEncodedLen, MAX_ENCODED_LEN); byte[] byteEncoding = IOUtils.safelyAllocate(totalEncodedLen, MAX_ENCODED_LEN);
in.readFully(byteEncoding); in.readFully(byteEncoding);
return new Formula(byteEncoding, encodedTokenLen); return new Formula(byteEncoding, encodedTokenLen, null);
} }
public Ptg[] getTokens() { public Ptg[] getTokens() {
if(_cache != null)
return _cache;
LittleEndianInput in = new LittleEndianByteArrayInputStream(_byteEncoding); LittleEndianInput in = new LittleEndianByteArrayInputStream(_byteEncoding);
return Ptg.readTokens(_encodedTokenLen, in); return _cache = Ptg.readTokens(_encodedTokenLen, in);
} }
/** /**
* Writes The formula encoding is includes: * Writes The formula encoding is includes:
@ -140,7 +144,7 @@ public class Formula {
byte[] encodedData = new byte[totalSize]; byte[] encodedData = new byte[totalSize];
Ptg.serializePtgs(ptgs, encodedData, 0); Ptg.serializePtgs(ptgs, encodedData, 0);
int encodedTokenLen = Ptg.getEncodedSizeWithoutArrayData(ptgs); int encodedTokenLen = Ptg.getEncodedSizeWithoutArrayData(ptgs);
return new Formula(encodedData, encodedTokenLen); return new Formula(encodedData, encodedTokenLen, ptgs); // todo: copy ptgs? don't think we need to
} }
/** /**
* Gets the {@link Ptg} array from the supplied {@link Formula}. * Gets the {@link Ptg} array from the supplied {@link Formula}.