Slightly improve when printing lots of numbers

This commit is contained in:
Jarred Sumner
2022-11-26 05:22:12 -08:00
parent 37753e9787
commit 8aec181f16
2 changed files with 55 additions and 1 deletions

View File

@@ -1763,10 +1763,25 @@ pub const ZigConsoleClient = struct {
}
},
.Integer => {
writer.print(comptime Output.prettyFmt("<r><yellow>{d}<r>", enable_ansi_colors), .{value.toInt64()});
const int = value.toInt64();
if (int < std.math.maxInt(u32)) {
var i = int;
const is_negative = i < 0;
if (is_negative) {
i = -i;
}
const digits = bun.fmt.fastDigitCount(@intCast(u32, i)) + @as(u32, @boolToInt(is_negative));
this.addForNewLine(digits);
} else {
this.addForNewLine(bun.fmt.count("{d}", .{int}));
}
writer.print(comptime Output.prettyFmt("<r><yellow>{d}<r>", enable_ansi_colors), .{int});
},
.BigInt => {
var out_str = value.getZigString(this.globalThis).slice();
this.addForNewLine(out_str.len);
writer.print(comptime Output.prettyFmt("<r><yellow>{s}n<r>", enable_ansi_colors), .{out_str});
},
.Double => {

View File

@@ -31,6 +31,45 @@ pub const path = @import("./resolver/resolve_path.zig");
pub const fmt = struct {
pub usingnamespace std.fmt;
// https://lemire.me/blog/2021/06/03/computing-the-number-of-digits-of-an-integer-even-faster/
pub fn fastDigitCount(x: u64) u64 {
const table = [_]u64{
4294967296,
8589934582,
8589934582,
8589934582,
12884901788,
12884901788,
12884901788,
17179868184,
17179868184,
17179868184,
21474826480,
21474826480,
21474826480,
21474826480,
25769703776,
25769703776,
25769703776,
30063771072,
30063771072,
30063771072,
34349738368,
34349738368,
34349738368,
34349738368,
38554705664,
38554705664,
38554705664,
41949672960,
41949672960,
41949672960,
42949672960,
42949672960,
};
return x + table[std.math.log2(x)] >> 32;
}
pub const SizeFormatter = struct {
value: usize = 0,
pub fn format(self: SizeFormatter, comptime _: []const u8, opts: fmt.FormatOptions, writer: anytype) !void {