Skip to content

Commit 2771518

Browse files
authored
entrypoint: Optimize to reduce by a few CUs (#9)
#### Problem There's a few inefficiencies in the entrypoint taking up a few unnecessary CUs. #### Summary of changes Do some math all together to use fewer CUs during the entrypoint. For the transfer-lamports example in Rosetta, this brings CU usage down from 44 CUs to 38 CUs.
1 parent 2396238 commit 2771518

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

src/account.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ pub const Account = struct {
5252

5353
ptr: *Account.Data,
5454

55+
pub fn fromDataPtr(ptr: *Account.Data) Account {
56+
return Account { .ptr = ptr };
57+
}
58+
5559
pub fn id(self: Account) PublicKey {
5660
return self.ptr.id;
5761
}

src/context.zig

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,42 @@ const allocator = @import("allocator.zig").allocator;
66
const PublicKey = @import("public_key.zig").PublicKey;
77

88
pub const Context = struct {
9-
num_accounts: usize,
9+
num_accounts: u64,
1010
accounts: [64]Account,
1111
data: []const u8,
1212
program_id: *align(1) PublicKey,
1313

1414
pub fn load(input: [*]u8) !Context {
1515
var ptr: [*]u8 = input;
1616

17-
const num_accounts = std.mem.bytesToValue(usize, ptr[0..@sizeOf(usize)]);
18-
ptr += @sizeOf(usize);
17+
const num_accounts: *u64 = @ptrCast(@alignCast(ptr));
18+
ptr += @sizeOf(u64);
1919

2020
var i: usize = 0;
2121
var accounts: [64]Account = undefined;
22-
while (i < num_accounts) {
23-
const data: *align(1) Account.Data = @ptrCast(ptr);
22+
while (i < num_accounts.*) {
23+
const data: *Account.Data = @ptrCast(@alignCast(ptr));
2424
if (data.duplicate_index != std.math.maxInt(u8)) {
25-
ptr += @sizeOf(usize);
25+
ptr += @sizeOf(u64);
2626
accounts[i] = accounts[data.duplicate_index];
2727
} else {
28-
ptr += Account.DATA_HEADER;
29-
ptr = @as([*]u8, @ptrFromInt(std.mem.alignForward(usize, @intFromPtr(ptr) + data.data_len + ACCOUNT_DATA_PADDING, @alignOf(usize))));
30-
ptr += @sizeOf(u64);
31-
accounts[i] = .{ .ptr = @as(*Account.Data, @ptrCast(@alignCast(data))) };
28+
accounts[i] = Account.fromDataPtr(data);
29+
ptr += Account.DATA_HEADER + data.data_len + ACCOUNT_DATA_PADDING + @sizeOf(u64);
30+
ptr = @ptrFromInt(std.mem.alignForward(u64, @intFromPtr(ptr), @alignOf(u64)));
3231
}
3332
i += 1;
3433
}
3534

36-
const data_len = std.math.cast(usize, std.mem.bytesToValue(u64, ptr[0..@sizeOf(u64)])) orelse return error.DataTooLarge;
35+
const data_len: *u64 = @ptrCast(@alignCast(ptr));
3736
ptr += @sizeOf(u64);
3837

39-
const data = ptr[0..data_len];
40-
ptr += data_len;
38+
const data = ptr[0..data_len.*];
39+
ptr += data_len.*;
4140

4241
const program_id = @as(*align(1) PublicKey, @ptrCast(ptr));
43-
ptr += @sizeOf(PublicKey);
4442

4543
return Context{
46-
.num_accounts = num_accounts,
44+
.num_accounts = num_accounts.*,
4745
.accounts = accounts,
4846
.data = data,
4947
.program_id = program_id,

0 commit comments

Comments
 (0)