pushrsp commented on issue #1457:
URL: 
https://github.com/apache/incubator-fury/issues/1457#issuecomment-2033992943

   > Regarding the second suggestion, how to simplify it? Extract a function 
separately for the checked logic and call it in readXXX?
   
   i would simplify like this below.
   ```java
   public byte readByte() {
       int readerIdx = readerIndex;
       throwIfOutOfBound(readerIdx, 1);
       readerIndex = readerIdx + 1;
       return UNSAFE.getByte(heapMemory, address + readerIdx);
     }
   
   public short readShort() {
       int readerIdx = readerIndex;
       throwIfOutOfBound(readerIdx, 2)
       readerIndex = readerIdx + 2;
       final long pos = address + readerIdx;
       if (LITTLE_ENDIAN) {
         return UNSAFE.getShort(heapMemory, pos);
       } else {
         return Short.reverseBytes(UNSAFE.getShort(heapMemory, pos));
       }
     }
   
   ...
   
   private void throwIfOutOfBound(int readerIdx, int dataSize) {
       if(BoundsChecking.BOUNDS_CHECKING_ENABLED && readerIdx > size - 
dataSize) {
         throw new IndexOutOfBoundsException(
                 String.format(
                         "readerIndex(%d) + length(%d) exceeds size(%d): %s", 
readerIdx, dataSize, size, this));
       }
     }
   ```


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