You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/10/08 08:12:28 UTC

[GitHub] [pulsar] Jason918 commented on a diff in pull request #17948: [improve][java-client][issue-17931]Reduce call of hashFunction in SchemaHash

Jason918 commented on code in PR #17948:
URL: https://github.com/apache/pulsar/pull/17948#discussion_r990609359


##########
pulsar-common/src/main/java/org/apache/pulsar/common/protocol/schema/SchemaHash.java:
##########
@@ -55,15 +82,118 @@ public static SchemaHash of(SchemaData schemaData) {
     }
 
     public static SchemaHash of(SchemaInfo schemaInfo) {
-        return of(schemaInfo == null ? new byte[0] : schemaInfo.getSchema(),
+        return of(schemaInfo == null ? null : schemaInfo.getSchema(),
                 schemaInfo == null ? null : schemaInfo.getType());
     }
 
-    public static SchemaHash of(byte[] schemaBytes, SchemaType schemaType) {
-        return new SchemaHash(hashFunction.hashBytes(schemaBytes), schemaType);
+    public static SchemaHash of() {
+        return of(null, null);
+    }
+
+    private static SchemaHash of(byte[] schemaBytes, SchemaType schemaType) {
+        SchemaHash result = null;
+        if (schemaBytes == null || schemaBytes.length == 0) {
+            result = EmptySchemaHashFactory.get(schemaType);
+        }
+
+        // This should not be a common occurrence if everything goes well.
+        if (result == null) {
+            log.warn("Could not get schemaHash from EmptySchemaHashFactory, will create by hashFunction. Might bring"
+                            + " performance regression. schemaBytes length:{}, schemaType:{}",
+                    schemaBytes == null ? "null" : schemaBytes.length, schemaType);
+            result = new SchemaHash(
+                    hashFunction.hashBytes(schemaBytes == null ? new byte[0] : schemaBytes), schemaType);
+        }
+        return result;
     }
 
     public byte[] asBytes() {
         return hash.asBytes();
     }
+
+    private static class EmptySchemaHashFactory {
+        private static final HashCode EMPTY_HASH = hashFunction.hashBytes(new byte[0]);
+        private static final SchemaHash NONE_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, NONE);
+        private static final SchemaHash STRING_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, STRING);
+        private static final SchemaHash JSON_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, JSON);
+        private static final SchemaHash PROTOBUF_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, PROTOBUF);
+        private static final SchemaHash AVRO_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, AVRO);
+        private static final SchemaHash BOOLEAN_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, BOOLEAN);
+        private static final SchemaHash INT8_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, INT8);
+        private static final SchemaHash INT16_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, INT16);
+        private static final SchemaHash INT32_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, INT32);
+        private static final SchemaHash INT64_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, INT64);
+        private static final SchemaHash FLOAT_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, FLOAT);
+        private static final SchemaHash DOUBLE_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, DOUBLE);
+        private static final SchemaHash DATE_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, DATE);
+        private static final SchemaHash TIME_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, TIME);
+        private static final SchemaHash TIMESTAMP_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, TIMESTAMP);
+        private static final SchemaHash KEY_VALUE_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, KEY_VALUE);
+        private static final SchemaHash INSTANT_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, INSTANT);
+        private static final SchemaHash LOCAL_DATE_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, LOCAL_DATE);
+        private static final SchemaHash LOCAL_TIME_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, LOCAL_TIME);
+        private static final SchemaHash LOCAL_DATE_TIME_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, LOCAL_DATE_TIME);
+        private static final SchemaHash PROTOBUF_NATIVE_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, PROTOBUF_NATIVE);
+        private static final SchemaHash BYTES_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, BYTES);
+        private static final SchemaHash AUTO_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, AUTO);
+        private static final SchemaHash AUTO_CONSUME_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, AUTO_CONSUME);
+        private static final SchemaHash AUTO_PUBLISH_SCHEMA_HASH = new SchemaHash(EMPTY_HASH, AUTO_PUBLISH);
+
+        public static SchemaHash get(SchemaType schemaType) {

Review Comment:
   How about using a ConcurrentHashMap and use computeIfAbsent to create new SchemaHash?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org