3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-13 10:45:51 +00:00

Undo the memory usage optimisation for scriptPubKey parsing on Android to see if CPU time now matters more than memory (especially when combined with the wallet unspents tracking work).

This commit is contained in:
Mike Hearn 2015-03-13 18:00:05 -07:00
parent 7f14f7e491
commit 7e12da357c

View File

@ -17,20 +17,12 @@
package org.bitcoinj.core; package org.bitcoinj.core;
import org.bitcoinj.script.Script; import org.bitcoinj.script.*;
import org.bitcoinj.script.ScriptBuilder; import org.slf4j.*;
import org.slf4j.Logger; import javax.annotation.*;
import org.slf4j.LoggerFactory; import java.io.*;
import java.util.*;
import javax.annotation.Nullable;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.util.Arrays;
import static com.google.common.base.Preconditions.*; import static com.google.common.base.Preconditions.*;
@ -50,7 +42,7 @@ public class TransactionOutput extends ChildMessage implements Serializable {
private byte[] scriptBytes; private byte[] scriptBytes;
// The script bytes are parsed and turned into a Script on demand. // The script bytes are parsed and turned into a Script on demand.
private transient WeakReference<Script> scriptPubKey; private transient Script scriptPubKey;
// These fields are Java serialized but not Bitcoin serialized. They are used for tracking purposes in our wallet // These fields are Java serialized but not Bitcoin serialized. They are used for tracking purposes in our wallet
// only. If set to true, this output is counted towards our balance. If false and spentBy is null the tx output // only. If set to true, this output is counted towards our balance. If false and spentBy is null the tx output
@ -121,17 +113,11 @@ public class TransactionOutput extends ChildMessage implements Serializable {
} }
public Script getScriptPubKey() throws ScriptException { public Script getScriptPubKey() throws ScriptException {
// Quick hack to try and reduce memory consumption on Androids. SoftReference is the same as WeakReference if (scriptPubKey == null) {
// on Dalvik (by design), so this arrangement just means that we can avoid the cost of re-parsing the script
// bytes if getScriptPubKey is called multiple times in quick succession in between garbage collections.
Script script = scriptPubKey == null ? null : scriptPubKey.get();
if (script == null) {
maybeParse(); maybeParse();
script = new Script(scriptBytes); scriptPubKey = new Script(scriptBytes);
scriptPubKey = new WeakReference<Script>(script);
return script;
} }
return script; return scriptPubKey;
} }
/** /**