yagagagaga commented on PR #880:
URL: 
https://github.com/apache/incubator-baremaps/pull/880#issuecomment-2173976507

   As all we known, Long is a 64-bits length data type with 63-bits represents 
the numerical value and 1-bit represents positive or negative.
   The `key` in `MemoryAlignedDataMap` needs to consider the following 
limitations:
   1. `value`'s data size. If the `value` occupies 4 bytes, it means that 
2-bits (in 63-bits) are needed to represent.
   2. The calculation of `segmentIndex` depends on the size of `segmentSize`. 
If the `segmentSize` is 1024, it means that 10-bits (in 61-bits) are needed to 
represent and the remaining 51-bits represent the number of segments. 
Considering that `segmentIndex` is an Integer field, it will cause precision 
loss when the `segmentIndex` is greater than 32-bits. There are two cases to 
discuss:
       1. `segmentIndex` will not loss precision if `segmentSize` greater than 
32-bits.
       ```java
        value data size = 2-bits                   segmentSize > 32-bits        
  
            /\                            /~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~\
           0111111111111111111111111111111111111111111111111111111111111111
           ^  \__________________________/
           |               v
           |       segmentIndex <= 30-bits
           |  \___________________________________________________________/
           |                            v
          -/+                       key's range
       ```
       2. `segmentIndex` will loss precision if `segmentSize` less than 
32-bits. The range of `segmentIndex` must be limited in order to be represented 
as an integer.
       ```java
                                            segmentIndex        segmentSize     
     
                                  /~~~~~~~~~~~~~~~^~~~~~~~~~~~~~\/~~~~^~~~\
           0111111111111111111111111111111111111111111111111111111111111111
           ^                      \/
           |             alue data size = 2-bits  
           |                        \_____________________________________/
           |                                         v
          -/+                                   key's range
       ```
   
   If following your implement, some wrong will happened:
   ```java
   // this.upperBoundary = ((long) Integer.MAX_VALUE) / (long) dataType.size() 
* ((long) memory.segmentSize());
   var map = new MemoryAlignedDataMap<>(new IntegerDataType(), new 
OnHeapMemory(1024));
   map.put(549755813887L, 1); // error will happen, but as the matter of fact, 
you can calculate segmentIndex and segmentOffset with this number
   
   var map = new MemoryAlignedDataMap<>(new IntegerDataType(), new 
OnHeapMemory(1L << 34)); // if could be, the upperBoundary will numeric overflow
   ```
   


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