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:
parent
0f25e3b0d6
commit
3b1f15e366
@ -79,25 +79,8 @@ public class DefaultHeaders<T> implements Headers<T> {
|
|||||||
private static final int HASH_CODE_PRIME = 31;
|
private static final int HASH_CODE_PRIME = 31;
|
||||||
private static final int DEFAULT_BUCKET_SIZE = 17;
|
private static final int DEFAULT_BUCKET_SIZE = 17;
|
||||||
private static final int DEFAULT_MAP_SIZE = 4;
|
private static final int DEFAULT_MAP_SIZE = 4;
|
||||||
|
|
||||||
private static final NameConverter<Object> DEFAULT_NAME_CONVERTER = new IdentityNameConverter<Object>();
|
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> entries;
|
||||||
private final IntObjectMap<HeaderEntry> tailEntries;
|
private final IntObjectMap<HeaderEntry> tailEntries;
|
||||||
private final HeaderEntry head;
|
private final HeaderEntry head;
|
||||||
@ -107,6 +90,8 @@ public class DefaultHeaders<T> implements Headers<T> {
|
|||||||
private final ValueConverter<T> valueConverter;
|
private final ValueConverter<T> valueConverter;
|
||||||
private final NameConverter<T> nameConverter;
|
private final NameConverter<T> nameConverter;
|
||||||
private final int bucketSize;
|
private final int bucketSize;
|
||||||
|
private EntryVisitor<T> setAllVisitor;
|
||||||
|
private EntryVisitor<T> addAllVisitor;
|
||||||
int size;
|
int size;
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@ -752,7 +737,7 @@ public class DefaultHeaders<T> implements Headers<T> {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
headers.forEachEntry(setAllVisitor);
|
headers.forEachEntry(setAllVisitor());
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
PlatformDependent.throwException(ex);
|
PlatformDependent.throwException(ex);
|
||||||
}
|
}
|
||||||
@ -1255,7 +1240,7 @@ public class DefaultHeaders<T> implements Headers<T> {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
headers.forEachEntry(addAllVisitor);
|
headers.forEachEntry(addAllVisitor());
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
PlatformDependent.throwException(ex);
|
PlatformDependent.throwException(ex);
|
||||||
}
|
}
|
||||||
@ -1322,6 +1307,38 @@ public class DefaultHeaders<T> implements Headers<T> {
|
|||||||
return removed;
|
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> {
|
private final class HeaderEntry implements Map.Entry<T, T> {
|
||||||
final int hash;
|
final int hash;
|
||||||
final T name;
|
final T name;
|
||||||
|
Loading…
Reference in New Issue
Block a user