Skip to content

Commit 329e150

Browse files
committed
More mem improvements
1 parent a350eff commit 329e150

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

solana-programs/programs/tuktuk/src/instructions/queue_task_v0.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use anchor_lang::{
55

66
use crate::{
77
error::ErrorCode,
8-
resize_to_fit::resize_to_fit,
98
state::{TaskQueueAuthorityV0, TaskQueueDataWrapper, TaskV0, TransactionSourceV0, TriggerV0},
109
};
1110

@@ -41,7 +40,7 @@ pub struct QueueTaskV0<'info> {
4140
#[account(
4241
init,
4342
payer = payer,
44-
space = 8 + std::mem::size_of::<TaskV0>() + 60 + args.description.len(),
43+
space = 8 + std::mem::size_of::<TaskV0>() + args.transaction.size() + args.description.len() + 60,
4544
seeds = [b"task".as_ref(), task_queue.key().as_ref(), &args.id.to_le_bytes()[..]],
4645
bump,
4746
)]
@@ -81,11 +80,10 @@ pub fn handler(ctx: Context<QueueTaskV0>, args: QueueTaskArgsV0) -> Result<()> {
8180
require_gte!(crank_reward, task_queue.header().min_crank_reward);
8281

8382
let mut transaction = args.transaction;
84-
if let TransactionSourceV0::CompiledV0(mut compiled_tx) = transaction {
83+
if let TransactionSourceV0::CompiledV0(ref mut compiled_tx) = transaction {
8584
compiled_tx
8685
.accounts
8786
.extend(ctx.remaining_accounts.iter().map(|a| a.key()));
88-
transaction = TransactionSourceV0::CompiledV0(compiled_tx);
8987
}
9088
ctx.accounts.task.set_inner(TaskV0 {
9189
free_tasks: args.free_tasks,
@@ -106,15 +104,9 @@ pub fn handler(ctx: Context<QueueTaskV0>, args: QueueTaskArgsV0) -> Result<()> {
106104
task_queue.header_mut().updated_at = Clock::get()?.unix_timestamp;
107105
task_queue.save()?;
108106

109-
// Drop the borrow before calling resize_to_fit
107+
// Drop the borrow
110108
drop(task_queue_data);
111109

112-
resize_to_fit(
113-
&ctx.accounts.payer.to_account_info(),
114-
&ctx.accounts.system_program.to_account_info(),
115-
&ctx.accounts.task,
116-
)?;
117-
118110
let rented_amount = ctx.accounts.task.to_account_info().lamports();
119111
ctx.accounts.task.rent_amount = rented_amount;
120112

solana-programs/programs/tuktuk/src/state.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ pub struct CompiledInstructionV0 {
308308

309309
impl CompiledInstructionV0 {
310310
pub fn size(&self) -> usize {
311-
1 + self.accounts.len() + self.data.len()
311+
1 + 4 + self.accounts.len() + 4 + self.data.len()
312312
}
313313
}
314314

@@ -334,6 +334,26 @@ pub struct CompiledTransactionV0 {
334334

335335
impl CompiledTransactionV0 {
336336
pub fn size(&self) -> usize {
337-
1 + self.accounts.len() + self.instructions.iter().map(|i| i.size()).sum::<usize>()
337+
// Calculate the size of the transaction header (3 u8 fields + 1 byte for instruction count)
338+
let header_size = 3 + 1;
339+
// Calculate the size of all accounts (each Pubkey is 32 bytes)
340+
let accounts_size = 4 + self.accounts.len() * 32;
341+
// Calculate the size of all instructions
342+
let instructions_size = 4 + self.instructions.iter().map(|i| i.size()).sum::<usize>();
343+
344+
header_size + accounts_size + instructions_size
345+
}
346+
}
347+
348+
impl TransactionSourceV0 {
349+
pub fn size(&self) -> usize {
350+
match self {
351+
TransactionSourceV0::CompiledV0(compiled_tx) => compiled_tx.size(),
352+
TransactionSourceV0::RemoteV0 { url, signer: _ } => {
353+
// For remote transactions, we need to account for the URL string length
354+
// and the signer pubkey (32 bytes)
355+
32 + 4 + url.len() // 4 bytes for string length prefix
356+
}
357+
}
338358
}
339359
}

0 commit comments

Comments
 (0)