Skip to content
Draft
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
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-05-24 - Serde Deserialization Overhead
**Learning:** `serde_json::from_value` and `serde_yaml::from_value` take ownership of the `Value`. Calling them with `value.clone()` results in an expensive deep clone of the entire JSON/YAML AST tree (which can be huge for OpenAPI specs).
**Action:** Always prefer `T::deserialize(&value)` which directly delegates to the reference's `Deserializer` implementation and avoids deep cloning. Only use `from_value(value)` if you are consuming the very root `Value` object and no longer need it.
40 changes: 21 additions & 19 deletions xdk-openapi/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::context::OpenApiContextGuard;
use crate::core::OpenApi;
use crate::error::OpenApiError;
use crate::error::Result;
use serde::Deserialize;
use serde_json::Value as JsonValue;
use serde_yaml::Value as YamlValue;

Expand Down Expand Up @@ -56,9 +57,8 @@ pub fn parse_yaml(yaml: &str) -> Result<OpenApi> {
if let Some(schemas) = components.get("schemas").and_then(|s| s.as_mapping()) {
for (name, schema_val) in schemas {
if let Some(name_str) = name.as_str() {
match serde_yaml::from_value::<crate::components::Schema>(
schema_val.clone(),
) {
// Performance: Use `deserialize` on `&Value` to avoid deep cloning the AST tree
match crate::components::Schema::deserialize(schema_val) {
Ok(schema) => ctx.add_schema(name_str.to_string(), schema),
Err(e) => {
eprintln!("Warning: Failed to parse schema {name_str}: {e}")
Expand All @@ -70,9 +70,8 @@ pub fn parse_yaml(yaml: &str) -> Result<OpenApi> {
if let Some(parameters) = components.get("parameters").and_then(|p| p.as_mapping()) {
for (name, param_val) in parameters {
if let Some(name_str) = name.as_str() {
match serde_yaml::from_value::<crate::components::Parameter>(
param_val.clone(),
) {
// Performance: Use `deserialize` on `&Value` to avoid deep cloning the AST tree
match crate::components::Parameter::deserialize(param_val) {
Ok(param) => ctx.add_parameter(name_str.to_string(), param),
Err(e) => {
eprintln!("Warning: Failed to parse parameter {name_str}: {e}")
Expand All @@ -84,7 +83,8 @@ pub fn parse_yaml(yaml: &str) -> Result<OpenApi> {
if let Some(responses) = components.get("responses").and_then(|r| r.as_mapping()) {
for (name, resp_val) in responses {
if let Some(name_str) = name.as_str() {
match serde_yaml::from_value::<crate::core::Response>(resp_val.clone()) {
// Performance: Use `deserialize` on `&Value` to avoid deep cloning the AST tree
match crate::core::Response::deserialize(resp_val) {
Ok(resp) => ctx.add_response(name_str.to_string(), resp),
Err(e) => {
eprintln!("Warning: Failed to parse response {name_str}: {e}")
Expand All @@ -99,7 +99,8 @@ pub fn parse_yaml(yaml: &str) -> Result<OpenApi> {
{
for (name, rb_val) in request_bodies {
if let Some(name_str) = name.as_str() {
match serde_yaml::from_value::<crate::core::RequestBody>(rb_val.clone()) {
// Performance: Use `deserialize` on `&Value` to avoid deep cloning the AST tree
match crate::core::RequestBody::deserialize(rb_val) {
Ok(rb) => ctx.add_request_body(name_str.to_string(), rb),
Err(e) => {
eprintln!("Warning: Failed to parse requestBody {name_str}: {e}")
Expand All @@ -114,9 +115,8 @@ pub fn parse_yaml(yaml: &str) -> Result<OpenApi> {
{
for (name, ss_val) in security_schemes {
if let Some(name_str) = name.as_str() {
match serde_yaml::from_value::<crate::components::SecurityScheme>(
ss_val.clone(),
) {
// Performance: Use `deserialize` on `&Value` to avoid deep cloning the AST tree
match crate::components::SecurityScheme::deserialize(ss_val) {
Ok(ss) => ctx.add_security_scheme(name_str.to_string(), ss),
Err(e) => {
eprintln!("Warning: Failed to parse securityScheme {name_str}: {e}")
Expand Down Expand Up @@ -211,24 +211,26 @@ pub fn parse_json(json: &str) -> Result<OpenApi> {
OpenApiContextGuard::with_context_mut(|ctx| {
if let Some(schemas) = components.get("schemas").and_then(|s| s.as_object()) {
for (name, schema_val) in schemas {
match serde_json::from_value::<crate::components::Schema>(schema_val.clone()) {
// Performance: Use `deserialize` on `&Value` to avoid deep cloning the AST tree
match crate::components::Schema::deserialize(schema_val) {
Ok(schema) => ctx.add_schema(name.clone(), schema),
Err(e) => eprintln!("Warning: Failed to parse schema {name}: {e}"),
}
}
}
if let Some(parameters) = components.get("parameters").and_then(|p| p.as_object()) {
for (name, param_val) in parameters {
match serde_json::from_value::<crate::components::Parameter>(param_val.clone())
{
// Performance: Use `deserialize` on `&Value` to avoid deep cloning the AST tree
match crate::components::Parameter::deserialize(param_val) {
Ok(param) => ctx.add_parameter(name.clone(), param),
Err(e) => eprintln!("Warning: Failed to parse parameter {name}: {e}"),
}
}
}
if let Some(responses) = components.get("responses").and_then(|r| r.as_object()) {
for (name, resp_val) in responses {
match serde_json::from_value::<crate::core::Response>(resp_val.clone()) {
// Performance: Use `deserialize` on `&Value` to avoid deep cloning the AST tree
match crate::core::Response::deserialize(resp_val) {
Ok(resp) => ctx.add_response(name.clone(), resp),
Err(e) => eprintln!("Warning: Failed to parse response {name}: {e}"),
}
Expand All @@ -239,7 +241,8 @@ pub fn parse_json(json: &str) -> Result<OpenApi> {
.and_then(|rb| rb.as_object())
{
for (name, rb_val) in request_bodies {
match serde_json::from_value::<crate::core::RequestBody>(rb_val.clone()) {
// Performance: Use `deserialize` on `&Value` to avoid deep cloning the AST tree
match crate::core::RequestBody::deserialize(rb_val) {
Ok(rb) => ctx.add_request_body(name.clone(), rb),
Err(e) => eprintln!("Warning: Failed to parse requestBody {name}: {e}"),
}
Expand All @@ -250,9 +253,8 @@ pub fn parse_json(json: &str) -> Result<OpenApi> {
.and_then(|ss| ss.as_object())
{
for (name, ss_val) in security_schemes {
match serde_json::from_value::<crate::components::SecurityScheme>(
ss_val.clone(),
) {
// Performance: Use `deserialize` on `&Value` to avoid deep cloning the AST tree
match crate::components::SecurityScheme::deserialize(ss_val) {
Ok(ss) => ctx.add_security_scheme(name.clone(), ss),
Err(e) => {
eprintln!("Warning: Failed to parse securityScheme {name}: {e}")
Expand Down
Loading