authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2025-01-20 17:27:50+11:00
committergravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2025-01-20 18:29:15+11:00
logd6e1166f1a93e8ed5dd0a74cc0b0daeae5c050a0
tree445a9c5806cb3a2288abc77624bcd07e82a9492b
parentbf89482f23aae3524c92d385d6d8241d065d1031

compiler-rt: reduce memmove and memcpy size for ReleaseSmall


2 files changed, 49 insertions(+), 4 deletions(-)

lib/compiler_rt/memcpy.zig+21-2
...@@ -4,10 +4,29 @@ const builtin = @import("builtin");...@@ -4,10 +4,29 @@ const builtin = @import("builtin");
44
5comptime {5comptime {
6 if (builtin.object_format != .c) {6 if (builtin.object_format != .c) {
7 @export(&memcpy, .{ .name = "memcpy", .linkage = common.linkage, .visibility = common.visibility });7 const export_options: std.builtin.ExportOptions = .{
8 .name = "memcpy",
9 .linkage = common.linkage,
10 .visibility = common.visibility,
11 };
12
13 if (builtin.mode == .ReleaseSmall)
14 @export(&memcpySmall, export_options)
15 else
16 @export(&memcpyFast, export_options);
17 }
18}
19
20fn memcpySmall(noalias dest: ?[*]u8, noalias src: ?[*]const u8, len: usize) callconv(.C) ?[*]u8 {
21 @setRuntimeSafety(builtin.is_test);
22
23 for (0..len) |i| {
24 dest.?[i] = src.?[i];
8 }25 }
26
27 return dest;
9}28}
1029
11fn memcpy(opt_dest: ?[*]u8, opt_src: ?[*]const u8, len: usize) callconv(.C) ?[*]u8 {30fn memcpyFast(opt_dest: ?[*]u8, opt_src: ?[*]const u8, len: usize) callconv(.C) ?[*]u8 {
12 return @call(.always_inline, @import("memmove.zig").memmove, .{ opt_dest, opt_src, len });31 return @call(.always_inline, @import("memmove.zig").memmove, .{ opt_dest, opt_src, len });
13}32}
lib/compiler_rt/memmove.zig+28-2
...@@ -4,11 +4,37 @@ const builtin = @import("builtin");...@@ -4,11 +4,37 @@ const builtin = @import("builtin");
44
5comptime {5comptime {
6 if (builtin.object_format != .c) {6 if (builtin.object_format != .c) {
7 @export(&memmove, .{ .name = "memmove", .linkage = common.linkage, .visibility = common.visibility });7 const export_options: std.builtin.ExportOptions = .{
8 .name = "memmove",
9 .linkage = common.linkage,
10 .visibility = common.visibility,
11 };
12
13 if (builtin.mode == .ReleaseSmall)
14 @export(&memmoveSmall, export_options)
15 else
16 @export(&memmoveFast, export_options);
8 }17 }
9}18}
1019
11pub fn memmove(opt_dest: ?[*]u8, opt_src: ?[*]const u8, len: usize) callconv(.C) ?[*]u8 {20fn memmoveSmall(opt_dest: ?[*]u8, opt_src: ?[*]const u8, len: usize) callconv(.C) ?[*]u8 {
21 const dest = opt_dest.?;
22 const src = opt_src.?;
23
24 if (@intFromPtr(dest) < @intFromPtr(src)) {
25 for (0..len) |i| {
26 dest[i] = src[i];
27 }
28 } else {
29 for (0..len) |i| {
30 dest[len - 1 - i] = src[len - 1 - i];
31 }
32 }
33
34 return dest;
35}
36
37pub fn memmoveFast(opt_dest: ?[*]u8, opt_src: ?[*]const u8, len: usize) callconv(.C) ?[*]u8 {
12 // a port of https://github.com/facebook/folly/blob/1c8bc50e88804e2a7361a57cd9b551dd10f6c5fd/folly/memcpy.S38 // a port of https://github.com/facebook/folly/blob/1c8bc50e88804e2a7361a57cd9b551dd10f6c5fd/folly/memcpy.S
13 if (len == 0) {39 if (len == 0) {
14 @branchHint(.unlikely);40 @branchHint(.unlikely);