bbeaudreault commented on PR #5104:
URL: https://github.com/apache/hbase/pull/5104#issuecomment-1472088802

   @Apache9 thank you very much for the review. I had an idea this morning, and 
wonder if you have any opinion.
   
   Currently we do this:
   
   ```java
   protected void checkRefCount() {
     ObjectUtil.checkPositive(refCnt(), REFERENCE_COUNT_NAME);
   }
   ```
   
   Calling `refCnt()` goes down the expensive path of getting the real refCnt 
numeric value. 
   
   I think what we really care about is "has this buffer been recycled". In 
which case, what if we added a volatile boolean to our RefCnt class which gets 
set to true when the Recycler is called? We don't care about synchronization 
since it always goes from false to true. The above method could become:
   
   ```java
   //
   // in RefCnt.java
   //
   private volatile boolean recycled;
   
   public boolean isRecycled() {
       return recycled;
   }
   
   @Override
   protected final void deallocate() {
       this.recycler.free();
       this.recycled = true; // of note
       if (leak != null) {
         this.leak.close(this);
       }
   }
   
   //
   // In ByteBuff.java
   //
   protected void checkRefCount() {
       Preconditions.checkState(!refCnt.isRecycled(), "ByteBuff has been 
recycled");
   }
   ```
   
   Of course we'd also rename the method. I plugged this into our test case and 
it performs similarly to this PR. The benefit of this approach is it might also 
speed up off-heap usages while still providing protection.


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

Reply via email to