Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundecies #2

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/dojo/core-cairo-test/src/tests/model/model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn test_values() {
fn test_from_values() {
let mut values = [3, 4].span();

let model_values: Option<FooValue> = ModelValue::<FooValue>::from_serialized(1, ref values);
let model_values: Option<FooValue> = ModelValue::<FooValue>::from_serialized(values);
assert!(model_values.is_some());
let model_values = model_values.unwrap();
assert!(model_values.v1 == 3 && model_values.v2 == 4);
Expand All @@ -71,7 +71,7 @@ fn test_from_values() {
#[test]
fn test_from_values_bad_data() {
let mut values = [3].span();
let res: Option<FooValue> = ModelValue::<FooValue>::from_serialized(1, ref values);
let res: Option<FooValue> = ModelValue::<FooValue>::from_serialized(values);
assert!(res.is_none());
}

Expand Down
4 changes: 2 additions & 2 deletions crates/dojo/core/src/model/model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub trait Model<M> {
/// Returns the values of the model.
fn serialized_values(self: @M) -> Span<felt252>;
/// Constructs a model from the given keys and values.
fn from_serialized(ref keys: Span<felt252>, ref values: Span<felt252>) -> Option<M>;
fn from_serialized(keys: Span<felt252>, values: Span<felt252>) -> Option<M>;
/// Returns the name of the model. (TODO: internalizing the name_hash could reduce poseidon
/// costs).
fn name() -> ByteArray;
Expand Down Expand Up @@ -85,7 +85,7 @@ pub impl ModelImpl<M, +ModelParser<M>, +ModelDefinition<M>, +Serde<M>> of Model<
ModelParser::<M>::serialize_values(self)
}

fn from_serialized(ref keys: Span<felt252>, ref values: Span<felt252>) -> Option<M> {
fn from_serialized(keys: Span<felt252>, values: Span<felt252>) -> Option<M> {
let mut serialized: Array<felt252> = keys.into();
serialized.append_span(values);
let mut span = serialized.span();
Expand Down
9 changes: 3 additions & 6 deletions crates/dojo/core/src/model/model_value.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub trait ModelValue<V> {
/// that is not a key.
fn serialized_values(self: @V) -> Span<felt252>;
/// Constructs a model value from its identifier and values.
fn from_serialized(entity_id: felt252, ref values: Span<felt252>) -> Option<V>;
fn from_serialized(values: Span<felt252>) -> Option<V>;
/// Returns the name of the model value type.
fn name() -> ByteArray;
/// Returns the layout of the model value type.
Expand All @@ -31,11 +31,8 @@ pub impl ModelValueImpl<V, +Serde<V>, +ModelDefinition<V>, +ModelValueParser<V>>
ModelValueParser::<V>::serialize_values(self)
}

fn from_serialized(entity_id: felt252, ref values: Span<felt252>) -> Option<V> {
let mut serialized: Array<felt252> = array![];
serialized.append_span(values);
let mut span = serialized.span();
Serde::<V>::deserialize(ref span)
fn from_serialized(mut values: Span<felt252>) -> Option<V> {
Serde::<V>::deserialize(ref values)
}

fn name() -> ByteArray {
Expand Down
51 changes: 16 additions & 35 deletions crates/dojo/core/src/world/storage.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ pub impl ModelStorageWorldStorageImpl<M, +Model<M>, +Drop<M>> of ModelStorage<Wo
let mut values = IWorldDispatcherTrait::entity(
*self.dispatcher,
Model::<M>::selector(*self.namespace_hash),
ModelIndex::Keys(keys),
ModelIndex::Id(entity_id_from_serialized_keys(keys)),
Model::<M>::layout()
);
match Model::<M>::from_serialized(ref keys, ref values) {
match Model::<M>::from_serialized(keys, values) {
Option::Some(model) => model,
Option::None => {
panic!(
Expand All @@ -85,9 +85,11 @@ pub impl ModelStorageWorldStorageImpl<M, +Model<M>, +Drop<M>> of ModelStorage<Wo

fn read_models<K, +Drop<K>, +Serde<K>>(self: @WorldStorage, keys: Span<K>) -> Array<M> {
let mut indexes: Array<ModelIndex> = array![];

let mut serialized_keys: Array<Span<felt252>> = array![];
for k in keys {
indexes.append(ModelIndex::Keys(serialize_inline::<K>(k)));
let sk = serialize_inline::<K>(k);
serialized_keys.append(sk);
indexes.append(ModelIndex::Id(entity_id_from_serialized_keys(sk)));
};

let all_values = IWorldDispatcherTrait::entities(
Expand All @@ -99,16 +101,9 @@ pub impl ModelStorageWorldStorageImpl<M, +Model<M>, +Drop<M>> of ModelStorage<Wo

let mut models: Array<M> = array![];

let mut i = 0;
loop {
if i >= indexes.len() {
break;
}

let mut mk = serialize_inline::<K>(keys[i]);
let mut mv = *all_values[i];

match Model::<M>::from_serialized(ref mk, ref mv) {
let (mut i, len) = (0, indexes.len());
while i < len {
match Model::<M>::from_serialized(*serialized_keys[i], *all_values[i]) {
Option::Some(model) => models.append(model),
Option::None => {
panic!(
Expand All @@ -119,7 +114,6 @@ pub impl ModelStorageWorldStorageImpl<M, +Model<M>, +Drop<M>> of ModelStorage<Wo

i += 1;
};

models
}

Expand Down Expand Up @@ -154,7 +148,7 @@ pub impl ModelStorageWorldStorageImpl<M, +Model<M>, +Drop<M>> of ModelStorage<Wo
IWorldDispatcherTrait::delete_entity(
self.dispatcher,
Model::<M>::selector(self.namespace_hash),
ModelIndex::Keys(Model::<M>::serialized_keys(model)),
ModelIndex::Id(Model::<M>::entity_id(model)),
Model::<M>::layout()
);
}
Expand Down Expand Up @@ -239,7 +233,7 @@ impl ModelValueStorageWorldStorageImpl<
ModelIndex::Id(entity_id),
ModelValue::<V>::layout()
);
match ModelValue::<V>::from_serialized(entity_id, ref values) {
match ModelValue::<V>::from_serialized(values) {
Option::Some(entity) => entity,
Option::None => {
panic!(
Expand All @@ -265,36 +259,23 @@ impl ModelValueStorageWorldStorageImpl<
for id in entity_ids {
indexes.append(ModelIndex::Id(*id));
};

let mut all_values = IWorldDispatcherTrait::entities(
let mut values = array![];
for v in IWorldDispatcherTrait::entities(
*self.dispatcher,
ModelValue::<V>::selector(*self.namespace_hash),
indexes.span(),
ModelValue::<V>::layout()
);

let mut values: Array<V> = array![];
let mut i = 0;
loop {
if i >= indexes.len() {
break;
}

let entity_id = *entity_ids[i];
let mut v = *all_values[i];

match ModelValue::<V>::from_serialized(entity_id, ref v) {
) {
let mut v = *v;
match ModelValue::<V>::from_serialized(v) {
Option::Some(value) => values.append(value),
Option::None => {
panic!(
"Value: deserialization failed. Ensure the length of the keys tuple is matching the number of #[key] fields in the model struct."
)
}
}

i += 1;
};

values
}

Expand Down
2 changes: 1 addition & 1 deletion crates/dojo/core/src/world/world_contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ pub mod world {

let mut keys = [resource_selector].span();

match Model::<ResourceMetadata>::from_serialized(ref keys, ref values) {
match Model::<ResourceMetadata>::from_serialized(keys, values) {
Option::Some(x) => x,
Option::None => panic!("Model `ResourceMetadata`: deserialization failed.")
}
Expand Down
Loading