Skip to content

Commit 1b5d3d8

Browse files
author
Timothy Zakian
committed
[sui-adapter] Add basic package cache
1 parent b875357 commit 1b5d3d8

File tree

9 files changed

+87
-30
lines changed

9 files changed

+87
-30
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Mysten Labs, Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use crate::data_store::PackageStore;
5+
use std::{cell::RefCell, collections::BTreeMap, rc::Rc};
6+
use sui_types::{base_types::ObjectID, error::SuiResult, move_package::MovePackage};
7+
8+
pub struct CachedPackageStore<'state> {
9+
pub package_store: Box<dyn PackageStore + 'state>,
10+
pub package_cache: RefCell<BTreeMap<ObjectID, Option<Rc<MovePackage>>>>,
11+
pub max_cache_size: usize,
12+
}
13+
14+
impl<'state> CachedPackageStore<'state> {
15+
pub const DEFAULT_MAX_CACHE_SIZE: usize = 200;
16+
pub fn new(package_store: Box<dyn PackageStore + 'state>) -> Self {
17+
Self {
18+
package_store,
19+
package_cache: RefCell::new(BTreeMap::new()),
20+
max_cache_size: Self::DEFAULT_MAX_CACHE_SIZE,
21+
}
22+
}
23+
24+
pub fn get_package(&self, id: &ObjectID) -> SuiResult<Option<Rc<MovePackage>>> {
25+
if let Some(pkg) = self.package_cache.borrow().get(id).cloned() {
26+
return Ok(pkg);
27+
}
28+
29+
if self.package_cache.borrow().len() >= self.max_cache_size {
30+
self.package_cache.borrow_mut().clear();
31+
}
32+
33+
let pkg = self.package_store.get_package(id)?;
34+
self.package_cache.borrow_mut().insert(*id, pkg.clone());
35+
Ok(pkg)
36+
}
37+
}
38+
39+
impl PackageStore for CachedPackageStore<'_> {
40+
fn get_package(&self, id: &ObjectID) -> SuiResult<Option<Rc<MovePackage>>> {
41+
self.get_package(id)
42+
}
43+
}

sui-execution/latest/sui-adapter/src/programmable_transactions/linkage_view.rs renamed to sui-execution/latest/sui-adapter/src/data_store/linkage_view.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Mysten Labs, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use crate::programmable_transactions::data_store::PackageStore;
4+
use crate::data_store::PackageStore;
55
use move_core_types::{
66
account_address::AccountAddress,
77
identifier::{IdentStr, Identifier},
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Mysten Labs, Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
pub mod cached_data_store;
5+
pub mod linkage_view;
6+
pub mod sui_data_store;
7+
8+
use std::rc::Rc;
9+
use sui_types::{
10+
base_types::ObjectID, error::SuiResult, move_package::MovePackage, storage::BackingPackageStore,
11+
};
12+
13+
// A unifying trait that allows us to load move packages that may not be objects just yet (e.g., if
14+
// they were published in the current transaction). Note that this needs to load `MovePackage`s and
15+
// not `MovePackageObject`s.
16+
pub trait PackageStore {
17+
fn get_package(&self, id: &ObjectID) -> SuiResult<Option<Rc<MovePackage>>>;
18+
}
19+
20+
impl<T: BackingPackageStore> PackageStore for T {
21+
fn get_package(&self, id: &ObjectID) -> SuiResult<Option<Rc<MovePackage>>> {
22+
Ok(self
23+
.get_package_object(id)?
24+
.map(|x| Rc::new(x.move_package().clone())))
25+
}
26+
}

sui-execution/latest/sui-adapter/src/programmable_transactions/data_store.rs renamed to sui-execution/latest/sui-adapter/src/data_store/sui_data_store.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
// Copyright (c) Mysten Labs, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use crate::programmable_transactions::linkage_view::LinkageView;
4+
use crate::data_store::{PackageStore, linkage_view::LinkageView};
55
use move_binary_format::errors::{Location, PartialVMError, PartialVMResult, VMResult};
66
use move_core_types::{
77
account_address::AccountAddress, identifier::IdentStr, language_storage::ModuleId,
88
resolver::ModuleResolver, vm_status::StatusCode,
99
};
1010
use move_vm_types::data_store::DataStore;
1111
use std::rc::Rc;
12-
use sui_types::{
13-
base_types::ObjectID, error::SuiResult, move_package::MovePackage, storage::BackingPackageStore,
14-
};
12+
use sui_types::{base_types::ObjectID, error::SuiResult, move_package::MovePackage};
1513

1614
// Implementation of the `DataStore` trait for the Move VM.
1715
// When used during execution it may have a list of new packages that have
@@ -101,21 +99,6 @@ impl DataStore for SuiDataStore<'_, '_> {
10199
}
102100
}
103101

104-
// A unifying trait that allows us to load move packages that may not be objects just yet (e.g., if
105-
// they were published in the current transaction). Note that this needs to load `MovePackage`s and
106-
// not `MovePackageObject`s.
107-
pub trait PackageStore {
108-
fn get_package(&self, id: &ObjectID) -> SuiResult<Option<Rc<MovePackage>>>;
109-
}
110-
111-
impl<T: BackingPackageStore> PackageStore for T {
112-
fn get_package(&self, id: &ObjectID) -> SuiResult<Option<Rc<MovePackage>>> {
113-
Ok(self
114-
.get_package_object(id)?
115-
.map(|x| Rc::new(x.move_package().clone())))
116-
}
117-
}
118-
119102
impl PackageStore for SuiDataStore<'_, '_> {
120103
fn get_package(&self, id: &ObjectID) -> SuiResult<Option<Rc<MovePackage>>> {
121104
for package in self.new_packages {

sui-execution/latest/sui-adapter/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
extern crate sui_types;
66

77
pub mod adapter;
8+
pub mod data_store;
89
pub mod error;
910
pub mod execution_engine;
1011
pub mod execution_mode;

sui-execution/latest/sui-adapter/src/programmable_transactions/context.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ pub use checked::*;
77
mod checked {
88
use crate::{
99
adapter::new_native_extensions,
10+
data_store::{
11+
PackageStore, cached_data_store::CachedPackageStore, linkage_view::LinkageView,
12+
sui_data_store::SuiDataStore,
13+
},
1014
error::convert_vm_error,
1115
execution_mode::ExecutionMode,
1216
execution_value::{
@@ -15,10 +19,6 @@ mod checked {
1519
},
1620
gas_charger::GasCharger,
1721
gas_meter::SuiGasMeter,
18-
programmable_transactions::{
19-
data_store::{PackageStore, SuiDataStore},
20-
linkage_view::LinkageView,
21-
},
2222
type_resolver::TypeTagResolver,
2323
};
2424
use move_binary_format::{
@@ -147,7 +147,9 @@ mod checked {
147147
where
148148
'a: 'state,
149149
{
150-
let mut linkage_view = LinkageView::new(Box::new(state_view.as_sui_resolver()));
150+
let mut linkage_view = LinkageView::new(Box::new(CachedPackageStore::new(Box::new(
151+
state_view.as_sui_resolver(),
152+
))));
151153
let mut input_object_map = BTreeMap::new();
152154
let inputs = inputs
153155
.into_iter()

sui-execution/latest/sui-adapter/src/programmable_transactions/execution.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ pub use checked::*;
77
mod checked {
88
use crate::{
99
adapter::substitute_package_id,
10+
data_store::sui_data_store::SuiDataStore,
1011
execution_mode::ExecutionMode,
1112
execution_value::{
1213
CommandKind, ExecutionState, ObjectContents, ObjectValue, RawValueType, Value,
1314
ensure_serialized_size,
1415
},
1516
gas_charger::GasCharger,
16-
programmable_transactions::{context::*, data_store::SuiDataStore},
17+
programmable_transactions::context::*,
1718
type_resolver::TypeTagResolver,
1819
};
1920
use move_binary_format::file_format::AbilitySet;

sui-execution/latest/sui-adapter/src/programmable_transactions/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
pub mod context;
5-
pub mod data_store;
65
pub mod execution;
7-
pub mod linkage_view;

sui-execution/latest/sui-adapter/src/type_layout_resolver.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Copyright (c) Mysten Labs, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
use crate::data_store::cached_data_store::CachedPackageStore;
5+
use crate::data_store::linkage_view::LinkageView;
46
use crate::programmable_transactions::context::load_type_from_struct;
5-
use crate::programmable_transactions::linkage_view::LinkageView;
67
use move_core_types::annotated_value as A;
78
use move_core_types::language_storage::StructTag;
89
use move_vm_runtime::move_vm::MoveVM;
@@ -26,7 +27,9 @@ struct NullSuiResolver<'state>(Box<dyn TypeLayoutStore + 'state>);
2627

2728
impl<'state, 'vm> TypeLayoutResolver<'state, 'vm> {
2829
pub fn new(vm: &'vm MoveVM, state_view: Box<dyn TypeLayoutStore + 'state>) -> Self {
29-
let linkage_view = LinkageView::new(Box::new(NullSuiResolver(state_view)));
30+
let linkage_view = LinkageView::new(Box::new(CachedPackageStore::new(Box::new(
31+
NullSuiResolver(state_view),
32+
))));
3033
Self { vm, linkage_view }
3134
}
3235
}

0 commit comments

Comments
 (0)