diff options
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.java | 36 |
1 files changed, 30 insertions, 6 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 24f36ecb07..60e5f29f87 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 @@ -15,22 +15,46 @@ package com.google.devtools.build.lib.skyframe.serialization.strings; import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec; +import java.util.logging.Logger; /** Utility for accessing (potentially platform-specific) {@link String} {@link ObjectCodec}s. */ public final class StringCodecs { - private static final FastStringCodec fastStringCodec = new FastStringCodec(); - private static final StringCodec stringCodec = new StringCodec(); + private static final Logger logger = Logger.getLogger(StringCodecs.class.getName()); + + private static final StringCodec stringCodec; + private static final ObjectCodec<String> asciiOptimized; + + static { + stringCodec = new StringCodec(); + if (FastStringCodec.isAvailable()) { + asciiOptimized = new FastStringCodec(); + } else { + logger.warning("Optimized string deserialization unavailable"); + asciiOptimized = stringCodec; + } + } private StringCodecs() {} /** - * Returns singleton instance optimized for almost-always ASCII data. This instance can still - * serialize/deserialize UTF-8 data, but with potentially worse performance than - * {@link #simple()}. + * Returns whether or not optimized codecs are available. Exposed so users can check at runtime + * if the expected optimizations are applied. + */ + public static boolean supportsOptimizedAscii() { + return asciiOptimized instanceof FastStringCodec; + } + + /** + * 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()}. */ public static ObjectCodec<String> asciiOptimized() { - return fastStringCodec; + return asciiOptimized; } /** |