zaaath commented on code in PR #2977:
URL: https://github.com/apache/cassandra/pull/2977#discussion_r1424378757


##########
src/java/org/apache/cassandra/utils/FBUtilities.java:
##########
@@ -939,6 +939,33 @@ public static String prettyPrintMemory(long size, String 
separator)
                                  UNIT_PREFIXES.charAt(UNIT_PREFIXES_BASE + 
prefixIndex));
     }
 
+    /**
+     * Convert the given size in bytes to a human-readable value using binary 
(i.e. 2^10-based) modifiers
+     * with only three significant digits.
+     * For example, "4.96 KiB", "48.8 KiB", "477 MiB", "0.422 KiB", "0.000 
KiB".
+     * @param size      Number to convert.
+     */
+    public static String prettyPrintMemoryShort(long size)

Review Comment:
   yeah, it is hard to follow. I tried my best to streamline the logic. Let me 
know if there is more I can do.
   
   This is actually similar to the existing `FBUtilities#prettyPrintMemory` 
method with some additional logic for keeping only the first three significant 
digits.
   
   "Line-by-line" explanation:
   1. Calculate Prefix Index: `int prefixIndex = (63 - 
Long.numberOfLeadingZeros(Math.abs(size))) / 10;` determines the appropriate 
prefix (like Kilo, Mega, Giga, etc.) for the given size. It calculates how many 
bits are needed to represent the absolute value of `size`, then divides this by 
10 to find the index in the `UNIT_PREFIXES` string.
   2. Adjust for Smallest Prefix: `if (prefixIndex == 0) prefixIndex = 1;` 
ensures that for sizes less than 1024 bytes (where `prefixIndex` would be 0), 
the 'Kilo' prefix is used instead.
   3. Get Prefix Character: `char prefix = 
UNIT_PREFIXES.charAt(UNIT_PREFIXES_BASE + prefixIndex);` retrieves the 
corresponding unit prefix character from UNIT_PREFIXES string based on the 
prefixIndex.
   4. Scale the Size: `float scaledSize = Math.scalb(size, -prefixIndex * 10);` 
scales the size down by the factor determined by the prefix index. This is 
effectively dividing the size by 2^(10×prefixIndex).
   5. Format Scaled Size: The block of code after `String scaledSizeStr;` 
formats the scaled size to have only three significant digits. It checks if the 
scaled size is between -1 and 1 (not inclusive) and simply calls 
`String.format` on it; otherwise, it calculates how many decimal places to 
include to ensure only three significant digits, after which calls 
`String.format` on it.
   6. Merge Scaled Size with Prefix. `return String.format` constructs the 
final string representing the size in a human-readable format, appending the 
appropriate unit prefix and the suffix 'iB' for binary unit.



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