diff --git a/client/src/com/aerospike/client/lua/LuaMap.java b/client/src/com/aerospike/client/lua/LuaMap.java index 0d9ed0cad..5a1755353 100644 --- a/client/src/com/aerospike/client/lua/LuaMap.java +++ b/client/src/com/aerospike/client/lua/LuaMap.java @@ -78,22 +78,34 @@ public LuaMap clone() { } public LuaMap merge(LuaMap map2, LuaFunction func) { - HashMap target = new HashMap(map.size() + map2.map.size()); - target.putAll(map); + // Cache references to avoid repeated field access + final Map m1 = this.map; + final Map 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 target = new HashMap(capacity); + target.putAll(m1); boolean hasFunc = !(func == null || func.isnil()); - for (Entry 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 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); }