aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/StringCodecs.java
diff options
context:
space:
mode:
authorGravatar janakr <janakr@google.com>2018-05-21 12:58:59 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-05-21 13:00:32 -0700
commit88f0f85fe775db0aac223050d1454c99e3f9f80e (patch)
treea2fef9444b95f3107afd085487cf08a673b0a284 /src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/StringCodecs.java
parentcaed1a27756d513d437143186771981b5fcfcdcc (diff)
When using JDK9, replace naive StringCodec with an optimized codec that uses the String's underlying raw bytes. This avoids byte copying and UTF interpretation. Experiments show it is approximately 15(!) times faster than the naive StringCodec for serialization and 2 times faster for deserialization (10 times faster for non-ASCII strings).
PiperOrigin-RevId: 197441758
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/StringCodecs.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/StringCodecs.java47
1 files changed, 24 insertions, 23 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/StringCodecs.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/StringCodecs.java
index a5bd332c16..9d696e0eb9 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/StringCodecs.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/StringCodecs.java
@@ -14,43 +14,44 @@
package com.google.devtools.build.lib.skyframe.serialization.strings;
+import static com.google.devtools.build.lib.skyframe.serialization.UnsafeJdk9StringCodec.canUseUnsafeCodec;
+
+import com.google.common.collect.ImmutableList;
+import com.google.devtools.build.lib.skyframe.serialization.CodecRegisterer;
import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
+import com.google.devtools.build.lib.skyframe.serialization.UnsafeJdk9StringCodec;
/** Utility for accessing (potentially platform-specific) {@link String} {@link ObjectCodec}s. */
public final class StringCodecs {
private static final StringCodec stringCodec = new StringCodec();
- private StringCodecs() {}
+ private static final UnsafeJdk9StringCodec unsafeCodec =
+ canUseUnsafeCodec() ? new UnsafeJdk9StringCodec() : null;
/**
- * Returns whether or not optimized codecs are available. Exposed so users can check at runtime
- * if the expected optimizations are applied.
+ * Returns optimized singleton instance, if supported. Otherwise, returns a functional, but not
+ * optimized implementation. Currently supported on JDK9.
*/
- public static boolean supportsOptimizedAscii() {
- return false;
+ public static ObjectCodec<String> asciiOptimized() {
+ return unsafeCodec != null ? unsafeCodec : stringCodec;
}
- /**
- * Returns singleton instance optimized for almost-always ASCII data, if supported. Otherwise,
- * returns a functional, but not optimized implementation. To tell if the optimized version is
- * supported see {@link #supportsOptimizedAscii()}.
- *
- * <p>Note that when optimized, this instance can still serialize/deserialize UTF-8 data, but with
- * potentially worse performance than {@link #simple()}.
- *
- * <p>Currently this is the same as {@link #simple()}, it remains to avoid a time-consuming
- * cleanup and in case we want to revive an optimized version in the near future.
- */
- // TODO(bazel-core): Determine if we need to revive ascii-optimized.
- public static ObjectCodec<String> asciiOptimized() {
- return simple();
+ static class UnsafeStringCodecRegisterer implements CodecRegisterer<UnsafeJdk9StringCodec> {
+ @Override
+ public Iterable<? extends ObjectCodec<?>> getCodecsToRegister() {
+ return canUseUnsafeCodec() ? ImmutableList.of(unsafeCodec) : ImmutableList.of();
+ }
}
- /**
- * Returns singleton instance of basic implementation. Should be preferred over
- * {@link #asciiOptimized()} when a sufficient amount of UTF-8 data is expected.
- */
+ static class SimpleStringCodecRegisterer implements CodecRegisterer<StringCodec> {
+ @Override
+ public Iterable<StringCodec> getCodecsToRegister() {
+ return canUseUnsafeCodec() ? ImmutableList.of() : ImmutableList.of(stringCodec);
+ }
+ }
+
+ /** Returns singleton instance of basic implementation. */
public static ObjectCodec<String> simple() {
return stringCodec;
}