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

JsonObject optimizations #5001

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
96 changes: 45 additions & 51 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 @@ -48,17 +49,32 @@ public JsonObject(String json) {
if (json == null) {
throw new NullPointerException();
}
fromJson(json);
map = Json.CODEC.fromString(json, Map.class);
if (map == null) {
throw new DecodeException("Invalid JSON object: " + json);
}
}

protected Map<String, Object> map(int expectedSize) {
// 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
return new LinkedHashMap<>(expectedSize < 3
? 3
: expectedSize * 4 / 3 + 1);// ~ size / DEFAULT_LOAD_FACTOR(0.75) + 1
}

/**
* 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 All @@ -82,7 +98,7 @@ public JsonObject(Buffer buf) {
if (buf == null) {
throw new NullPointerException();
}
fromBuffer(buf);
map = Json.CODEC.fromBuffer(buf, Map.class);
if (map == null) {
throw new DecodeException("Invalid JSON object: " + buf);
}
Expand All @@ -105,7 +121,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(1);

obj.put(k1, v1);

Expand All @@ -122,7 +138,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(2);

obj.put(k1, v1);
obj.put(k2, v2);
Expand All @@ -142,7 +158,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(3);

obj.put(k1, v1);
obj.put(k2, v2);
Expand All @@ -166,7 +182,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(4);

obj.put(k1, v1);
obj.put(k2, v2);
Expand All @@ -193,7 +209,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(5);

obj.put(k1, v1);
obj.put(k2, v2);
Expand Down Expand Up @@ -223,7 +239,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(6);

obj.put(k1, v1);
obj.put(k2, v2);
Expand Down Expand Up @@ -257,7 +273,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(7);

obj.put(k1, v1);
obj.put(k2, v2);
Expand Down Expand Up @@ -294,7 +310,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(8);

obj.put(k1, v1);
obj.put(k2, v2);
Expand Down Expand Up @@ -334,7 +350,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(9);

obj.put(k1, v1);
obj.put(k2, v2);
Expand Down Expand Up @@ -378,7 +394,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(10);

obj.put(k1, v1);
obj.put(k2, v2);
Expand Down Expand Up @@ -1075,10 +1091,13 @@ public JsonObject copy() {
*/
public JsonObject copy(Function<Object, ?> cloner) {
Map<String, Object> copiedMap;
int capacity = map.size() * 4 / 3 + 1;
if (map instanceof LinkedHashMap) {
copiedMap = new LinkedHashMap<>(map.size());
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,7 +1133,13 @@ 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);
}

/**
Expand Down Expand Up @@ -1242,16 +1267,8 @@ public int readFromBuffer(int pos, Buffer buffer) {
int length = buffer.getInt(pos);
int start = pos + 4;
Buffer buf = buffer.getBuffer(start, start + length);
fromBuffer(buf);
return pos + length + 4;
}

private void fromJson(String json) {
map = Json.CODEC.fromString(json, Map.class);
}

private void fromBuffer(Buffer buf) {
map = Json.CODEC.fromBuffer(buf, Map.class);
return pos + length + 4;
}

private static class Iter implements Iterator<Map.Entry<String, Object>> {
Expand All @@ -1275,7 +1292,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 +1305,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();
}
}
}
45 changes: 45 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,48 @@ 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());

assertEquals("=null, k=null",
jo.stream().sorted(Map.Entry.comparingByKey())
.map(e -> e.getKey()+"="+e.getValue()).collect(Collectors.joining(", ")));
}

@Test
public void testBuffer() {
JsonObject jo = new JsonObject("{\"k\": 1}");
assertEquals("{\"k\":1}", jo.toString());
assertEquals("{\"k\":1}", jo.toBuffer().toString());
assertEquals("{\"k\":1}", jo.toBuffer().toJson().toString());

Buffer buf = Buffer.buffer();
buf.appendLong(Long.MIN_VALUE);
jo.writeToBuffer(buf);
JsonObject jo0 = JsonObject.of("aaa", 111, "BBB", 222);
jo0.writeToBuffer(buf);

assertEquals(Long.MIN_VALUE, buf.getLong(0));
JsonObject jo1 = JsonObject.of("x", 0);
int index = jo1.readFromBuffer(8, buf);
assertEquals(jo, jo1);
JsonObject jo2 = JsonObject.of("x", 0);
jo2.readFromBuffer(index, buf);
assertEquals(jo0, jo2);
}
}