PHOENIX-1958 Minimize memory allocation on new connection
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixConnection.java b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixConnection.java
index 4c57d09..11ff1d8 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixConnection.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixConnection.java
@@ -47,7 +47,6 @@
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
 import java.util.Properties;
 import java.util.concurrent.Executor;
 
@@ -91,7 +90,6 @@
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableMap.Builder;
 import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
 
 
 /**
@@ -166,21 +164,9 @@
             if (tenantId != null) {
                 services = services.getChildQueryServices(tenantId.getBytesPtr());
             }
-            // TODO: we could avoid creating another wrapper if the only property
-            // specified was for the tenant ID
-            Map<String, String> existingProps = services.getProps().asMap();
-            final Map<String, String> tmpAugmentedProps = Maps.newHashMapWithExpectedSize(existingProps.size() + info.size());
-            tmpAugmentedProps.putAll(existingProps);
-            boolean needsDelegate = false;
-            for (Entry<Object, Object> entry : this.info.entrySet()) {
-                String key = entry.getKey().toString();
-                String value = entry.getValue().toString();
-                String oldValue = tmpAugmentedProps.put(key, value);
-                needsDelegate |= !Objects.equal(oldValue, value);
-            }
-            this.services = !needsDelegate ? services : new DelegateConnectionQueryServices(services) {
-                final ReadOnlyProps augmentedProps = new ReadOnlyProps(tmpAugmentedProps);
-    
+            ReadOnlyProps currentProps = services.getProps();
+            final ReadOnlyProps augmentedProps = currentProps.addAll(filterKnownNonProperties(this.info));
+            this.services = augmentedProps == currentProps ? services : new DelegateConnectionQueryServices(services) {
                 @Override
                 public ReadOnlyProps getProps() {
                     return augmentedProps;
@@ -222,6 +208,23 @@
         this.customTracingAnnotations = getImmutableCustomTracingAnnotations();
     }
     
+    private static Properties filterKnownNonProperties(Properties info) {
+        Properties prunedProperties = info;
+        if (info.contains(PhoenixRuntime.CURRENT_SCN_ATTRIB)) {
+            if (prunedProperties == info) {
+                prunedProperties = PropertiesUtil.deepCopy(info);
+            }
+            prunedProperties.remove(PhoenixRuntime.CURRENT_SCN_ATTRIB);
+        }
+        if (info.contains(PhoenixRuntime.TENANT_ID_ATTRIB)) {
+            if (prunedProperties == info) {
+                prunedProperties = PropertiesUtil.deepCopy(info);
+            }
+            prunedProperties.remove(PhoenixRuntime.TENANT_ID_ATTRIB);
+        }
+        return prunedProperties;
+    }
+
     private ImmutableMap<String, String> getImmutableCustomTracingAnnotations() {
     	Builder<String, String> result = ImmutableMap.builder();
     	result.putAll(JDBCUtil.getAnnotations(url, info));
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/ReadOnlyProps.java b/phoenix-core/src/main/java/org/apache/phoenix/util/ReadOnlyProps.java
index 68b0879..47137ef 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/util/ReadOnlyProps.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/ReadOnlyProps.java
@@ -23,10 +23,13 @@
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Properties;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import com.google.common.base.Objects;
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
 
 /**
  * 
@@ -61,6 +64,17 @@
         this.props = ImmutableMap.copyOf(props);
     }
 
+    private ReadOnlyProps(ReadOnlyProps defaultProps, Properties overrides) {
+        Map<String,String> combinedProps = Maps.newHashMapWithExpectedSize(defaultProps.props.size() + overrides.size());
+        combinedProps.putAll(defaultProps.props);
+        for (Entry<Object, Object> entry : overrides.entrySet()) {
+            String key = entry.getKey().toString();
+            String value = entry.getValue().toString();
+            combinedProps.put(key, value);
+        }
+        this.props = ImmutableMap.copyOf(combinedProps);
+    }
+
     private static Pattern varPat = Pattern.compile("\\$\\{[^\\}\\$\u0020]+\\}");
     private static int MAX_SUBST = 20;
 
@@ -269,4 +283,22 @@
     public boolean isEmpty() {
         return props.isEmpty();
     }
+
+    /**
+     * Constructs new map only if necessary for adding the override properties.
+     * @param overrides Map of properties to override current properties.
+     * @return new ReadOnlyProps if in applying the overrides there are
+     * modifications to the current underlying Map, otherwise returns this.
+     */
+    public ReadOnlyProps addAll(Properties overrides) {
+        for (Entry<Object, Object> entry : overrides.entrySet()) {
+            String key = entry.getKey().toString();
+            String value = entry.getValue().toString();
+            String oldValue = props.get(key);
+            if (!Objects.equal(oldValue, value)) {
+                return new ReadOnlyProps(this, overrides);
+            }
+        }
+        return this;
+    }
 }