Skip to content

Commit 8dd63ae

Browse files
committed
enforce inlining in Int, Float and Bool
1 parent d69dc03 commit 8dd63ae

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/root.zig

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,6 @@ pub fn Int(T: type) type {
145145
return 1 + nbytes;
146146
}
147147

148-
pub fn countSm(value: T) usize {
149-
return pipeSm(std.io.null_writer, value) catch unreachable;
150-
}
151-
152148
fn headerOf(value: T) u8 {
153149
return if (signed) switch (nbytes) {
154150
0 => makeFixIntNeg(@intCast(value)),
@@ -191,6 +187,10 @@ pub fn Int(T: type) type {
191187
return @call(.always_inline, pipe, .{ stream.writer(), value }) catch unreachable;
192188
}
193189

190+
pub fn countSm(value: T) usize {
191+
return @call(.always_inline, pipeSm, .{ std.io.null_writer, value }) catch unreachable;
192+
}
193+
194194
/// Write integer into `writer` uses smallest msgpack type.
195195
pub fn pipeSm(writer: anytype, value: T) !usize {
196196
if (value >= 0) {
@@ -223,8 +223,7 @@ pub fn Int(T: type) type {
223223

224224
pub fn writeSm(dst: []u8, value: T) usize {
225225
var stream = std.io.fixedBufferStream(dst);
226-
// TODO: enforce always_inline
227-
return @call(.always_inline, pipeSm, .{ stream.writer(), value }) catch unreachable;
226+
return pipeSm(stream.writer(), value) catch unreachable;
228227
}
229228
};
230229
}
@@ -269,11 +268,11 @@ pub fn Float(T: type) type {
269268

270269
pub fn write(dst: []u8, value: T) usize {
271270
var stream = std.io.fixedBufferStream(dst);
272-
return pipe(stream.writer(), value) catch unreachable;
271+
return @call(.always_inline, pipe, .{ stream.writer(), value }) catch unreachable;
273272
}
274273

275274
pub fn countSm(value: T) usize {
276-
return pipeSm(std.io.null_writer, value) catch unreachable;
275+
return @call(.always_inline, pipeSm, .{ std.io.null_writer, value }) catch unreachable;
277276
}
278277

279278
pub fn pipeSm(writer: anytype, value: T) !usize {
@@ -288,7 +287,7 @@ pub fn Float(T: type) type {
288287

289288
pub fn writeSm(dst: []u8, value: T) usize {
290289
var stream = std.io.fixedBufferStream(dst);
291-
return pipeSm(stream.writer(), value) catch unreachable;
290+
return @call(.always_inline, pipeSm, .{ stream.writer(), value }) catch unreachable;
292291
}
293292
};
294293
}
@@ -325,17 +324,21 @@ pub const Bool = struct {
325324
return 1;
326325
}
327326

328-
pub fn pipe(writer: anytype, value: bool) !usize {
329-
_ = try writer.writeByte(switch (value) {
327+
inline fn convert(value: bool) u8 {
328+
return switch (value) {
330329
true => 0xc3,
331330
false => 0xc2,
332-
});
331+
};
332+
}
333+
334+
pub fn pipe(writer: anytype, value: bool) !usize {
335+
_ = try writer.writeByte(convert(value));
333336
return 1;
334337
}
335338

336339
pub fn write(dst: []u8, value: bool) usize {
337-
var stream = std.io.fixedBufferStream(dst);
338-
return pipe(stream.writer(), value) catch unreachable;
340+
dst[0] = convert(value);
341+
return 1;
339342
}
340343
};
341344

0 commit comments

Comments
 (0)