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
26 changes: 19 additions & 7 deletions client/src/com/aerospike/client/lua/LuaMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,34 @@ public LuaMap clone() {
}

public LuaMap merge(LuaMap map2, LuaFunction func) {
HashMap<LuaValue,LuaValue> target = new HashMap<LuaValue,LuaValue>(map.size() + map2.map.size());
target.putAll(map);
// Cache references to avoid repeated field access
final Map<LuaValue,LuaValue> m1 = this.map;
final Map<LuaValue,LuaValue> m2 = map2.map;

// Precompute capacity to avoid rehashing (assumes default load factor 0.75)
int expected = m1.size() + m2.size();
int capacity = (int)(expected / 0.75f) + 1;
HashMap<LuaValue,LuaValue> target = new HashMap<LuaValue,LuaValue>(capacity);
target.putAll(m1);

boolean hasFunc = !(func == null || func.isnil());

for (Entry<LuaValue,LuaValue> entry : map2.map.entrySet()) {
if (hasFunc) {
LuaValue value = map.get(entry.getKey());
if (!hasFunc) {
// Fast path: no merge function -> copy remaining entries in bulk
target.putAll(m2);
}
else {
for (Entry<LuaValue,LuaValue> entry : m2.entrySet()) {
LuaValue key = entry.getKey();
LuaValue value = m1.get(key);

if (value != null) {
Varargs ret = func.invoke(value, entry.getValue());
target.put(entry.getKey(), (LuaValue)ret);
target.put(key, (LuaValue)ret);
continue;
}
target.put(key, entry.getValue());
}
target.put(entry.getKey(), entry.getValue());
}
return new LuaMap(instance, target);
}
Expand Down