You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@bookkeeper.apache.org by GitBox <gi...@apache.org> on 2018/04/02 01:26:22 UTC

[GitHub] sijie closed pull request #1306: Minimize number of thread local instances in CRC32CDigestManager

sijie closed pull request #1306: Minimize number of thread local instances in CRC32CDigestManager
URL: https://github.com/apache/bookkeeper/pull/1306
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/checksum/CRC32CDigestManager.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/checksum/CRC32CDigestManager.java
index ca7fdec3f..138702895 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/checksum/CRC32CDigestManager.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/checksum/CRC32CDigestManager.java
@@ -20,24 +20,28 @@
 
 import com.scurrilous.circe.checksum.Crc32cIntChecksum;
 import com.scurrilous.circe.crc.Sse42Crc32C;
+
 import io.netty.buffer.ByteBuf;
-import org.apache.commons.lang3.mutable.MutableBoolean;
+import io.netty.util.concurrent.FastThreadLocal;
+
+import lombok.extern.slf4j.Slf4j;
+
 import org.apache.commons.lang3.mutable.MutableInt;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
+@Slf4j
 class CRC32CDigestManager extends DigestManager {
-    static final Logger LOG = LoggerFactory.getLogger(CRC32CDigestManager.class);
 
-    private final ThreadLocal<MutableInt> currentCrc = ThreadLocal
-            .withInitial(() -> new MutableInt(0));
-    private final ThreadLocal<MutableBoolean> isNewCrc = ThreadLocal
-            .withInitial(() -> new MutableBoolean(true));
+    private static final FastThreadLocal<MutableInt> currentCrc = new FastThreadLocal<MutableInt>() {
+        @Override
+        protected MutableInt initialValue() throws Exception {
+            return new MutableInt(0);
+        }
+    };
 
     public CRC32CDigestManager(long ledgerId) {
         super(ledgerId);
         if (!Sse42Crc32C.isSupported()) {
-            LOG.error("Sse42Crc32C is not supported, will use less slower CRC32C implementation.");
+            log.error("Sse42Crc32C is not supported, will use less slower CRC32C implementation.");
         }
     }
 
@@ -48,18 +52,15 @@ int getMacCodeLength() {
 
     @Override
     void populateValueAndReset(ByteBuf buf) {
-        buf.writeInt(currentCrc.get().intValue());
-        isNewCrc.get().setTrue();
+        MutableInt current = currentCrc.get();
+        buf.writeInt(current.intValue());
+        current.setValue(0);
     }
 
     @Override
     void update(ByteBuf data) {
-        if (isNewCrc.get().isTrue()) {
-            isNewCrc.get().setFalse();
-            currentCrc.get().setValue(Crc32cIntChecksum.computeChecksum(data));
-        } else {
-            final int lastCrc = currentCrc.get().intValue();
-            currentCrc.get().setValue(Crc32cIntChecksum.resumeChecksum(lastCrc, data));
-        }
+        MutableInt current = currentCrc.get();
+        final int lastCrc = current.intValue();
+        current.setValue(Crc32cIntChecksum.resumeChecksum(lastCrc, data));
     }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services