TreeMap extra get operation removed.

Motivation:
Checking if a key exists on a TreeMap has a Big O of "log 2 N",
doing it twice is not cheap.

Modifications:
Get the key instead which has the same cost and check if it is null.

Result:
Faster code due to one expensive operation removed.
This commit is contained in:
Guido Medina 2016-04-22 09:34:56 +01:00 committed by Scott Mitchell
parent 946d033379
commit 5b59250657

View File

@ -36,18 +36,13 @@ public class DefaultSpdySettingsFrame implements SpdySettingsFrame {
@Override @Override
public boolean isSet(int id) { public boolean isSet(int id) {
Integer key = Integer.valueOf(id); return settingsMap.containsKey(id);
return settingsMap.containsKey(key);
} }
@Override @Override
public int getValue(int id) { public int getValue(int id) {
Integer key = Integer.valueOf(id); final Setting setting = settingsMap.get(id);
if (settingsMap.containsKey(key)) { return setting != null ? setting.getValue() : -1;
return settingsMap.get(key).getValue();
} else {
return -1;
}
} }
@Override @Override
@ -60,9 +55,9 @@ public class DefaultSpdySettingsFrame implements SpdySettingsFrame {
if (id < 0 || id > SpdyCodecUtil.SPDY_SETTINGS_MAX_ID) { if (id < 0 || id > SpdyCodecUtil.SPDY_SETTINGS_MAX_ID) {
throw new IllegalArgumentException("Setting ID is not valid: " + id); throw new IllegalArgumentException("Setting ID is not valid: " + id);
} }
Integer key = Integer.valueOf(id); final Integer key = Integer.valueOf(id);
if (settingsMap.containsKey(key)) { final Setting setting = settingsMap.get(key);
Setting setting = settingsMap.get(key); if (setting != null) {
setting.setValue(value); setting.setValue(value);
setting.setPersist(persistValue); setting.setPersist(persistValue);
setting.setPersisted(persisted); setting.setPersisted(persisted);
@ -74,47 +69,36 @@ public class DefaultSpdySettingsFrame implements SpdySettingsFrame {
@Override @Override
public SpdySettingsFrame removeValue(int id) { public SpdySettingsFrame removeValue(int id) {
Integer key = Integer.valueOf(id); settingsMap.remove(id);
if (settingsMap.containsKey(key)) {
settingsMap.remove(key);
}
return this; return this;
} }
@Override @Override
public boolean isPersistValue(int id) { public boolean isPersistValue(int id) {
Integer key = Integer.valueOf(id); final Setting setting = settingsMap.get(id);
if (settingsMap.containsKey(key)) { return setting != null && setting.isPersist();
return settingsMap.get(key).isPersist();
} else {
return false;
}
} }
@Override @Override
public SpdySettingsFrame setPersistValue(int id, boolean persistValue) { public SpdySettingsFrame setPersistValue(int id, boolean persistValue) {
Integer key = Integer.valueOf(id); final Setting setting = settingsMap.get(id);
if (settingsMap.containsKey(key)) { if (setting != null) {
settingsMap.get(key).setPersist(persistValue); setting.setPersist(persistValue);
} }
return this; return this;
} }
@Override @Override
public boolean isPersisted(int id) { public boolean isPersisted(int id) {
Integer key = Integer.valueOf(id); final Setting setting = settingsMap.get(id);
if (settingsMap.containsKey(key)) { return setting != null && setting.isPersisted();
return settingsMap.get(key).isPersisted();
} else {
return false;
}
} }
@Override @Override
public SpdySettingsFrame setPersisted(int id, boolean persisted) { public SpdySettingsFrame setPersisted(int id, boolean persisted) {
Integer key = Integer.valueOf(id); final Setting setting = settingsMap.get(id);
if (settingsMap.containsKey(key)) { if (setting != null) {
settingsMap.get(key).setPersisted(persisted); setting.setPersisted(persisted);
} }
return this; return this;
} }