From 31ba09082b769c2cf3d11fe0b90a0c678a395446 Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Tue, 23 May 2017 15:32:56 -0400 Subject: [PATCH] In Formula cache Ptg[] instead of constantly (un)serializing it from the byte encoding --- src/java/org/apache/poi/ss/formula/Formula.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/java/org/apache/poi/ss/formula/Formula.java b/src/java/org/apache/poi/ss/formula/Formula.java index 50ae3d2ae..a4d78cf4c 100644 --- a/src/java/org/apache/poi/ss/formula/Formula.java +++ b/src/java/org/apache/poi/ss/formula/Formula.java @@ -39,15 +39,17 @@ public class Formula { //Arbitrarily set. May need to increase. 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 */ private final byte[] _byteEncoding; private final int _encodedTokenLen; + private Ptg[] _cache; - private Formula(byte[] byteEncoding, int encodedTokenLen) { + private Formula(byte[] byteEncoding, int encodedTokenLen, Ptg[] cache) { _byteEncoding = byteEncoding.clone(); _encodedTokenLen = encodedTokenLen; + _cache = cache; // TODO - this seems to occur when IntersectionPtg is present // This example file "IntersectionPtg.xls" @@ -74,12 +76,14 @@ public class Formula { public static Formula read(int encodedTokenLen, LittleEndianInput in, int totalEncodedLen) { byte[] byteEncoding = IOUtils.safelyAllocate(totalEncodedLen, MAX_ENCODED_LEN); in.readFully(byteEncoding); - return new Formula(byteEncoding, encodedTokenLen); + return new Formula(byteEncoding, encodedTokenLen, null); } public Ptg[] getTokens() { + if(_cache != null) + return _cache; LittleEndianInput in = new LittleEndianByteArrayInputStream(_byteEncoding); - return Ptg.readTokens(_encodedTokenLen, in); + return _cache = Ptg.readTokens(_encodedTokenLen, in); } /** * Writes The formula encoding is includes: @@ -140,7 +144,7 @@ public class Formula { byte[] encodedData = new byte[totalSize]; Ptg.serializePtgs(ptgs, encodedData, 0); 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}.