diff options
author | 2022-11-08 12:55:45 -0800 | |
---|---|---|
committer | 2022-11-08 12:55:45 -0800 | |
commit | 1604666988b5aa674104d10fbc5d2e86cc04e870 (patch) | |
tree | 716ee440962803a7ac00e2b40fca490f3ce47a08 /src/string_immutable.zig | |
parent | 6c6e680d913d440d22d23a599de58b51359d6f1c (diff) | |
download | bun-1604666988b5aa674104d10fbc5d2e86cc04e870.tar.gz bun-1604666988b5aa674104d10fbc5d2e86cc04e870.tar.zst bun-1604666988b5aa674104d10fbc5d2e86cc04e870.zip |
20% faster TextDecoder on small inputs
Diffstat (limited to 'src/string_immutable.zig')
-rw-r--r-- | src/string_immutable.zig | 37 |
1 files changed, 16 insertions, 21 deletions
diff --git a/src/string_immutable.zig b/src/string_immutable.zig index 94e0b7730..9a0165020 100644 --- a/src/string_immutable.zig +++ b/src/string_immutable.zig @@ -942,16 +942,13 @@ pub fn toUTF16Alloc(allocator: std.mem.Allocator, bytes: []const u8, comptime fa var remaining = chunk; { - var sequence: [4]u8 = undefined; - - if (remaining.len >= 4) { - sequence = remaining[0..4].*; - } else { - sequence[0] = remaining[0]; - sequence[1] = if (remaining.len > 1) remaining[1] else 0; - sequence[2] = if (remaining.len > 2) remaining[2] else 0; - sequence[3] = 0; - } + const sequence: [4]u8 = switch (remaining.len) { + 0 => unreachable, + 1 => [_]u8{ remaining[0], 0, 0, 0 }, + 2 => [_]u8{ remaining[0], remaining[1], 0, 0 }, + 3 => [_]u8{ remaining[0], remaining[1], remaining[2], 0 }, + else => remaining[0..4].*, + }; const replacement = strings.convertUTF8BytesIntoUTF16(&sequence); if (comptime fail_if_invalid) { @@ -980,16 +977,13 @@ pub fn toUTF16Alloc(allocator: std.mem.Allocator, bytes: []const u8, comptime fa const last = remaining[0..j]; remaining = remaining[j..]; - var sequence: [4]u8 = undefined; - - if (remaining.len >= 4) { - sequence = remaining[0..4].*; - } else { - sequence[0] = remaining[0]; - sequence[1] = if (remaining.len > 1) remaining[1] else 0; - sequence[2] = if (remaining.len > 2) remaining[2] else 0; - sequence[3] = 0; - } + const sequence: [4]u8 = switch (remaining.len) { + 0 => unreachable, + 1 => [_]u8{ remaining[0], 0, 0, 0 }, + 2 => [_]u8{ remaining[0], remaining[1], 0, 0 }, + 3 => [_]u8{ remaining[0], remaining[1], remaining[2], 0 }, + else => remaining[0..4].*, + }; const replacement = strings.convertUTF8BytesIntoUTF16(&sequence); if (comptime fail_if_invalid) { @@ -1016,7 +1010,8 @@ pub fn toUTF16Alloc(allocator: std.mem.Allocator, bytes: []const u8, comptime fa } if (remaining.len > 0) { - try output.ensureUnusedCapacity(remaining.len); + try output.ensureTotalCapacityPrecise(output.items.len + remaining.len); + output.items.len += remaining.len; strings.copyU8IntoU16(output.items[output.items.len - remaining.len ..], remaining); } |