From e9b3577eecb59bfdbc25fbc77d951a769dcce699 Mon Sep 17 00:00:00 2001 From: zzz <zzz@mail.i2p> Date: Sun, 2 Feb 2014 17:19:06 +0000 Subject: [PATCH] snark: cache PeerID.toString() --- apps/i2psnark/java/src/org/klomp/snark/PeerID.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/apps/i2psnark/java/src/org/klomp/snark/PeerID.java b/apps/i2psnark/java/src/org/klomp/snark/PeerID.java index 16dc9224a9..0a71450c7c 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/PeerID.java +++ b/apps/i2psnark/java/src/org/klomp/snark/PeerID.java @@ -52,6 +52,7 @@ class PeerID implements Comparable<PeerID> private boolean triedDestLookup; private final int hash; private final I2PSnarkUtil util; + private String _toStringCache; public PeerID(byte[] id, Destination address) { @@ -216,13 +217,15 @@ class PeerID implements Comparable<PeerID> } /** - * Returns the String "id@address" where id is the base64 encoded id - * and address is the base64 dest (was the base64 hash of the dest) which + * Returns the String "id@address" where id is the first 4 chars of the base64 encoded id + * and address is the first 6 chars of the base64 dest (was the base64 hash of the dest) which * should match what the bytemonsoon tracker reports on its web pages. */ - @Override + @Override public String toString() { + if (_toStringCache != null) + return _toStringCache; if (id == null || address == null) return "unkn@" + Base64.encode(destHash).substring(0, 6); int nonZero = 0; @@ -232,7 +235,8 @@ class PeerID implements Comparable<PeerID> break; } } - return Base64.encode(id, nonZero, id.length-nonZero).substring(0,4) + "@" + address.toBase64().substring(0,6); + _toStringCache = Base64.encode(id, nonZero, id.length-nonZero).substring(0,4) + "@" + address.toBase64().substring(0,6); + return _toStringCache; } /** -- GitLab