bereng commented on code in PR #2464:
URL: https://github.com/apache/cassandra/pull/2464#discussion_r1271753873


##########
test/microbench/org/apache/cassandra/test/microbench/DeletionTimeDeSerBench.java:
##########
@@ -0,0 +1,366 @@
+/*
+ * 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.test.microbench;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.file.NoSuchFileException;
+import java.util.ArrayList;
+import java.util.Random;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.cassandra.db.DeletionTime;
+import org.apache.cassandra.db.DeletionTime.Serializer;
+import org.apache.cassandra.io.util.DataInputBuffer;
+import org.apache.cassandra.io.util.DataInputPlus;
+import org.apache.cassandra.io.util.DataOutputBuffer;
+import org.apache.cassandra.io.util.DataOutputStreamPlus;
+import org.apache.cassandra.io.util.FileInputStreamPlus;
+import org.apache.cassandra.io.util.FileOutputStreamPlus;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.TearDown;
+import org.openjdk.jmh.annotations.Threads;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.infra.Blackhole;
+
+@BenchmarkMode(Mode.AverageTime)
+@OutputTimeUnit(TimeUnit.NANOSECONDS)
+@Warmup(iterations = 2, time = 1, timeUnit = TimeUnit.SECONDS)
+@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
+@Fork(value = 3, jvmArgsAppend = "-Xmx512M")
+@Threads(1)
+@State(Scope.Benchmark)
+public class DeletionTimeDeSerBench
+{
+    final static int BUFFER_SIZE = 1024 * 1024;
+    final static int DT_STD_SIZE = 8 + 4; // Long + Int
+
+    final static Random random = new Random(100);
+    final static ArrayList<DeletionTime> TEST_DTS_70PC_LIVE = generateDTs(70);
+    final static ArrayList<DeletionTime> TEST_DTS_30PC_LIVE = generateDTs(30);
+
+    // Parameters
+    final static String ncSstableParam = "NC";
+    final static String oaSstableParam = "OA";
+    @Param({ ncSstableParam, oaSstableParam })
+    String sstableParam;
+
+    final static String live70PcParam = "70PcLive";
+    final static String live30PcParam = "30PcLive";
+    @Param({ live70PcParam, live30PcParam })
+    String liveDTPcParam;
+
+    final static String RAMParam = "RAM";
+    final static String diskParam = "Disk";
+    @Param({ RAMParam,  diskParam })
+    String diskRAMParam;
+
+    // Files
+    File serMMapedFile = new File(DeletionTimeDeSerBench.class + "_Sermmap");
+    ByteBuffer serMMap = allocateMmapedByteBuffer(serMMapedFile, BUFFER_SIZE);
+    File deserMMapedFile = new File(DeletionTimeDeSerBench.class + 
"_Desermmap");
+    ByteBuffer deserMMap = allocateMmapedByteBuffer(deserMMapedFile, 
BUFFER_SIZE);
+    File diskFile = new File(DeletionTimeDeSerBench.class + "_DTBenchTest");
+
+    @TearDown
+    public void tearDown()
+    {
+        serMMapedFile.delete();
+        deserMMapedFile.delete();
+        diskFile.delete();
+    }
+
+    @Benchmark
+    public void testE2ESerializeDT(final Blackhole bh) throws IOException
+    {
+        ByteBuffer buffer = serMMap;
+        Serializer serializer = getSerializer(sstableParam);
+        ArrayList<DeletionTime> dts = getDTs(liveDTPcParam);
+        
+        try(DataOutputStreamPlus out = getSerOut(diskRAMParam, serMMap))
+        {
+            for (int i = 0, m = dts.size(); i < m; i++)
+                serializer.serialize(dts.get(i), out);
+            bh.consume(out);
+        }
+
+        bh.consume(buffer);
+        bh.consume(serializer);
+        bh.consume(dts);
+        buffer.clear();
+    }
+
+    @Benchmark
+    public void testE2EDeSerializeDT(final Blackhole bh) throws IOException
+    {
+        ByteBuffer buffer = deserMMap;
+        Serializer serializer = getSerializer(sstableParam);
+        ArrayList<DeletionTime> dts = getDTs(liveDTPcParam);
+        
+        try(DataOutputStreamPlus out = getSerOut(diskRAMParam, deserMMap))
+        {
+            for (int i = 0, m = dts.size(); i < m; i++)
+                serializer.serialize(dts.get(i), out);
+            bh.consume(out);
+        }
+
+        DataInputPlus in = getDeSerIn(diskRAMParam, deserMMap);
+        for (int i = 0, m = dts.size(); i < m; i++)
+            serializer.deserialize(in);
+
+        bh.consume(buffer);
+        bh.consume(serializer);
+        bh.consume(dts);
+        bh.consume(in);
+        buffer.clear();
+    }
+
+    /**
+     * Test the impact of the different bit ops and write calls. Against 
memory only.
+     */
+    @Benchmark
+    public void testRawAlgWrites(final Blackhole bh)
+    {
+        if (diskRAMParam != RAMParam)
+            return;
+
+        ArrayList<DeletionTime> dts = getDTs(liveDTPcParam);
+        ByteBuffer buffer = ByteBuffer.allocate(DT_STD_SIZE);
+
+        if (sstableParam == oaSstableParam)
+        {
+            for (int i = 0, m = dts.size(); i < m; i++)
+            {
+                buffer.clear();
+                if (dts.get(i).equals(DeletionTime.LIVE))
+                    buffer.put((byte) 0b1000_0000);
+                else
+                {
+                    buffer.putLong(Math.abs(random.nextLong()));

Review Comment:
   Done in latest commit



##########
src/java/org/apache/cassandra/db/DeletionTime.java:
##########
@@ -195,42 +195,95 @@ public static Serializer getSerializer(Version version)
             return legacySerializer;
     }
 
-    // Serializer for Usigned Integer ldt
+    /* Serializer for Usigned Integer ldt
+     *
+     * ldt is encoded as a uint in seconds since unix epoch, it can go up o 
2106-02-07T06:28:13+00:00 only. 
+     * mfda is a positive timestamp. We use the sign bit to enconde LIVE 
DeletionTimes
+     */
     public static class Serializer implements ISerializer<DeletionTime>
     {
+        // We use the sign bit to signal LIVE DeletionTimes
+        private final static int IS_LIVE_DELETION = 0b1000_0000;
+
         public void serialize(DeletionTime delTime, DataOutputPlus out) throws 
IOException
         {
-            out.writeInt(delTime.localDeletionTimeUnsignedInteger);
-            out.writeLong(delTime.markedForDeleteAt());
+            if (delTime == LIVE)
+                out.writeByte(IS_LIVE_DELETION);
+            else
+            {
+                // The sign bit is zero here, so we can write a long directly
+                out.writeLong(delTime.markedForDeleteAt());
+                out.writeInt(delTime.localDeletionTimeUnsignedInteger);
+            }
         }
 
         public DeletionTime deserialize(DataInputPlus in) throws IOException
         {
-            int localDeletionTimeUnsignedInteger = in.readInt();
-            long mfda = in.readLong();
-            return mfda == Long.MIN_VALUE && localDeletionTimeUnsignedInteger 
== Cell.NO_DELETION_TIME_UNSIGNED_INTEGER
-                 ? LIVE
-                 : new DeletionTime(mfda, localDeletionTimeUnsignedInteger);
+            int flags = in.readByte();
+            if ((flags & IS_LIVE_DELETION) != 0)
+                return LIVE;
+            else
+            {
+                // Read the remaining 7 bytes
+                int bytes4 = in.readInt();

Review Comment:
   Done in latest commit



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