Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions client/src/com/aerospike/client/lua/LuaMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,26 @@ public LuaMap merge(LuaMap map2, LuaFunction func) {
}

public LuaMap diff(LuaMap map2) {
HashMap<LuaValue,LuaValue> target = new HashMap<LuaValue,LuaValue>(map.size() + map2.map.size());

for (Entry<LuaValue,LuaValue> entry : map.entrySet()) {
if (!map2.map.containsKey(entry.getKey())) {
target.put(entry.getKey(), entry.getValue());
// Cache reference to other map to avoid repeated field access
final Map<LuaValue,LuaValue> other = map2.map;

// Expected number of entries in the result is at most sum of sizes.
// Compute initial capacity to avoid rehashing (load factor = 0.75).
int expected = map.size() + other.size();
int capacity = (int)(expected / 0.75f) + 1;
HashMap<LuaValue,LuaValue> target = new HashMap<LuaValue,LuaValue>(capacity);

// Copy all entries from this map, then process the other map:
// - If a key exists in target (was in this map), remove it (present in both -> exclude).
// - Otherwise, add the entry from the other map (unique to other).
target.putAll(map);
for (Entry<LuaValue,LuaValue> entry : other.entrySet()) {
LuaValue key = entry.getKey();
if (target.containsKey(key)) {
target.remove(key);
}
}

for (Entry<LuaValue,LuaValue> entry : map2.map.entrySet()) {
if (!map.containsKey(entry.getKey())) {
target.put(entry.getKey(), entry.getValue());
else {
target.put(key, entry.getValue());
}
}
return new LuaMap(instance, target);
Expand Down