fix(helper): handle negative byte counts in get_readable_size#6244
Open
JSap0914 wants to merge 1 commit into
Open
fix(helper): handle negative byte counts in get_readable_size#6244JSap0914 wants to merge 1 commit into
JSap0914 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Goals:
get_readable_sizeproduce human-readable units for negative byte counts instead of falling back to rawBytes.get_readable_sizeonly scales values intoKB/MB/GBwhennum_bytes >= 1024. Because the very first branch isif num_bytes < 1024, every negative value (which is always< 1024) is returned verbatim asBytes, e.g.get_readable_size(-2 * 1024**2)returns'-2097152 Bytes'instead of'-2.0 MB'.This actually surfaces in the profiler.
jina/logging/profile.pyreports a memory delta withget_readable_size(end_mem - start_mem), and that delta is negative whenever memory is freed, so freed-memory deltas are printed as a large raw-byte number rather than a readable size.Fix:
abs(num_bytes)and re-apply the original sign to the formatted string. Positive inputs are unchanged.Verification:
Result:
9 passed. Added a parametrized regression test covering0, positiveBytes/KB/MB/GB, and the negativeBytes/KB/MB/GBcases. Before the fix the four negative cases failed (e.g.assert '-2097152 Bytes' == '-2.0 MB'); after the fix they pass.