Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use memset_patternN in Buffer.fill #14599

Merged
merged 4 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions bench/snippets/buffer-fill.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { bench, run } from "./runner.mjs";

for (let size of [32, 2048, 1024 * 16, 1024 * 1024 * 2, 1024 * 1024 * 16]) {
for (let fillSize of [4, 8, 16, 11]) {
const buffer = Buffer.allocUnsafe(size);

const pattern = "x".repeat(fillSize);

bench(`Buffer.fill ${size} bytes with ${fillSize} byte value`, () => {
buffer.fill(pattern);
});
}
}

await run();
51 changes: 37 additions & 14 deletions src/bun.js/node/buffer.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const bun = @import("root").bun;
const JSC = bun.JSC;
const Encoder = JSC.WebCore.Encoder;
const Environment = bun.Environment;

pub const BufferVectorized = struct {
pub fn fill(
Expand Down Expand Up @@ -49,23 +50,45 @@ pub const BufferVectorized = struct {
} catch return false;

switch (written) {
0 => {},
1 => @memset(buf, buf[0]),
else => {
var contents = buf[0..written];
buf = buf[written..];
0 => return true,
1 => {
@memset(buf, buf[0]);
return true;
},
4 => if (comptime Environment.isMac) {
const pattern = buf[0..4];
buf = buf[pattern.len..];
bun.C.memset_pattern4(buf.ptr, pattern.ptr, buf.len);
return true;
},
8 => if (comptime Environment.isMac) {
const pattern = buf[0..8];
buf = buf[pattern.len..];
bun.C.memset_pattern8(buf.ptr, pattern.ptr, buf.len);
return true;
},
16 => if (comptime Environment.isMac) {
const pattern = buf[0..16];
buf = buf[pattern.len..];
bun.C.memset_pattern16(buf.ptr, pattern.ptr, buf.len);
return true;
},
else => {},
}

while (buf.len >= contents.len) {
bun.copy(u8, buf, contents);
buf = buf[contents.len..];
contents.len *= 2;
}
var contents = buf[0..written];
buf = buf[written..];

if (buf.len > 0) {
bun.copy(u8, buf, contents[0..buf.len]);
}
},
while (buf.len >= contents.len) {
@memcpy(buf[0..contents.len], contents);
dylan-conway marked this conversation as resolved.
Show resolved Hide resolved
buf = buf[contents.len..];
contents.len *= 2;
}

if (buf.len > 0) {
@memcpy(buf, contents[0..buf.len]);
}

return true;
}
};
Expand Down
4 changes: 4 additions & 0 deletions src/darwin_c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -888,3 +888,7 @@ pub const CLOCK_THREAD_CPUTIME_ID = 1;
pub const netdb = @cImport({
@cInclude("netdb.h");
});

pub extern fn memset_pattern4(buf: [*]u8, pattern: [*]const u8, len: usize) void;
pub extern fn memset_pattern8(buf: [*]u8, pattern: [*]const u8, len: usize) void;
pub extern fn memset_pattern16(buf: [*]u8, pattern: [*]const u8, len: usize) void;