Skip to content

Commit

Permalink
UniqueId.suggest: remove unnecessary memory allocation.
Browse files Browse the repository at this point in the history
If there was N suggestions, we'd allocate N strings unnecessarily.
When trying to check whether a UID is already in the cache, we can
check either the forward mapping (name -> ID) or the backward mapping
(ID -> name), but the former doesn't require any memory allocation so
do that instead (the latter requires converting a byte[] to a String).

Change-Id: I11b1e30e6c8f4f5cfadd2068216610311a950393
Reviewed-by: Berk D. Demir <bdd@mindcast.org>
  • Loading branch information
tsuna committed Mar 16, 2011
1 parent 3deb5fa commit bc4d17e
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/uid/UniqueId.java
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,14 @@ public List<String> suggest(final String search) throws HBaseException {
final byte[] key = row.get(0).key();
final String name = fromBytes(key);
final byte[] id = row.get(0).value();
final String cached_name = idCache.get(new String(id));
if (cached_name == null) {
final byte[] cached_id = nameCache.get(name);
if (cached_id == null) {
addIdToCache(name, id);
addNameToCache(id, name);
} else if (!cached_name.equals(name)) {
} else if (!Arrays.equals(id, cached_id)) {
throw new IllegalStateException("WTF? For kind=" + kind()
+ " id=" + Arrays.toString(id) + " I already have name="
+ cached_name + " in cache, but I just scanned name=" + name);
+ " name=" + name + ", we have id=" + Arrays.toString(cached_id)
+ " in cache, but just scanned id=" + Arrays.toString(id));
}
suggestions.add(name);
if ((short) suggestions.size() > MAX_SUGGESTIONS) {
Expand Down

0 comments on commit bc4d17e

Please sign in to comment.