Skip to content

Commit 6109e6b

Browse files
committed
compatstd: minor optimization
1 parent 6f1c867 commit 6109e6b

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/compatstd.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ const nativeEndian = @import("builtin").cpu.arch.endian();
99
const isZig0d14AndLater = builtin.zig_version.order(.{ .major = 0, .minor = 13, .patch = 0 }).compare(.gt);
1010

1111
pub const mem = struct {
12-
pub fn writeIntBig(T: type, dst: *[@divExact(meta.bitsOfType(T), 8)]u8, value: T) void {
12+
pub inline fn writeIntBig(T: type, dst: *[@divExact(meta.bitsOfType(T), 8)]u8, value: T) void {
1313
return std.mem.writeInt(T, dst, value, .big);
1414
}
1515

16-
pub fn readIntBig(T: type, src: *const [@divExact(meta.bitsOfType(T), 8)]u8) T {
16+
pub inline fn readIntBig(T: type, src: *const [@divExact(meta.bitsOfType(T), 8)]u8) T {
1717
return std.mem.readInt(T, src, .big);
1818
}
1919

20-
pub fn readFloatBig(comptime T: type, src: *const [@divExact(meta.bitsOfType(T), 8)]u8) T {
20+
pub inline fn readFloatBig(comptime T: type, src: *const [@divExact(meta.bitsOfType(T), 8)]u8) T {
2121
if (meta.bitsOfType(T) > 64) {
2222
@compileError("readFloatBig does not support float types have more than 64 bits.");
2323
}
@@ -35,11 +35,11 @@ pub const mem = struct {
3535
return bytesAsValue(T, src).*;
3636
}
3737

38-
pub fn writeFloatBig(comptime T: type, dest: *[@divExact(meta.bitsOfType(T), 8)]u8, val: T) void {
38+
pub inline fn writeFloatBig(comptime T: type, noalias dest: *[@divExact(meta.bitsOfType(T), 8)]u8, noalias val: T) void {
3939
if (meta.bitsOfType(T) > 64) {
4040
@compileError("writeFloatBig does not support float types have more than 64 bits.");
4141
}
42-
std.mem.copyForwards(u8, dest, asBytes(&val));
42+
@memcpy(dest, asBytes(&val));
4343
if (nativeEndian == .little) {
4444
for (0..@divExact(dest.len, 2)) |i| {
4545
std.mem.swap(u8, &dest[i], &dest[dest.len - i - 1]);
@@ -49,7 +49,7 @@ pub const mem = struct {
4949
};
5050

5151
pub const math = struct {
52-
pub fn absCast(value: anytype) @TypeOf(@abs(value)) {
52+
pub inline fn absCast(value: anytype) @TypeOf(@abs(value)) {
5353
return @abs(value);
5454
}
5555
};

src/root.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
//!
3131
//! var buf: [content.len + zigpak.PREFIX_BUFSIZE]u8 = undefined;
3232
//! const prefix = zigpak.AnyStr.prefix(@intCast(content.len));
33-
//! std.mem.copyForward(u8, &buf, prefix.constSlice());
34-
//! std.mem.copyForward(u8, buf[prefix.len..], content);
33+
//! @memcpy(u8, &buf, prefix.constSlice());
34+
//! @memcpy(u8, buf[prefix.len..], content);
3535
//!
3636
//! const result = buf[0..prefix.len + content.len]; // the constructed value
3737
//! ```
@@ -59,15 +59,15 @@
5959
//! AnyArray.pipe(buf.writer(), 3) catch unreachable;
6060
//!
6161
//! { // The first element: nil
62-
//! Nil.pipe(buf.writer()) catch unreachable;
62+
//! _ = Nil.pipe(buf.writer()) catch unreachable;
6363
//! }
6464
//!
6565
//! { // The second element: int 1
66-
//! Int(i8).pipe(buf.writer(), 1) catch unreachable;
66+
//! _ = Int(i8).pipe(buf.writer(), 1) catch unreachable;
6767
//! }
6868
//!
6969
//! { // The third element: a string
70-
//! AnyStr.pipe(buf.writer(), @intCast(strContent.len)) catch unreachable;
70+
//! _ = AnyStr.pipe(buf.writer(), @intCast(strContent.len)) catch unreachable;
7171
//! buf.appendSliceAssumeCapacity(strContent);
7272
//! }
7373
//!

0 commit comments

Comments
 (0)