Skip to content

Commit 12da4da

Browse files
committed
HeaderType.nextComponentSize: add lookup table
1 parent 7a82656 commit 12da4da

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

src/budopts.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
/// like the mispredict problem, in exchange of program size.
55
///
66
/// Please be adviced that lookup tables do not always boost
7-
/// performance.
7+
/// performance. While using lookup tables, behaviour may not be
8+
/// completely same to the spec.
89
pub const LookupTableOptimize = enum {
910
/// Don't use lookup table if possible.
1011
///
1112
/// This option emits smallest object.
1213
none,
13-
/// Use small lookup tables (<= 64 bytes each).
14+
/// Use small lookup tables (<= 64 bytes each, assume usize is 8 bytes).
1415
///
1516
/// This may boost performance for certain
1617
/// platforms.

src/root.zig

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@
9393
//!
9494

9595
pub const io = @import("./io.zig");
96-
const budopts = @import("budopts");
96+
pub const LookupTableOptimize = @import("./budopts.zig").LookupTableOptimize;
97+
98+
pub const lookupTableMode: LookupTableOptimize = @enumFromInt(@intFromEnum(@import("budopts").lookupTable));
9799

98100
test {
99101
_ = io;
@@ -680,7 +682,7 @@ pub const HeaderType = union(enum) {
680682

681683
/// Convert a value to `HeaderType`.
682684
pub fn from(value: u8) ?HeaderType {
683-
switch (budopts.lookupTable) {
685+
switch (lookupTableMode) {
684686
.all => {
685687
return lookupAll(value);
686688
},
@@ -693,8 +695,27 @@ pub const HeaderType = union(enum) {
693695
}
694696
}
695697

696-
pub fn nextComponentSize(self: @This()) usize {
697-
// TODO: Needs lookup table
698+
/// Look up next component size for the .small level.
699+
///
700+
/// This function accepts bin64/str64/ext64, which are invalid in the spec.
701+
inline fn lookupNextComponentSize(self: HeaderType) usize {
702+
const REGULAR = [_]usize{ 1, 2, 4, 8 };
703+
const FLOAT = [_]usize{ 4, 8 };
704+
const ARRAY = [_]usize{ 2, 4 };
705+
706+
return switch (self) {
707+
.nil, .bool, .fixint, .fixarray, .fixmap => 0,
708+
.bin, .str => |n| REGULAR[n],
709+
.fixstr => |n| n,
710+
.fixext => |n| 1 + n,
711+
.ext => |n| 1 + REGULAR[n],
712+
.int, .uint => |n| REGULAR[n],
713+
.float => |n| FLOAT[n],
714+
.array, .map => |n| ARRAY[n],
715+
};
716+
}
717+
718+
inline fn findNextComponentSize(self: HeaderType) usize {
698719
return switch (self) {
699720
.nil, .bool, .fixint, .fixarray, .fixmap => 0,
700721
.bin, .str => |n| switch (n) {
@@ -728,6 +749,26 @@ pub const HeaderType = union(enum) {
728749
};
729750
}
730751

752+
/// Return the next component size of the value.
753+
///
754+
/// ```
755+
/// | HeaderType | header data | payload |
756+
/// ```
757+
///
758+
/// - For fixed-number types, return the payload size.
759+
/// - For fixed array and maps, return 0.
760+
/// - For the other types, return the header data size.
761+
pub fn nextComponentSize(self: HeaderType) usize {
762+
switch (lookupTableMode) {
763+
.all, .small => {
764+
return lookupNextComponentSize(self);
765+
},
766+
.none => {
767+
return findNextComponentSize(self);
768+
},
769+
}
770+
}
771+
731772
pub fn family(self: HeaderType) ValueTypeFamily {
732773
return switch (self) {
733774
.nil => .nil,

0 commit comments

Comments
 (0)