Opportunity for lazy initialization in Headers interface

Motivation:
The Headers interface had two member variables (addAllVisitor, setAllVisitor) which are not necessarily always needed but are always instantiated.  This may result in excess memory being used.

Modifications:
 - addAllVisitor will be accessed via a method addAllVisitor() which will use lazy initialization.
 - setAllVisitor will be accessed via a method addAllVisitor() which will use lazy initialization.

Result:
Potential memory savings by using lazy initialization.
This commit is contained in:
Scott Mitchell 2015-01-21 13:40:09 -05:00
parent 0fc097cb20
commit 27a2017f7f

View File

@ -79,25 +79,8 @@ public class DefaultHeaders<T> implements Headers<T> {
private static final int HASH_CODE_PRIME = 31;
private static final int DEFAULT_BUCKET_SIZE = 17;
private static final int DEFAULT_MAP_SIZE = 4;
private static final NameConverter<Object> DEFAULT_NAME_CONVERTER = new IdentityNameConverter<Object>();
private final EntryVisitor<T> setAllVisitor = new EntryVisitor<T>() {
@Override
public boolean visit(Entry<T, T> entry) {
set(entry.getKey(), entry.getValue());
return true;
}
};
private final EntryVisitor<T> addAllVisitor = new EntryVisitor<T>() {
@Override
public boolean visit(Entry<T, T> entry) {
add(entry.getKey(), entry.getValue());
return true;
}
};
private final IntObjectMap<HeaderEntry> entries;
private final IntObjectMap<HeaderEntry> tailEntries;
private final HeaderEntry head;
@ -107,6 +90,8 @@ public class DefaultHeaders<T> implements Headers<T> {
private final ValueConverter<T> valueConverter;
private final NameConverter<T> nameConverter;
private final int bucketSize;
private EntryVisitor<T> setAllVisitor;
private EntryVisitor<T> addAllVisitor;
int size;
@SuppressWarnings("unchecked")
@ -752,7 +737,7 @@ public class DefaultHeaders<T> implements Headers<T> {
}
} else {
try {
headers.forEachEntry(setAllVisitor);
headers.forEachEntry(setAllVisitor());
} catch (Exception ex) {
PlatformDependent.throwException(ex);
}
@ -1255,7 +1240,7 @@ public class DefaultHeaders<T> implements Headers<T> {
}
} else {
try {
headers.forEachEntry(addAllVisitor);
headers.forEachEntry(addAllVisitor());
} catch (Exception ex) {
PlatformDependent.throwException(ex);
}
@ -1322,6 +1307,38 @@ public class DefaultHeaders<T> implements Headers<T> {
return removed;
}
/**
* Lazy initialization of the visitor which will set all headers.
*/
private EntryVisitor<T> setAllVisitor() {
if (setAllVisitor == null) {
setAllVisitor = new EntryVisitor<T>() {
@Override
public boolean visit(Entry<T, T> entry) {
set(entry.getKey(), entry.getValue());
return true;
}
};
}
return setAllVisitor;
}
/**
* Lazy initialization of the visitor which will add all headers.
*/
private EntryVisitor<T> addAllVisitor() {
if (addAllVisitor == null) {
addAllVisitor = new EntryVisitor<T>() {
@Override
public boolean visit(Entry<T, T> entry) {
add(entry.getKey(), entry.getValue());
return true;
}
};
}
return addAllVisitor;
}
private final class HeaderEntry implements Map.Entry<T, T> {
final int hash;
final T name;