jyothsnakonisa commented on code in PR #4530:
URL: https://github.com/apache/cassandra/pull/4530#discussion_r2624675943


##########
src/java/org/apache/cassandra/serializers/UTF8Serializer.java:
##########
@@ -57,8 +60,51 @@ static <V> boolean validate(V value, ValueAccessor<V> 
accessor)
             if (value == null)
                 return false;
 
-            int b = 0;
-            int offset = 0;
+            // perf optimizations:
+            // 1) avoid bymorhic/megamorphic calls via ValueAccessor
+            // 2) use a simplified logic to handle ASCII prefixed String 
scenario faster
+            if (accessor == ByteArrayAccessor.instance)
+            {
+                byte[] valueAsArray = accessor.toArray(value);
+                for (int i = 0; i < valueAsArray.length; i++)
+                {
+                    if (valueAsArray[i] < 0)
+                        return validateSlowPath(value, accessor, i);
+                }
+                return true;

Review Comment:
   I see this block of code is repeated across this method, can you please 
extract it to a helper to reduce code duplication and for better readability?



##########
src/java/org/apache/cassandra/serializers/UTF8Serializer.java:
##########
@@ -57,8 +60,51 @@ static <V> boolean validate(V value, ValueAccessor<V> 
accessor)
             if (value == null)
                 return false;
 
-            int b = 0;
-            int offset = 0;
+            // perf optimizations:
+            // 1) avoid bymorhic/megamorphic calls via ValueAccessor
+            // 2) use a simplified logic to handle ASCII prefixed String 
scenario faster
+            if (accessor == ByteArrayAccessor.instance)
+            {
+                byte[] valueAsArray = accessor.toArray(value);
+                for (int i = 0; i < valueAsArray.length; i++)
+                {
+                    if (valueAsArray[i] < 0)
+                        return validateSlowPath(value, accessor, i);
+                }
+                return true;
+            }
+
+            if (accessor == ByteBufferAccessor.instance)
+            {
+                ByteBuffer valueAsBuffer = accessor.toBuffer(value);
+                if (valueAsBuffer.hasArray())
+                {
+                    byte[] valueAsArray = valueAsBuffer.array();
+                    int start = valueAsBuffer.position();
+                    int end = start + valueAsBuffer.remaining();
+                    for (int i = start; i < end; i++)
+                    {
+                        if (valueAsArray[i] < 0)
+                            return validateSlowPath(value, accessor, i - 
start);
+                    }
+                }
+                return true;

Review Comment:
   should we always return true? there will never be no validation when 
`valueAsBuffer.hasArray()` is false. Should you move the return statement into 
if block above instead?



##########
test/unit/org/apache/cassandra/serializers/UTF8ValidatorTest.java:
##########
@@ -0,0 +1,145 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.serializers;
+
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+
+import org.junit.Test;
+
+import org.apache.cassandra.db.marshal.ByteArrayAccessor;
+import org.apache.cassandra.db.marshal.ByteBufferAccessor;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+// https://www.w3.org/2001/06/utf-8-test/UTF-8-demo.html
+public class UTF8ValidatorTest
+{

Review Comment:
   Can you please add tests for ByteBuffer using `ByteBuffer.allocateDirect`. 
It would be valuable to look at the if conditions in validate method and add 
tests that cover all branches.



##########
src/java/org/apache/cassandra/serializers/UTF8Serializer.java:
##########
@@ -57,8 +60,51 @@ static <V> boolean validate(V value, ValueAccessor<V> 
accessor)
             if (value == null)
                 return false;
 
-            int b = 0;
-            int offset = 0;
+            // perf optimizations:
+            // 1) avoid bymorhic/megamorphic calls via ValueAccessor

Review Comment:
   ```suggestion
               // 1) avoid bimorphic/megamorphic calls via ValueAccessor
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to