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

Optimize InstantDeserializer method replaceZeroOffsetAsZIfNecessary() #266

Merged
merged 3 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ public class InstantDeserializer<T extends Temporal>
{
private static final long serialVersionUID = 1L;

/**
* Constants used to check if the time offset is zero. See [jackson-modules-java8#18]
*
* @since 2.9.0
*/
private static final Pattern ISO8601_UTC_ZERO_OFFSET_SUFFIX_REGEX = Pattern.compile("\\+00:?(00)?$");

/**
* Constants used to check if ISO 8601 time string is colonless. See [jackson-modules-java8#131]
*
Expand Down Expand Up @@ -226,7 +219,7 @@ public T deserialize(JsonParser parser, DeserializationContext context) throws I
return (T) parser.getEmbeddedObject();

case JsonTokenId.ID_START_ARRAY:
return _deserializeFromArray(parser, context);
return _deserializeFromArray(parser, context);
}
return _handleUnexpectedToken(context, parser, JsonToken.VALUE_STRING,
JsonToken.VALUE_NUMBER_INT, JsonToken.VALUE_NUMBER_FLOAT);
Expand Down Expand Up @@ -337,12 +330,31 @@ private ZoneId getZone(DeserializationContext context)
private String replaceZeroOffsetAsZIfNecessary(String text)
{
if (replaceZeroOffsetAsZ) {
return ISO8601_UTC_ZERO_OFFSET_SUFFIX_REGEX.matcher(text).replaceFirst("Z");
return replaceZeroOffsetAsZ(text);
}

return text;
}

private static String replaceZeroOffsetAsZ(String text)
{
int plusIndex = text.lastIndexOf('+');
if (plusIndex < 0) {
return text;
}
int maybeOffsetIndex = plusIndex + 1;
int remaining = text.length() - maybeOffsetIndex;
if (remaining < 2 || remaining == 3 || remaining > 5) {
return text;
}

String maybeOffset = text.substring(maybeOffsetIndex);
if ("00".equals(maybeOffset) || "0000".equals(maybeOffset) || "00:00".equals(maybeOffset)) {
Copy link
Contributor

@carterkozak carterkozak Feb 9, 2023

Choose a reason for hiding this comment

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

Can we optimize this using String.regionMatches?
perhaps using something along the lines of

switch (remaining) {
    case 2:
       return text.regionmatches..."00" ? text.substring(0, plusIndex) + 'Z' : text;
     case 4:
        ..etc
     case 5:
       ..etc
 }
 return text;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems reasonable to avoid the substring allocation, updated to use regionMatches & switch and reran JMH and tested locally with JDKs 8, 11, 17.

Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz
JDK 17.0.5, OpenJDK 64-Bit Server VM, 17.0.5+8-LTS
Benchmark                           Mode  Cnt    Score   Error  Units
InstantDeserializerBenchmark.index  avgt    5   20.003 ± 0.486  ns/op
InstantDeserializerBenchmark.regex  avgt    5  156.174 ± 7.654  ns/op
Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz
JDK 11.0.16.1, OpenJDK 64-Bit Server VM, 11.0.16.1+9-LTS
Benchmark                           Mode  Cnt    Score   Error  Units
InstantDeserializerBenchmark.index  avgt    5   20.818 ± 1.267  ns/op
InstantDeserializerBenchmark.regex  avgt    5  192.248 ± 7.341  ns/op
Apple M1 Pro aarch64
JDK 17.0.6, OpenJDK 64-Bit Server VM, 17.0.6+10-LTS
Benchmark                           Mode  Cnt   Score   Error  Units
InstantDeserializerBenchmark.index  avgt    5  17.498 ± 0.677  ns/op
InstantDeserializerBenchmark.regex  avgt    5  58.096 ± 1.330  ns/op
Apple M1 Pro aarch64
Benchmark                           Mode  Cnt   Score   Error  Units
InstantDeserializerBenchmark.index  avgt    5  16.176 ± 7.842  ns/op
InstantDeserializerBenchmark.regex  avgt    5  63.201 ± 5.668  ns/op

return text.substring(0, plusIndex) + 'Z';
}
return text;
}

// @since 2.13
private String addInColonToOffsetIfMissing(String text)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer.ISO8601_COLONLESS_OFFSET_REGEX;
import static org.junit.Assert.*;
import static org.junit.Assert.assertNull;
import static org.junit.Assume.assumeTrue;

public class InstantDeserTest extends ModuleTestBase
{
Expand Down Expand Up @@ -434,10 +435,43 @@ public void testDeserializationFromStringWithZeroZoneOffset03() throws Exception
assertEquals("The value is not correct.", date, result);
}

@Test
public void testDeserializationFromStringWithZeroZoneOffset04() throws Exception {
assumeInstantCanParseOffsets();
Instant date = Instant.now();
String json = formatWithZeroZoneOffset(date, "+00:30");
Instant result = READER.readValue(json);
assertNotEquals("The value is not correct.", date, result);
}

@Test
public void testDeserializationFromStringWithZeroZoneOffset05() throws Exception {
assumeInstantCanParseOffsets();
Instant date = Instant.now();
String json = formatWithZeroZoneOffset(date, "+01:30");
Instant result = READER.readValue(json);
assertNotEquals("The value is not correct.", date, result);
}

@Test
public void testDeserializationFromStringWithZeroZoneOffset06() throws Exception {
assumeInstantCanParseOffsets();
Instant date = Instant.now();
String json = formatWithZeroZoneOffset(date, "-00:00");
Instant result = READER.readValue(json);
assertEquals("The value is not correct.", date, result);
}

private String formatWithZeroZoneOffset(Instant date, String offset){
return '"' + FORMATTER.format(date).replaceFirst("Z$", offset) + '"';
}

private static void assumeInstantCanParseOffsets() {
// DateTimeFormatter.ISO_INSTANT didn't handle offsets until JDK 12+.
// This was added by https://bugs.openjdk.org/browse/JDK-8166138
assumeTrue(System.getProperty("java.specification.version").compareTo("12") > 0);
}

/*
/**********************************************************************
/* Deserialization, misc other
Expand Down