mirror of
https://github.com/oven-sh/bun
synced 2026-02-15 21:32:05 +00:00
Add VLQ bench, improve decodeVLQ perf
This commit is contained in:
@@ -392,6 +392,13 @@ pub fn build(b: *std.build.Builder) !void {
|
||||
try configureObjectStep(b, headers_obj, target, obj.main_pkg_path.?);
|
||||
}
|
||||
|
||||
{
|
||||
const headers_step = b.step("vlq-bench", "Build vlq bench");
|
||||
var headers_obj: *std.build.LibExeObjStep = b.addExecutable("vlq-bench", "src/sourcemap/vlq_bench.zig");
|
||||
defer headers_step.dependOn(&headers_obj.step);
|
||||
try configureObjectStep(b, headers_obj, target, obj.main_pkg_path.?);
|
||||
}
|
||||
|
||||
{
|
||||
const headers_step = b.step("tgz-obj", "Build tgz (object files)");
|
||||
var headers_obj: *std.build.LibExeObjStep = b.addObject("tgz", "misctools/tgz.zig");
|
||||
|
||||
@@ -298,7 +298,7 @@ const base64_lut: [std.math.maxInt(u7)]u7 = brk: {
|
||||
break :brk bytes;
|
||||
};
|
||||
|
||||
fn decodeVLQ(encoded: []const u8, start: usize) VLQResult {
|
||||
pub fn decodeVLQ(encoded: []const u8, start: usize) VLQResult {
|
||||
var shift: u8 = 0;
|
||||
var vlq: u32 = 0;
|
||||
|
||||
@@ -306,8 +306,10 @@ fn decodeVLQ(encoded: []const u8, start: usize) VLQResult {
|
||||
// by doing it this way, we can hint to the compiler that it will not exceed 9
|
||||
const encoded_ = encoded[start..][0..@minimum(encoded.len - start, comptime (vlq_max_in_bytes + 1))];
|
||||
|
||||
for (encoded_) |c, i| {
|
||||
const index = @as(u32, base64_lut[@truncate(u7, c)]);
|
||||
comptime var i: usize = 0;
|
||||
|
||||
inline while (i < vlq_max_in_bytes + 1) : (i += 1) {
|
||||
const index = @as(u32, base64_lut[@truncate(u7, encoded_[i])]);
|
||||
|
||||
// decode a byte
|
||||
vlq |= (index & 31) << @truncate(u5, shift);
|
||||
@@ -563,6 +565,7 @@ pub fn appendMappingToBuffer(buffer_: MutableString, last_byte: u8, prev_state:
|
||||
if (needs_comma) {
|
||||
buffer.appendCharAssumeCapacity(',');
|
||||
}
|
||||
|
||||
comptime var i: usize = 0;
|
||||
inline while (i < vlq.len) : (i += 1) {
|
||||
buffer.appendAssumeCapacity(vlq[i].bytes[0..vlq[i].len]);
|
||||
|
||||
125
src/sourcemap/vlq_bench.zig
Normal file
125
src/sourcemap/vlq_bench.zig
Normal file
@@ -0,0 +1,125 @@
|
||||
const std = @import("std");
|
||||
|
||||
const SourceMap = @import("./sourcemap.zig");
|
||||
|
||||
pub fn main() anyerror!void {
|
||||
const args = try std.process.argsAlloc(std.heap.c_allocator);
|
||||
const how_many = try std.fmt.parseInt(u64, args[args.len - 1], 10);
|
||||
|
||||
var numbers = try std.heap.c_allocator.alloc(i32, how_many);
|
||||
var results = try std.heap.c_allocator.alloc(SourceMap.VLQ, how_many);
|
||||
const byte_size = std.mem.sliceAsBytes(numbers).len;
|
||||
|
||||
var rand = std.rand.DefaultPrng.init(0);
|
||||
|
||||
std.debug.print("Random values:\n\n", .{});
|
||||
|
||||
for (numbers) |_, i| {
|
||||
numbers[i] = rand.random().int(i32);
|
||||
}
|
||||
|
||||
{
|
||||
var timer = try std.time.Timer.start();
|
||||
|
||||
for (numbers) |n, i| {
|
||||
results[i] = SourceMap.encodeVLQ(n);
|
||||
}
|
||||
const elapsed = timer.read();
|
||||
std.debug.print("[{d}] encode: {} in {}\n", .{ how_many, std.fmt.fmtIntSizeDec(byte_size), std.fmt.fmtDuration(elapsed) });
|
||||
}
|
||||
|
||||
{
|
||||
var timer = try std.time.Timer.start();
|
||||
|
||||
for (numbers) |n, i| {
|
||||
results[i] = SourceMap.encodeVLQWithLookupTable(n);
|
||||
}
|
||||
const elapsed = timer.read();
|
||||
std.debug.print("[{d}] encodeWithLookupTable: {} in {}\n", .{ how_many, std.fmt.fmtIntSizeDec(byte_size), std.fmt.fmtDuration(elapsed) });
|
||||
}
|
||||
|
||||
{
|
||||
var timer = try std.time.Timer.start();
|
||||
|
||||
for (results) |n, i| {
|
||||
numbers[i] = SourceMap.decodeVLQ(n.bytes[0..n.len], 0).value;
|
||||
}
|
||||
|
||||
const elapsed = timer.read();
|
||||
std.debug.print("[{d}] decode: {} in {}\n", .{ how_many, std.fmt.fmtIntSizeDec(byte_size), std.fmt.fmtDuration(elapsed) });
|
||||
}
|
||||
|
||||
std.debug.print("\nNumbers between 0 - 8096 (most columns won't exceed 255):\n\n", .{});
|
||||
|
||||
for (numbers) |_, i| {
|
||||
numbers[i] = rand.random().intRangeAtMost(i32, 0, 8096);
|
||||
}
|
||||
|
||||
{
|
||||
var timer = try std.time.Timer.start();
|
||||
|
||||
for (numbers) |n, i| {
|
||||
results[i] = SourceMap.encodeVLQ(n);
|
||||
}
|
||||
const elapsed = timer.read();
|
||||
std.debug.print("[{d}] encode: {} in {}\n", .{ how_many, std.fmt.fmtIntSizeDec(byte_size), std.fmt.fmtDuration(elapsed) });
|
||||
}
|
||||
|
||||
{
|
||||
var timer = try std.time.Timer.start();
|
||||
|
||||
for (numbers) |n, i| {
|
||||
results[i] = SourceMap.encodeVLQWithLookupTable(n);
|
||||
}
|
||||
const elapsed = timer.read();
|
||||
std.debug.print("[{d}] encodeWithLookupTable: {} in {}\n", .{ how_many, std.fmt.fmtIntSizeDec(byte_size), std.fmt.fmtDuration(elapsed) });
|
||||
}
|
||||
|
||||
{
|
||||
var timer = try std.time.Timer.start();
|
||||
|
||||
for (results) |n, i| {
|
||||
numbers[i] = SourceMap.decodeVLQ(n.bytes[0..n.len], 0).value;
|
||||
}
|
||||
|
||||
const elapsed = timer.read();
|
||||
std.debug.print("[{d}] decode: {} in {}\n", .{ how_many, std.fmt.fmtIntSizeDec(byte_size), std.fmt.fmtDuration(elapsed) });
|
||||
}
|
||||
|
||||
std.debug.print("\nNumbers between 0 - 255 (most columns won't exceed 255):\n\n", .{});
|
||||
|
||||
for (numbers) |_, i| {
|
||||
numbers[i] = rand.random().intRangeAtMost(i32, 0, 255);
|
||||
}
|
||||
|
||||
{
|
||||
var timer = try std.time.Timer.start();
|
||||
|
||||
for (numbers) |n, i| {
|
||||
results[i] = SourceMap.encodeVLQ(n);
|
||||
}
|
||||
const elapsed = timer.read();
|
||||
std.debug.print("[{d}] encode: {} in {}\n", .{ how_many, std.fmt.fmtIntSizeDec(byte_size), std.fmt.fmtDuration(elapsed) });
|
||||
}
|
||||
|
||||
{
|
||||
var timer = try std.time.Timer.start();
|
||||
|
||||
for (numbers) |n, i| {
|
||||
results[i] = SourceMap.encodeVLQWithLookupTable(n);
|
||||
}
|
||||
const elapsed = timer.read();
|
||||
std.debug.print("[{d}] encodeWithLookupTable: {} in {}\n", .{ how_many, std.fmt.fmtIntSizeDec(byte_size), std.fmt.fmtDuration(elapsed) });
|
||||
}
|
||||
|
||||
{
|
||||
var timer = try std.time.Timer.start();
|
||||
|
||||
for (results) |n, i| {
|
||||
numbers[i] = SourceMap.decodeVLQ(n.bytes[0..n.len], 0).value;
|
||||
}
|
||||
|
||||
const elapsed = timer.read();
|
||||
std.debug.print("[{d}] decode: {} in {}\n", .{ how_many, std.fmt.fmtIntSizeDec(byte_size), std.fmt.fmtDuration(elapsed) });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user