From 9c092753b0dce6ee96a9bd21060c3544d42191e4 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 8 Jun 2017 11:22:23 -0400 Subject: [PATCH] * Updates array constructor and bulk operations to best guess capacity information * Update JSONObject to allow best guess for initial capacity. --- JSONArray.java | 13 ++++++++++--- JSONObject.java | 17 ++++++++++++++--- Property.java | 2 +- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/JSONArray.java b/JSONArray.java index d08586d..0853d00 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -154,7 +154,7 @@ public class JSONArray implements Iterable { * A Collection. */ public JSONArray(Collection collection) { - this.myArrayList = new ArrayList(); + this.myArrayList = new ArrayList(collection == null ? 0 : collection.size()); if (collection != null) { for (Object o: collection){ this.myArrayList.add(JSONObject.wrap(o)); @@ -172,6 +172,7 @@ public class JSONArray implements Iterable { this(); if (array.getClass().isArray()) { int length = Array.getLength(array); + this.myArrayList.ensureCapacity(length); for (int i = 0; i < length; i += 1) { this.put(JSONObject.wrap(Array.get(array, i))); } @@ -495,7 +496,7 @@ public class JSONArray implements Iterable { * Get the optional object value associated with an index. * * @param index - * The index must be between 0 and length() - 1. + * The index must be between 0 and length() - 1. If not, null is returned. * @return An object value, or null if there is no object at that index. */ public Object opt(int index) { @@ -1150,7 +1151,13 @@ public class JSONArray implements Iterable { } if (index < this.length()) { this.myArrayList.set(index, value); + } else if(index == this.length()){ + // simple append + this.put(value); } else { + // if we are inserting past the length, we want to grow the array all at once + // instead of incrementally. + this.myArrayList.ensureCapacity(index + 1); while (index != this.length()) { this.put(JSONObject.NULL); } @@ -1302,7 +1309,7 @@ public class JSONArray implements Iterable { if (names == null || names.length() == 0 || this.length() == 0) { return null; } - JSONObject jo = new JSONObject(); + JSONObject jo = new JSONObject(names.length()); for (int i = 0; i < names.length(); i += 1) { jo.put(names.getString(i), this.opt(i)); } diff --git a/JSONObject.java b/JSONObject.java index ff4acfa..7795fbe 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -178,7 +178,7 @@ public class JSONObject { * An array of strings. */ public JSONObject(JSONObject jo, String[] names) { - this(); + this(names.length); for (int i = 0; i < names.length; i += 1) { try { this.putOnce(names[i], jo.opt(names[i])); @@ -250,7 +250,7 @@ public class JSONObject { * the JSONObject. */ public JSONObject(Map m) { - this.map = new HashMap(); + this.map = new HashMap(m == null ? 0 : m.size()); if (m != null) { for (final Entry e : m.entrySet()) { final Object value = e.getValue(); @@ -302,7 +302,7 @@ public class JSONObject { * from the object. */ public JSONObject(Object object, String names[]) { - this(); + this(names.length); Class c = object.getClass(); for (int i = 0; i < names.length; i += 1) { String name = names[i]; @@ -371,6 +371,17 @@ public class JSONObject { } } } + + /** + * Constructor to specify an initial capacity of the internal map. Useful for library + * internal calls where we know, or at least can best guess, how big this JSONObject + * will be. + * + * @param initialCapacity initial capacity of the internal map. + */ + protected JSONObject(int initialCapacity){ + this.map = new HashMap(initialCapacity); + } /** * Accumulate values under a key. It is similar to the put method except diff --git a/Property.java b/Property.java index 4f1d7c4..51b97ed 100644 --- a/Property.java +++ b/Property.java @@ -41,7 +41,7 @@ public class Property { * @throws JSONException */ public static JSONObject toJSONObject(java.util.Properties properties) throws JSONException { - JSONObject jo = new JSONObject(); + JSONObject jo = new JSONObject(properties == null ? 0 : properties.size()); if (properties != null && !properties.isEmpty()) { Enumeration enumProperties = properties.propertyNames(); while(enumProperties.hasMoreElements()) {