mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
Change Java JSON to Avaje Jsonb (#8721)
Jackson is a very heavy library; it supports loads of features that we don't need, and historically has caused issues due to long class loading times (a little over 2 seconds to load AprilTagFieldLayout). This often manifests as a help request in the form of "my robot disables when I do X, but doesn't disable when doing X in subsequent attempts until code restart." While SC has brought down Jackson loading times significantly, with AprilTagFieldLayout loads taking only 330 milliseconds, that's still a rather long delay, and while libraries should handle any JSON loading ahead of time to prevent delays in auto/teleop, it would still be good to make the worst case better to reduce user frustration. Benchmarks indicate using [Avaje Jsonb](https://github.com/avaje/avaje-jsonb) to load AprilTagFieldLayout only takes ~70 ms, a fair chunk of which isn't actually in Avaje Jsonb (~4 ms is spent on using getResourceAsStream to retrieve the JSON file, ~8 ms is spent on just loading the AprilTag class and its dependencies). Note that all times listed are end-to-end, meaning nothing else was done except for the operation being benchmarked, and doing arithmetic on them can be flawed due to some classes being loaded twice, i.e., getResourceAsStream and `new AprilTag()` likely load some of the same JDK classes and so subtracting both from the Avaje Jsonb load time is likely slightly incorrect because class loading is being double counted. For our purposes, it's likely accurate enough and is mostly just for contextualization. Benchmarks were run on a Raspberry Pi CM5 with 2 GB of RAM. Source code for the [results](https://github.com/user-attachments/files/26471452/benchmark.txt) can be found in the "Fastjson2" commit (2456d15ca8ebd17635e607cd40bf8816e77869a1). Avaje Jsonb uses code generation via annotation processors to generate the classes needed to do JSON serde and uses service providers to find them, which will require downstream changes in robot projects, as the different service providers in each library must be merged together for Avaje Jsonb to function. We will use the Gradle shadow plugin, as its already used by the installer and therefore adds zero additional dependencies.
This commit is contained in:
@@ -38,12 +38,15 @@ wpilib_java_library(
|
||||
extra_source_pkgs = ["resources"],
|
||||
maven_artifact_name = "fields-java",
|
||||
maven_group_id = "org.wpilib.fields",
|
||||
plugins = [
|
||||
"//:avaje_jsonb_generator",
|
||||
],
|
||||
resource_strip_prefix = "fields/src/main/native/resources",
|
||||
resources = glob(["src/main/native/resources/**"]),
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"@maven//:com_fasterxml_jackson_core_jackson_annotations",
|
||||
"@maven//:com_fasterxml_jackson_core_jackson_databind",
|
||||
"@maven//:io_avaje_avaje_json_core",
|
||||
"@maven//:io_avaje_avaje_jsonb",
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ include(GenResources)
|
||||
if(WITH_JAVA)
|
||||
include(UseJava)
|
||||
|
||||
file(GLOB JACKSON_JARS "${WPILIB_BINARY_DIR}/wpiutil/thirdparty/jackson/*.jar")
|
||||
file(GLOB AVAJE_JARS "${WPILIB_BINARY_DIR}/wpiutil/thirdparty/avaje/*.jar")
|
||||
|
||||
file(GLOB_RECURSE JAVA_SOURCES src/main/java/*.java)
|
||||
file(
|
||||
@@ -21,7 +21,7 @@ if(WITH_JAVA)
|
||||
SOURCES ${JAVA_SOURCES}
|
||||
RESOURCES
|
||||
NAMESPACE "org/wpilib/fields" ${JAVA_RESOURCES}
|
||||
INCLUDE_JARS ${JACKSON_JARS}
|
||||
INCLUDE_JARS ${AVAJE_JARS}
|
||||
OUTPUT_DIR ${WPILIB_BINARY_DIR}/${java_lib_dest}
|
||||
OUTPUT_NAME fields
|
||||
)
|
||||
|
||||
@@ -14,7 +14,8 @@ if (OperatingSystem.current().isWindows()) {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation libs.bundles.jackson
|
||||
annotationProcessor libs.avaje.jsonb.generator
|
||||
implementation libs.avaje.jsonb
|
||||
}
|
||||
|
||||
ext {
|
||||
|
||||
@@ -4,39 +4,38 @@
|
||||
|
||||
package org.wpilib.fields;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.avaje.jsonb.Json;
|
||||
import io.avaje.jsonb.Jsonb;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
@Json
|
||||
public class FieldConfig {
|
||||
public static class Corners {
|
||||
@JsonProperty("top-left")
|
||||
@Json.Property("top-left")
|
||||
public double[] m_topLeft;
|
||||
|
||||
@JsonProperty("bottom-right")
|
||||
@Json.Property("bottom-right")
|
||||
public double[] m_bottomRight;
|
||||
}
|
||||
|
||||
@JsonProperty("game")
|
||||
@Json.Property("game")
|
||||
public String m_game;
|
||||
|
||||
@JsonProperty("field-image")
|
||||
@Json.Property("field-image")
|
||||
public String m_fieldImage;
|
||||
|
||||
@JsonProperty("field-corners")
|
||||
@Json.Property("field-corners")
|
||||
public Corners m_fieldCorners;
|
||||
|
||||
@JsonProperty("field-size")
|
||||
@Json.Property("field-size")
|
||||
public double[] m_fieldSize;
|
||||
|
||||
@JsonProperty("field-unit")
|
||||
@Json.Property("field-unit")
|
||||
public String m_fieldUnit;
|
||||
|
||||
public FieldConfig() {}
|
||||
@@ -69,7 +68,7 @@ public class FieldConfig {
|
||||
*/
|
||||
public static FieldConfig loadFromFile(Path file) throws IOException {
|
||||
try (BufferedReader reader = Files.newBufferedReader(file)) {
|
||||
return new ObjectMapper().readerFor(FieldConfig.class).readValue(reader);
|
||||
return Jsonb.instance().type(FieldConfig.class).fromJson(reader);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,9 +80,8 @@ public class FieldConfig {
|
||||
* @throws IOException Throws if the resource could not be loaded
|
||||
*/
|
||||
public static FieldConfig loadFromResource(String resourcePath) throws IOException {
|
||||
try (InputStream stream = FieldConfig.class.getResourceAsStream(resourcePath);
|
||||
InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
|
||||
return new ObjectMapper().readerFor(FieldConfig.class).readValue(reader);
|
||||
try (InputStream stream = FieldConfig.class.getResourceAsStream(resourcePath)) {
|
||||
return Jsonb.instance().type(FieldConfig.class).fromJson(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user