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 12:34:21 UTC

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

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


##########
pulsar-common/src/main/java/org/apache/pulsar/common/protocol/schema/SchemaHash.java:
##########
@@ -55,15 +82,123 @@ 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 empty() {
+        return of(null, null);
+    }
+
+    private static SchemaHash of(byte[] schemaBytes, SchemaType schemaType) {
+        SchemaHash result;
+        if (schemaBytes == null || schemaBytes.length == 0) {
+            result = EmptySchemaHashFactory.get(schemaType);
+            if (result == null) {

Review Comment:
   Why should that method possibly fail? Couldn’t we assume that it always returns a result. This would simplify the code here. 



##########
pulsar-common/src/main/java/org/apache/pulsar/common/protocol/schema/SchemaHash.java:
##########
@@ -55,15 +82,123 @@ 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 empty() {
+        return of(null, null);
+    }
+
+    private static SchemaHash of(byte[] schemaBytes, SchemaType schemaType) {
+        SchemaHash result;
+        if (schemaBytes == null || schemaBytes.length == 0) {
+            result = EmptySchemaHashFactory.get(schemaType);
+            if (result == null) {
+                log.warn("Could not get schemaHash from EmptySchemaHashFactory, will create by hashFunction. Might"
+                                + " bring performance regression. schemaBytes:{}, schemaType:{}",
+                        schemaBytes == null ? "null" : "0", schemaType);
+                result = new SchemaHash(
+                        hashFunction.hashBytes(schemaBytes == null ? new byte[0] : schemaBytes), schemaType);
+            }
+        } else {
+            result = new SchemaHash(hashFunction.hashBytes(schemaBytes), schemaType);
+        }
+        return result;
     }
 
     public byte[] asBytes() {
         return hash.asBytes();
     }
+
+    private static class EmptySchemaHashFactory {

Review Comment:
   It may require some refactoring though why not having SchemaInfo.hash() method? The schemas are already all statically initialized 



-- 
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