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

Optimize TryGetableFromJson #1249

Merged
merged 1 commit into from
Dec 1, 2022
Merged

Optimize TryGetableFromJson #1249

merged 1 commit into from
Dec 1, 2022

Conversation

billy1624
Copy link
Member

PR Info

Changes

  • Optimize the default implementation of TryGetableFromJson::try_get_from_json(): deserializing into Self directly without the need of a intermediate serde_json::Value

@baoyachi
Copy link
Contributor

@billy1624 I suggest the code here, add some unit tests to robust this logic

@billy1624
Copy link
Member Author

We already have the test cases in place, this PR is a performance improvement with no breaking changes

use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "json_struct")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub json: Json,
pub json_value: KeyValue,
pub json_value_opt: Option<KeyValue>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, FromJsonQueryResult)]
pub struct KeyValue {
pub id: i32,
pub name: String,
pub price: f32,
pub notes: Option<String>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

pub mod common;
pub use common::{features::*, setup::*, TestContext};
use pretty_assertions::assert_eq;
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection};
use serde_json::json;
#[sea_orm_macros::test]
#[cfg(any(
feature = "sqlx-mysql",
feature = "sqlx-sqlite",
feature = "sqlx-postgres"
))]
async fn main() -> Result<(), DbErr> {
let ctx = TestContext::new("json_struct_tests").await;
create_tables(&ctx.db).await?;
insert_json_struct_1(&ctx.db).await?;
insert_json_struct_2(&ctx.db).await?;
ctx.delete().await;
Ok(())
}
pub async fn insert_json_struct_1(db: &DatabaseConnection) -> Result<(), DbErr> {
use json_struct::*;
let model = Model {
id: 1,
json: json!({
"id": 1,
"name": "apple",
"price": 12.01,
"notes": "hand picked, organic",
}),
json_value: KeyValue {
id: 1,
name: "apple".into(),
price: 12.01,
notes: Some("hand picked, organic".into()),
},
json_value_opt: Some(KeyValue {
id: 1,
name: "apple".into(),
price: 12.01,
notes: Some("hand picked, organic".into()),
}),
};
let result = model.clone().into_active_model().insert(db).await?;
assert_eq!(result, model);
assert_eq!(
Entity::find()
.filter(Column::Id.eq(model.id))
.one(db)
.await?,
Some(model)
);
Ok(())
}
pub async fn insert_json_struct_2(db: &DatabaseConnection) -> Result<(), DbErr> {
use json_struct::*;
let model = Model {
id: 2,
json: json!({
"id": 2,
"name": "orange",
"price": 10.93,
"notes": "sweet & juicy",
}),
json_value: KeyValue {
id: 1,
name: "orange".into(),
price: 10.93,
notes: None,
},
json_value_opt: None,
};
let result = model.clone().into_active_model().insert(db).await?;
assert_eq!(result, model);
assert_eq!(
Entity::find()
.filter(Column::Id.eq(model.id))
.one(db)
.await?,
Some(model)
);
Ok(())
}

@baoyachi
Copy link
Contributor

baoyachi commented Nov 24, 2022

👍 @billy1624

How much performance will be improved? Is there a numerical result comparison?

@billy1624
Copy link
Member Author

I don't have a precise benchmark

@billy1624 billy1624 merged commit 3de0078 into master Dec 1, 2022
@billy1624 billy1624 deleted the optimize-try-get-from-json branch December 1, 2022 05:23
tyt2y3 pushed a commit that referenced this pull request Dec 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Use serde_json::Value is an intermediate state, and the performance of sea-orm will be lost
3 participants