Add improved FormatBytesCount to print large sizes in more detailed way

Instead of printing `2 GB` output `2.43 GB` to be more explicit.
Deprecate version with `int` in favor of a single `ulong`.
This commit is contained in:
Wojtek Figat
2024-08-17 14:35:13 +02:00
parent 7650cead3d
commit 650a2921a3
7 changed files with 14 additions and 16 deletions

View File

@@ -388,7 +388,7 @@ namespace FlaxEditor.Content
{
sb.Append("Type: ").Append(TypeDescription).AppendLine();
if (File.Exists(Path))
sb.Append("Size: ").Append(Utilities.Utils.FormatBytesCount((int)new FileInfo(Path).Length)).AppendLine();
sb.Append("Size: ").Append(Utilities.Utils.FormatBytesCount((ulong)new FileInfo(Path).Length)).AppendLine();
sb.Append("Path: ").Append(Utilities.Utils.GetAssetNamePathWithExt(Path)).AppendLine();
}

View File

@@ -146,19 +146,14 @@ namespace FlaxEditor.Utilities
/// <summary>
/// Formats the amount of bytes to get a human-readable data size in bytes with abbreviation. Eg. 32 kB
/// [Deprecated in v1.9]
/// </summary>
/// <param name="bytes">The bytes.</param>
/// <returns>The formatted amount of bytes.</returns>
[Obsolete("Use FormatBytesCount with ulong instead")]
public static string FormatBytesCount(int bytes)
{
int order = 0;
while (bytes >= 1024 && order < MemorySizePostfixes.Length - 1)
{
order++;
bytes /= 1024;
}
return string.Format("{0:0.##} {1}", bytes, MemorySizePostfixes[order]);
return FormatBytesCount((ulong)bytes);
}
/// <summary>
@@ -169,12 +164,15 @@ namespace FlaxEditor.Utilities
public static string FormatBytesCount(ulong bytes)
{
int order = 0;
ulong bytesPrev = bytes;
while (bytes >= 1024 && order < MemorySizePostfixes.Length - 1)
{
bytesPrev = bytes;
order++;
bytes /= 1024;
}
if (order >= 3) // GB or higher use up to 2 decimal places for more precision
return string.Format("{0:0.##} {1}", FlaxEngine.Utils.RoundTo2DecimalPlaces(bytesPrev / 1024.0f), MemorySizePostfixes[order]);
return string.Format("{0:0.##} {1}", bytes, MemorySizePostfixes[order]);
}

View File

@@ -181,7 +181,7 @@ namespace FlaxEditor.Windows.Assets
group.Label("Frames: " + info.FramesCount);
group.Label("Channels: " + info.ChannelsCount);
group.Label("Keyframes: " + info.KeyframesCount);
group.Label("Memory Usage: " + Utilities.Utils.FormatBytesCount(info.MemoryUsage));
group.Label("Memory Usage: " + Utilities.Utils.FormatBytesCount((ulong)info.MemoryUsage));
}
base.Initialize(layout);

View File

@@ -52,7 +52,7 @@ namespace FlaxEditor.Windows.Profiler
AnchorPreset = AnchorPresets.HorizontalStretchTop,
Offsets = Margin.Zero,
Height = SingleChart.DefaultHeight,
FormatSample = v => Utilities.Utils.FormatBytesCount((int)v),
FormatSample = v => Utilities.Utils.FormatBytesCount((ulong)v),
Parent = mainPanel,
};
_memoryUsageChart.SelectedSampleChanged += OnSelectedSampleChanged;

View File

@@ -175,7 +175,7 @@ namespace FlaxEditor.Windows.Profiler
private string FormatCellBytes(object x)
{
return Utilities.Utils.FormatBytesCount((int)x);
return Utilities.Utils.FormatBytesCount((ulong)x);
}
/// <inheritdoc />

View File

@@ -36,14 +36,14 @@ namespace FlaxEditor.Windows.Profiler
_nativeAllocationsChart = new SingleChart
{
Title = "Native Memory Allocation",
FormatSample = v => Utilities.Utils.FormatBytesCount((int)v),
FormatSample = v => Utilities.Utils.FormatBytesCount((ulong)v),
Parent = layout,
};
_nativeAllocationsChart.SelectedSampleChanged += OnSelectedSampleChanged;
_managedAllocationsChart = new SingleChart
{
Title = "Managed Memory Allocation",
FormatSample = v => Utilities.Utils.FormatBytesCount((int)v),
FormatSample = v => Utilities.Utils.FormatBytesCount((ulong)v),
Parent = layout,
};
_managedAllocationsChart.SelectedSampleChanged += OnSelectedSampleChanged;

View File

@@ -319,7 +319,7 @@ namespace FlaxEditor.Windows.Profiler
private static string FormatCellBytes(object x)
{
return Utilities.Utils.FormatBytesCount((int)x);
return Utilities.Utils.FormatBytesCount((ulong)x);
}
private static int SortRows(Control x, Control y)