3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-15 03:35:52 +00:00

Quick hack to try and reduce memory consumption for Androids.

Takes a test wallet from around 11.3mb to 9.6mb
This commit is contained in:
Mike Hearn 2014-01-11 16:02:37 +01:00
parent d50412e24c
commit 843489111e
2 changed files with 20 additions and 10 deletions

View File

@ -17,13 +17,13 @@
package com.google.bitcoin.core; package com.google.bitcoin.core;
import com.google.bitcoin.script.Script; import com.google.bitcoin.script.Script;
import com.google.common.base.Preconditions;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.Serializable; import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.util.Map; import java.util.Map;
import static com.google.common.base.Preconditions.checkElementIndex; import static com.google.common.base.Preconditions.checkElementIndex;
@ -52,7 +52,7 @@ public class TransactionInput extends ChildMessage implements Serializable {
private byte[] scriptBytes; private byte[] scriptBytes;
// The Script object obtained from parsing scriptBytes. Only filled in on demand and if the transaction is not // The Script object obtained from parsing scriptBytes. Only filled in on demand and if the transaction is not
// coinbase. // coinbase.
transient private Script scriptSig; transient private WeakReference<Script> scriptSig;
// A pointer to the transaction that owns this input. // A pointer to the transaction that owns this input.
private Transaction parentTransaction; private Transaction parentTransaction;
@ -159,16 +159,19 @@ public class TransactionInput extends ChildMessage implements Serializable {
public Script getScriptSig() throws ScriptException { public Script getScriptSig() throws ScriptException {
// Transactions that generate new coins don't actually have a script. Instead this // Transactions that generate new coins don't actually have a script. Instead this
// parameter is overloaded to be something totally different. // parameter is overloaded to be something totally different.
if (scriptSig == null) { Script script = scriptSig == null ? null : scriptSig.get();
if (script == null) {
maybeParse(); maybeParse();
scriptSig = new Script(Preconditions.checkNotNull(scriptBytes)); script = new Script(scriptBytes);
scriptSig = new WeakReference<Script>(script);
return script;
} }
return scriptSig; return script;
} }
/** Set the given program as the scriptSig that is supposed to satisfy the connected output script. */ /** Set the given program as the scriptSig that is supposed to satisfy the connected output script. */
public void setScriptSig(Script scriptSig) { public void setScriptSig(Script scriptSig) {
this.scriptSig = checkNotNull(scriptSig); this.scriptSig = new WeakReference<Script>(checkNotNull(scriptSig));
// TODO: This should all be cleaned up so we have a consistent internal representation. // TODO: This should all be cleaned up so we have a consistent internal representation.
setScriptBytes(scriptSig.getProgram()); setScriptBytes(scriptSig.getProgram());
} }

View File

@ -26,6 +26,7 @@ import java.io.IOException;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.Serializable; import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.math.BigInteger; import java.math.BigInteger;
import static com.google.common.base.Preconditions.*; import static com.google.common.base.Preconditions.*;
@ -44,7 +45,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 Script scriptPubKey; private transient WeakReference<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
@ -118,11 +119,17 @@ public class TransactionOutput extends ChildMessage implements Serializable {
} }
public Script getScriptPubKey() throws ScriptException { public Script getScriptPubKey() throws ScriptException {
if (scriptPubKey == null) { // Quick hack to try and reduce memory consumption on Androids. SoftReference is the same as WeakReference
// 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();
scriptPubKey = new Script(scriptBytes); script = new Script(scriptBytes);
scriptPubKey = new WeakReference<Script>(script);
return script;
} }
return scriptPubKey; return script;
} }
protected void parseLite() throws ProtocolException { protected void parseLite() throws ProtocolException {