Skip to content

Commit 387fa68

Browse files
committed
accdb: move slot field to DB layer
Removes the slot field from account_meta (runtime layer). The database layer is aware of the slot numbers in which accounts change anyways. This saves 8 bytes per account and removes a cache miss during snapshot loading (checking if a duplicate account is newer or obsolete). This is a low-risk change because the slot field is not consensus- relevant outside of snapshot loading.
1 parent 549ed2a commit 387fa68

File tree

14 files changed

+23
-40
lines changed

14 files changed

+23
-40
lines changed

src/discof/genesis/fd_genesi_tile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ initialize_accdb( fd_genesi_tile_t * ctx ) {
138138
void * data = (void *)( meta+1 );
139139
fd_memcpy( meta->owner, account->account.owner.uc, sizeof(fd_pubkey_t) );
140140
meta->lamports = account->account.lamports;
141-
meta->slot = 0UL;
141+
rec->slot = 0UL;
142142
meta->executable = !!account->account.executable;
143143
meta->dlen = (uint)account->account.data_len;
144144
fd_memcpy( data, fd_solana_account_data_join( &account->account ), account->account.data_len );

src/discof/restore/fd_snapin_tile_funk.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fd_snapin_process_account_header_funk( fd_snapin_tile_t * ctx,
4848
if( FD_UNLIKELY( existing_rec ) ) {
4949
fd_account_meta_t * meta = fd_funk_val( existing_rec, funk->wksp );
5050
if( FD_UNLIKELY( meta ) ) {
51-
if( FD_LIKELY( meta->slot>result->account_header.slot ) ) {
51+
if( FD_LIKELY( rec->slot>result->account_header.slot ) ) {
5252
ctx->acc_data = NULL;
5353
fd_snapin_send_duplicate_account( ctx, result->account_header.lamports, NULL, result->account_header.data_len, (uchar)result->account_header.executable, result->account_header.owner, result->account_header.pubkey, 0, &early_exit );
5454
return early_exit;
@@ -78,7 +78,7 @@ fd_snapin_process_account_header_funk( fd_snapin_tile_t * ctx,
7878
rec->val_sz = (uint)( alloc_sz & FD_FUNK_REC_VAL_MAX );
7979

8080
meta->dlen = (uint)result->account_header.data_len;
81-
meta->slot = result->account_header.slot;
81+
rec->slot = result->account_header.slot;
8282
memcpy( meta->owner, result->account_header.owner, sizeof(fd_pubkey_t) );
8383
meta->lamports = result->account_header.lamports;
8484
meta->executable = (uchar)result->account_header.executable;
@@ -129,10 +129,10 @@ streamlined_insert( fd_snapin_tile_t * ctx,
129129
rec->val_gaddr = fd_wksp_gaddr_fast( funk->wksp, meta );
130130
rec->val_max = (uint)( fd_ulong_min( alloc_max, FD_FUNK_REC_VAL_MAX ) & FD_FUNK_REC_VAL_MAX );
131131
rec->val_sz = (uint)( alloc_sz & FD_FUNK_REC_VAL_MAX );
132+
rec->slot = slot;
132133

133134
/* Write metadata */
134135
meta->dlen = (uint)data_len;
135-
meta->slot = slot;
136136
memcpy( meta->owner, owner, sizeof(fd_pubkey_t) );
137137
meta->lamports = lamports;
138138
meta->executable = (uchar)executable;
@@ -237,11 +237,11 @@ fd_snapin_process_account_batch_funk( fd_snapin_tile_t * ctx,
237237
fd_account_meta_t const * existing = fd_funk_val( r, funk->wksp );
238238
if( FD_UNLIKELY( !existing ) ) FD_LOG_HEXDUMP_NOTICE(( "r", r, sizeof(fd_funk_rec_t) ));
239239
FD_TEST( existing );
240-
if( existing->slot > slot ) {
240+
if( r->slot > slot ) {
241241
rec[ i ] = NULL; /* skip record if existing value is newer */
242242
/* send the skipped account to the subtracting hash tile */
243243
fd_snapin_send_duplicate_account( ctx, lamports, data, data_len, executable, owner, pubkey, 1, &early_exit );
244-
} else if( slot > existing->slot) {
244+
} else if( slot > r->slot) {
245245
/* send the to-be-replaced account to the subtracting hash tile */
246246
fd_snapin_send_duplicate_account( ctx, existing->lamports, (uchar const *)existing + sizeof(fd_account_meta_t), existing->dlen, existing->executable, existing->owner, pubkey, 1, &early_exit );
247247
} else { /* slot==existing->slot */
@@ -310,7 +310,6 @@ fd_snapin_read_account_funk( fd_snapin_tile_t * ctx,
310310

311311
memcpy( meta->owner, fd_accdb_ref_owner( peek->acc ), sizeof(fd_pubkey_t) );
312312
meta->lamports = fd_accdb_ref_lamports( peek->acc );
313-
meta->slot = fd_accdb_ref_slot( peek->acc );
314313
meta->dlen = (uint)data_sz;
315314
meta->executable = !!fd_accdb_ref_exec_bit( peek->acc );
316315
fd_memcpy( data, fd_accdb_ref_data_const( peek->acc ), data_sz );

src/discof/restore/fd_snapin_tile_vinyl.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,6 @@ fd_snapin_process_account_header_vinyl( fd_snapin_tile_t * ctx,
482482
memset( meta, 0, sizeof(fd_account_meta_t) ); /* bulk zero */
483483
memcpy( meta->owner, result->account_header.owner, sizeof(fd_pubkey_t) );
484484
meta->lamports = result->account_header.lamports;
485-
meta->slot = result->account_header.slot;
486485
meta->dlen = (uint)result->account_header.data_len;
487486
meta->executable = (uchar)result->account_header.executable;
488487

@@ -651,7 +650,6 @@ fd_snapin_process_account_batch_vinyl( fd_snapin_tile_t * ctx,
651650
memset( meta, 0, sizeof(fd_account_meta_t) ); /* bulk zero */
652651
memcpy( meta->owner, owner, sizeof(fd_pubkey_t) );
653652
meta->lamports = lamports;
654-
meta->slot = result->account_batch.slot;
655653
meta->dlen = (uint)data_len;
656654
meta->executable = !!executable;
657655

src/flamenco/accdb/fd_accdb_impl_v1.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,9 @@ fd_accdb_prep_create( fd_accdb_rw_t * rw,
269269
rec->tag = 0;
270270
rec->prev_idx = FD_FUNK_REC_IDX_NULL;
271271
rec->next_idx = FD_FUNK_REC_IDX_NULL;
272+
rec->slot = xid->ul[0];
272273

273274
fd_account_meta_t * meta = val;
274-
meta->slot = xid->ul[0];
275275

276276
accdb->base.rw_active++;
277277
*rw = (fd_accdb_rw_t) {

src/flamenco/accdb/fd_accdb_ref.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fd_accdb_ref_exec_bit( fd_accdb_ro_t const * ro ) {
6868

6969
static inline ulong
7070
fd_accdb_ref_slot( fd_accdb_ro_t const * ro ) {
71-
return ro->meta->slot;
71+
return ro->rec->slot;
7272
}
7373

7474
// void
@@ -165,7 +165,7 @@ fd_accdb_ref_exec_bit_set( fd_accdb_rw_t * rw,
165165
static inline void
166166
fd_accdb_ref_slot_set( fd_accdb_rw_t * rw,
167167
ulong slot ) {
168-
rw->meta->slot = slot;
168+
rw->rec->slot = slot;
169169
}
170170

171171
FD_PROTOTYPES_END

src/flamenco/fd_flamenco_base.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ typedef struct fd_log_collector fd_log_collector_t;
109109
struct fd_account_meta {
110110
uchar owner[32];
111111
ulong lamports;
112-
ulong slot;
113112
uint dlen;
114113
uchar executable;
115114
uchar padding[3];

src/flamenco/rewards/fd_rewards.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ calculate_rewards_and_distribute_vote_rewards( fd_bank_t * ba
813813
fd_txn_account_get_data( vote_rec ),
814814
prev_hash );
815815

816-
fd_txn_account_set_slot( vote_rec, fd_bank_slot_get( bank ) );
816+
prepare.rec->slot = fd_bank_slot_get( bank );
817817

818818
if( FD_UNLIKELY( fd_txn_account_checked_add_lamports( vote_rec, rewards ) ) ) {
819819
FD_LOG_ERR(( "Adding lamports to vote account would cause overflow" ));
@@ -879,7 +879,7 @@ distribute_epoch_reward_to_stake_acc( fd_bank_t * bank,
879879
fd_txn_account_get_data( stake_acc_rec ),
880880
prev_hash );
881881

882-
fd_txn_account_set_slot( stake_acc_rec, fd_bank_slot_get( bank ) );
882+
prepare.rec->slot = fd_bank_slot_get( bank );
883883

884884
fd_stake_state_v2_t stake_state[1] = {0};
885885
if( fd_stake_get_state( stake_acc_rec, stake_state ) != 0 ) {

src/flamenco/runtime/fd_executor.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,9 +1390,7 @@ fd_execute_instr( fd_runtime_t * runtime,
13901390
}
13911391

13921392
void
1393-
fd_executor_reclaim_account( fd_txn_account_t * account,
1394-
ulong slot ) {
1395-
fd_txn_account_set_slot( account, slot );
1393+
fd_executor_reclaim_account( fd_txn_account_t * account ) {
13961394
if( FD_UNLIKELY( fd_txn_account_get_lamports( account )==0UL ) ) {
13971395
fd_txn_account_set_data_len( account, 0UL );
13981396
fd_txn_account_clear_owner( account );

src/flamenco/runtime/fd_executor.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ fd_executor_txn_check( fd_bank_t * bank,
109109
fd_txn_out_t * txn_out );
110110

111111
void
112-
fd_executor_reclaim_account( fd_txn_account_t * account,
113-
ulong slot );
112+
fd_executor_reclaim_account( fd_txn_account_t * account );
114113

115114
/* fd_io_strerror converts an FD_EXECUTOR_INSTR_ERR_{...} code into a
116115
human readable cstr. The lifetime of the returned pointer is

src/flamenco/runtime/fd_runtime.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ fd_runtime_freeze( fd_bank_t * bank,
318318

319319
/* TODO: is it ok to not check the overflow error here? */
320320
fd_txn_account_checked_add_lamports( rec, fees );
321-
fd_txn_account_set_slot( rec, fd_bank_slot_get( bank ) );
321+
prepare.rec->slot = fd_bank_slot_get( bank );
322322

323323
fd_hashes_update_lthash( rec, prev_hash, bank, capture_ctx );
324324
fd_txn_account_mutable_fini( rec, accdb, &prepare );
@@ -1018,6 +1018,7 @@ fd_runtime_finalize_account( fd_funk_t * funk,
10181018
FD_LOG_CRIT(( "fd_runtime_finalize_account: account is not mutable" ));
10191019
}
10201020

1021+
ulong slot = xid->ul[0];
10211022
fd_pubkey_t const * key = acc->pubkey;
10221023
uchar const * record_data = (uchar *)fd_txn_account_get_meta( acc );
10231024
ulong record_sz = fd_account_meta_get_record_sz( acc->meta );
@@ -1033,6 +1034,7 @@ fd_runtime_finalize_account( fd_funk_t * funk,
10331034
FD_LOG_ERR(( "fd_runtime_finalize_account: failed to prepare record (%i-%s)", err, fd_funk_strerror( err ) ));
10341035
}
10351036

1037+
rec->slot = slot;
10361038
if( FD_UNLIKELY( !fd_funk_val_truncate(
10371039
rec,
10381040
fd_funk_alloc( funk ),
@@ -1049,6 +1051,7 @@ fd_runtime_finalize_account( fd_funk_t * funk,
10491051

10501052
} else {
10511053

1054+
prev_rec->slot = slot;
10521055
if( FD_UNLIKELY( !fd_funk_val_truncate(
10531056
prev_rec,
10541057
fd_funk_alloc( funk ),
@@ -1133,7 +1136,8 @@ fd_runtime_buffer_solcap_account_update( fd_txn_account_t * account,
11331136
- Queries funk for the previous account version
11341137
- Computes the hash of the previous version (or uses zero for new)
11351138
- Calls fd_hashes_update_lthash with the computed previous hash
1136-
- Saves the new version of the account to Funk
1139+
- Saves the new version of the account to Funk (also updates the
1140+
record's slot field)
11371141
- Notifies the replay tile that an account update has occurred, so it
11381142
can write the account to the solcap file.
11391143
@@ -1251,7 +1255,7 @@ fd_runtime_commit_txn( fd_runtime_t * runtime,
12511255

12521256
/* Reclaim any accounts that have 0-lamports, now that any related
12531257
cache updates have been applied. */
1254-
fd_executor_reclaim_account( &txn_out->accounts.accounts[i], fd_bank_slot_get( bank ) );
1258+
fd_executor_reclaim_account( &txn_out->accounts.accounts[i] );
12551259

12561260
fd_runtime_save_account( runtime->funk, &xid, &txn_out->accounts.accounts[i], bank, runtime->log.capture_ctx );
12571261
}

0 commit comments

Comments
 (0)