Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonArray optimizations #4995

Closed
wants to merge 14 commits into from
33 changes: 26 additions & 7 deletions src/main/java/io/vertx/core/json/JsonArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.*;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static io.vertx.core.json.impl.JsonUtil.*;
import static java.time.format.DateTimeFormatter.ISO_INSTANT;
Expand Down Expand Up @@ -65,16 +66,27 @@ public JsonArray() {
list = new ArrayList<>();
}

/**
* Create an empty instance with initialCapacity
*/
public JsonArray(int initialCapacity) {
list = new ArrayList<>(initialCapacity);
}

/**
* Create an instance from a List. The List is not copied.
*
* @param list the underlying backing list
*/
public JsonArray(List list) {
if (list == null) {
@SuppressWarnings({"rawtypes", "unchecked"})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is an avoidable breaking change, we need to keep the List constructor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it really break?

I just thought widening is not a problem 🤷‍♀️

public JsonArray(Collection list) {
if (list instanceof List) {
this.list = (List) list;
} else if (list == null) {
throw new NullPointerException();
} else {
this.list = new ArrayList(list);
}
this.list = list;
}

/**
Expand All @@ -100,12 +112,13 @@ public JsonArray(Buffer buf) {
*/
public static JsonArray of(Object... values) {
// implicit nullcheck of values
if (values.length == 0) {
int len = values.length;
if (len == 0) {
return new JsonArray();
}

JsonArray arr = new JsonArray(new ArrayList<>(values.length));
for(int i = 0; i< values.length; ++i) {
JsonArray arr = new JsonArray(new ArrayList<>(len));
for (int i = 0; i < len; ++i) {
arr.add(values[i]);
}

Expand Down Expand Up @@ -616,7 +629,13 @@ public JsonArray copy(Function<Object, ?> cloner) {
* @return a Stream
*/
public Stream<Object> stream() {
return asStream(iterator());
// JsonUtil.asStream(iterator()) is too generic
return StreamSupport.stream(spliterator(), false);
}

@Override
public Spliterator<Object> spliterator() {
return Spliterators.spliterator(iterator(), list.size(), Spliterator.ORDERED);
}

@Override
Expand Down
98 changes: 52 additions & 46 deletions src/main/java/io/vertx/core/json/JsonObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.*;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static io.vertx.core.json.impl.JsonUtil.*;
import static java.time.format.DateTimeFormatter.ISO_INSTANT;
Expand Down Expand Up @@ -54,11 +55,23 @@ public JsonObject(String json) {
}
}

@SuppressWarnings("unchecked")
private static <T> Map<String,T> map(int expectedSize) {
return (Map<String,T>) MAP_CREATOR.apply(expectedSize);
}

/**
* Create a new, empty instance
*/
public JsonObject() {
map = new LinkedHashMap<>();
map = map(0);// => 3
}

/**
* Create a new, empty instance with expected size (initial capacity will be calculated)
*/
public JsonObject(int expectedSize) {
map = map(expectedSize);
}

/**
Expand Down Expand Up @@ -105,7 +118,7 @@ public static JsonObject of() {
* @return a JsonObject containing the specified mapping.
*/
public static JsonObject of(String k1, Object v1) {
JsonObject obj = new JsonObject(new LinkedHashMap<>(1));
JsonObject obj = new JsonObject(map(1));

obj.put(k1, v1);

Expand All @@ -122,7 +135,7 @@ public static JsonObject of(String k1, Object v1) {
* @return a JsonObject containing the specified mappings.
*/
public static JsonObject of(String k1, Object v1, String k2, Object v2) {
JsonObject obj = new JsonObject(new LinkedHashMap<>(2));
JsonObject obj = new JsonObject(map(2));

obj.put(k1, v1);
obj.put(k2, v2);
Expand All @@ -142,7 +155,7 @@ public static JsonObject of(String k1, Object v1, String k2, Object v2) {
* @return a JsonObject containing the specified mappings.
*/
public static JsonObject of(String k1, Object v1, String k2, Object v2, String k3, Object v3) {
JsonObject obj = new JsonObject(new LinkedHashMap<>(3));
JsonObject obj = new JsonObject(map(3));

obj.put(k1, v1);
obj.put(k2, v2);
Expand All @@ -166,7 +179,7 @@ public static JsonObject of(String k1, Object v1, String k2, Object v2, String k
*/
public static JsonObject of(String k1, Object v1, String k2, Object v2, String k3, Object v3,
String k4, Object v4) {
JsonObject obj = new JsonObject(new LinkedHashMap<>(4));
JsonObject obj = new JsonObject(map(4));

obj.put(k1, v1);
obj.put(k2, v2);
Expand All @@ -193,7 +206,7 @@ public static JsonObject of(String k1, Object v1, String k2, Object v2, String k
*/
public static JsonObject of(String k1, Object v1, String k2, Object v2, String k3, Object v3,
String k4, Object v4, String k5, Object v5) {
JsonObject obj = new JsonObject(new LinkedHashMap<>(5));
JsonObject obj = new JsonObject(map(5));

obj.put(k1, v1);
obj.put(k2, v2);
Expand Down Expand Up @@ -223,7 +236,7 @@ public static JsonObject of(String k1, Object v1, String k2, Object v2, String k
*/
public static JsonObject of(String k1, Object v1, String k2, Object v2, String k3, Object v3,
String k4, Object v4, String k5, Object v5, String k6, Object v6) {
JsonObject obj = new JsonObject(new LinkedHashMap<>(6));
JsonObject obj = new JsonObject(map(6));

obj.put(k1, v1);
obj.put(k2, v2);
Expand Down Expand Up @@ -257,7 +270,7 @@ public static JsonObject of(String k1, Object v1, String k2, Object v2, String k
public static JsonObject of(String k1, Object v1, String k2, Object v2, String k3, Object v3,
String k4, Object v4, String k5, Object v5, String k6, Object v6,
String k7, Object v7) {
JsonObject obj = new JsonObject(new LinkedHashMap<>(7));
JsonObject obj = new JsonObject(map(7));

obj.put(k1, v1);
obj.put(k2, v2);
Expand Down Expand Up @@ -294,7 +307,7 @@ public static JsonObject of(String k1, Object v1, String k2, Object v2, String k
public static JsonObject of(String k1, Object v1, String k2, Object v2, String k3, Object v3,
String k4, Object v4, String k5, Object v5, String k6, Object v6,
String k7, Object v7, String k8, Object v8) {
JsonObject obj = new JsonObject(new LinkedHashMap<>(8));
JsonObject obj = new JsonObject(map(8));

obj.put(k1, v1);
obj.put(k2, v2);
Expand Down Expand Up @@ -334,7 +347,7 @@ public static JsonObject of(String k1, Object v1, String k2, Object v2, String k
public static JsonObject of(String k1, Object v1, String k2, Object v2, String k3, Object v3,
String k4, Object v4, String k5, Object v5, String k6, Object v6,
String k7, Object v7, String k8, Object v8, String k9, Object v9) {
JsonObject obj = new JsonObject(new LinkedHashMap<>(9));
JsonObject obj = new JsonObject(map(9));

obj.put(k1, v1);
obj.put(k2, v2);
Expand Down Expand Up @@ -378,7 +391,7 @@ public static JsonObject of(String k1, Object v1, String k2, Object v2, String k
String k4, Object v4, String k5, Object v5, String k6, Object v6,
String k7, Object v7, String k8, Object v8, String k9, Object v9,
String k10, Object v10) {
JsonObject obj = new JsonObject(new LinkedHashMap<>(10));
JsonObject obj = new JsonObject(map(10));

obj.put(k1, v1);
obj.put(k2, v2);
Expand Down Expand Up @@ -1075,10 +1088,13 @@ public JsonObject copy() {
*/
public JsonObject copy(Function<Object, ?> cloner) {
Map<String, Object> copiedMap;
if (map instanceof LinkedHashMap) {
copiedMap = new LinkedHashMap<>(map.size());
int capacity = map.size() * 4 / 3 + 1;
if (map instanceof LinkedHashMap){
copiedMap = new LinkedHashMap<>(capacity);
} else if (map instanceof SortedMap){
copiedMap = new TreeMap<>();
} else {
copiedMap = new HashMap<>(map.size());
copiedMap = new HashMap<>(capacity);
}
for (Map.Entry<String, Object> entry : map.entrySet()) {
Object val = deepCopy(entry.getValue(), cloner);
Expand Down Expand Up @@ -1114,9 +1130,17 @@ public Map<String, Object> getMap() {
* @return a Stream
*/
public Stream<Map.Entry<String, Object>> stream() {
return asStream(iterator());
// JsonUtil.asStream(iterator()) is too generic
return StreamSupport.stream(spliterator(), false);
}


@Override
public Spliterator<Map.Entry<String,Object>> spliterator() {
return Spliterators.spliterator(iterator(), map.size(), Spliterator.DISTINCT | Spliterator.NONNULL);
}


/**
* Get an Iterator of the entries in the JSON object.
*
Expand Down Expand Up @@ -1158,16 +1182,21 @@ public String toString() {
return encode();
}

/**
How to Write an Equality Method in Java
https://www.artima.com/articles/how-to-write-an-equality-method-in-java
*/
protected boolean canEqual(Object other) {
return getClass() == other.getClass();
}

@Override
public boolean equals(Object o) {
// null check
if (o == null)
return false;
// self check
if (this == o)
return true;
// type check and cast
if (getClass() != o.getClass())
// type check, cast, null check
if (!(o instanceof JsonObject && ((JsonObject)o).canEqual(this)))
return false;

JsonObject other = (JsonObject) o;
Expand Down Expand Up @@ -1275,7 +1304,9 @@ public Map.Entry<String, Object> next() {
final Object wrapped = wrapJsonValue(val);

if (val != wrapped) {
return new Entry(entry.getKey(), wrapped);
// Map.entry disallows null keys and values: we disallow null keys,
// (val != wrapped) skips null values (wrapJsonValue doesn't wrap null)
return Map.entry(entry.getKey(), wrapped);
}

return entry;
Expand All @@ -1286,29 +1317,4 @@ public void remove() {
mapIter.remove();
}
}

private static final class Entry implements Map.Entry<String, Object> {
final String key;
final Object value;

public Entry(String key, Object value) {
this.key = key;
this.value = value;
}

@Override
public String getKey() {
return key;
}

@Override
public Object getValue() {
return value;
}

@Override
public Object setValue(Object value) {
throw new UnsupportedOperationException();
}
}
}
22 changes: 16 additions & 6 deletions src/main/java/io/vertx/core/json/impl/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

import java.time.Instant;
import java.util.Base64;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

Expand Down Expand Up @@ -63,10 +66,6 @@ public final class JsonUtil {
* @return wrapped type or {@code val} if not applicable.
*/
public static Object wrapJsonValue(Object val) {
if (val == null) {
return null;
}

// perform wrapping
if (val instanceof Map) {
val = new JsonObject((Map) val);
Expand All @@ -81,14 +80,20 @@ public static Object wrapJsonValue(Object val) {
} else if (val instanceof Enum) {
val = ((Enum) val).name();
}

return val;
return val; // includes null
}

public static final Function<Object, ?> DEFAULT_CLONER = o -> {
throw new IllegalStateException("Illegal type in Json: " + o.getClass());
};

// todo replace with HashMap? → Less memory usage, but random keys order, but .stream().sorted()?
// immutable Map.of javadoc: The iteration order of mappings is unspecified and is subject to change
@SuppressWarnings("rawtypes")
public static final IntFunction<Map> MAP_CREATOR = size -> new LinkedHashMap(size < 3
? 3
: size * 4 / 3 + 1);// ~ size / DEFAULT_LOAD_FACTOR(0.75) + 1

@SuppressWarnings("unchecked")
public static Object deepCopy(Object val, Function<Object, ?> copier) {
if (val == null) {
Expand Down Expand Up @@ -124,6 +129,11 @@ public static Object deepCopy(Object val, Function<Object, ?> copier) {
return val;
}

/**
@deprecated too generic
@see Iterable#spliterator()
*/
@Deprecated
public static <T> Stream<T> asStream(Iterator<T> sourceIterator) {
Iterable<T> iterable = () -> sourceIterator;
return StreamSupport.stream(iterable.spliterator(), false);
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/io/vertx/core/json/JsonObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package io.vertx.core.json;

import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.impl.JsonUtil;
import io.vertx.core.shareddata.Shareable;
import io.vertx.test.core.TestUtils;
import org.junit.Before;
Expand Down Expand Up @@ -1973,4 +1974,22 @@ public void testJsonObjectOfArgs() {
public void testJsonObjectOfEmpty() {
assertEquals(new JsonObject(), JsonObject.of());
}

@Test
public void testNull() {
assertEquals(JsonObject.of(), JsonObject.of());
assertFalse(JsonObject.of().equals(null));
assertFalse(JsonObject.of().equals(new JsonObject(){}));
assertNull(JsonUtil.wrapJsonValue(null));

JsonObject jo = JsonObject.of("k", null, "", null);

AtomicInteger cnt = new AtomicInteger();
jo.iterator().forEachRemaining(e -> {
assertNotNull(e.getKey());
assertNull(e.getValue());// null is ok
cnt.incrementAndGet();
});
assertEquals(2, cnt.get());
}
}
Loading