Skip to content

Commit 9c09275

Browse files
author
John J. Aylward
committed
* Updates array constructor and bulk operations to best guess capacity information
* Update JSONObject to allow best guess for initial capacity.
1 parent ef7a5e4 commit 9c09275

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

JSONArray.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public JSONArray(String source) throws JSONException {
154154
* A Collection.
155155
*/
156156
public JSONArray(Collection<?> collection) {
157-
this.myArrayList = new ArrayList<Object>();
157+
this.myArrayList = new ArrayList<Object>(collection == null ? 0 : collection.size());
158158
if (collection != null) {
159159
for (Object o: collection){
160160
this.myArrayList.add(JSONObject.wrap(o));
@@ -172,6 +172,7 @@ public JSONArray(Object array) throws JSONException {
172172
this();
173173
if (array.getClass().isArray()) {
174174
int length = Array.getLength(array);
175+
this.myArrayList.ensureCapacity(length);
175176
for (int i = 0; i < length; i += 1) {
176177
this.put(JSONObject.wrap(Array.get(array, i)));
177178
}
@@ -495,7 +496,7 @@ public int length() {
495496
* Get the optional object value associated with an index.
496497
*
497498
* @param index
498-
* The index must be between 0 and length() - 1.
499+
* The index must be between 0 and length() - 1. If not, null is returned.
499500
* @return An object value, or null if there is no object at that index.
500501
*/
501502
public Object opt(int index) {
@@ -1150,7 +1151,13 @@ public JSONArray put(int index, Object value) throws JSONException {
11501151
}
11511152
if (index < this.length()) {
11521153
this.myArrayList.set(index, value);
1154+
} else if(index == this.length()){
1155+
// simple append
1156+
this.put(value);
11531157
} else {
1158+
// if we are inserting past the length, we want to grow the array all at once
1159+
// instead of incrementally.
1160+
this.myArrayList.ensureCapacity(index + 1);
11541161
while (index != this.length()) {
11551162
this.put(JSONObject.NULL);
11561163
}
@@ -1302,7 +1309,7 @@ public JSONObject toJSONObject(JSONArray names) throws JSONException {
13021309
if (names == null || names.length() == 0 || this.length() == 0) {
13031310
return null;
13041311
}
1305-
JSONObject jo = new JSONObject();
1312+
JSONObject jo = new JSONObject(names.length());
13061313
for (int i = 0; i < names.length(); i += 1) {
13071314
jo.put(names.getString(i), this.opt(i));
13081315
}

JSONObject.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public JSONObject() {
178178
* An array of strings.
179179
*/
180180
public JSONObject(JSONObject jo, String[] names) {
181-
this();
181+
this(names.length);
182182
for (int i = 0; i < names.length; i += 1) {
183183
try {
184184
this.putOnce(names[i], jo.opt(names[i]));
@@ -250,7 +250,7 @@ public JSONObject(JSONTokener x) throws JSONException {
250250
* the JSONObject.
251251
*/
252252
public JSONObject(Map<?, ?> m) {
253-
this.map = new HashMap<String, Object>();
253+
this.map = new HashMap<String, Object>(m == null ? 0 : m.size());
254254
if (m != null) {
255255
for (final Entry<?, ?> e : m.entrySet()) {
256256
final Object value = e.getValue();
@@ -302,7 +302,7 @@ public JSONObject(Object bean) {
302302
* from the object.
303303
*/
304304
public JSONObject(Object object, String names[]) {
305-
this();
305+
this(names.length);
306306
Class<?> c = object.getClass();
307307
for (int i = 0; i < names.length; i += 1) {
308308
String name = names[i];
@@ -371,6 +371,17 @@ public JSONObject(String baseName, Locale locale) throws JSONException {
371371
}
372372
}
373373
}
374+
375+
/**
376+
* Constructor to specify an initial capacity of the internal map. Useful for library
377+
* internal calls where we know, or at least can best guess, how big this JSONObject
378+
* will be.
379+
*
380+
* @param initialCapacity initial capacity of the internal map.
381+
*/
382+
protected JSONObject(int initialCapacity){
383+
this.map = new HashMap<String, Object>(initialCapacity);
384+
}
374385

375386
/**
376387
* Accumulate values under a key. It is similar to the put method except

Property.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class Property {
4141
* @throws JSONException
4242
*/
4343
public static JSONObject toJSONObject(java.util.Properties properties) throws JSONException {
44-
JSONObject jo = new JSONObject();
44+
JSONObject jo = new JSONObject(properties == null ? 0 : properties.size());
4545
if (properties != null && !properties.isEmpty()) {
4646
Enumeration<?> enumProperties = properties.propertyNames();
4747
while(enumProperties.hasMoreElements()) {

0 commit comments

Comments
 (0)