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

simplify the code generated for unit, externally tagged and internally tagged enums #266

Closed

Conversation

icewind1991
Copy link
Contributor

@icewind1991 icewind1991 commented Jan 31, 2024

Partial solution for #246

  • replace the manual construction of Schema::Object with a helper method
  • replace metadata setting with a function call per-metadata

Taking the enum from https://github.com/adamchalmers/rust-problems/blob/achalmers/schemars-llvm-lines/src/lib.rs and running cargo rustc -- -Z unpretty=mir | wc -l it goes from 165725 lines of MIR to 4871 (removing the serde derives from the linked code)

The per-variant generate code goes from:

{
    let schema = schemars::schema::Schema::Object(schemars::schema::SchemaObject {
        instance_type: Some(
            schemars::schema::InstanceType::String.into(),
        ),
        enum_values: Some(
            <[_]>::into_vec(
                #[rustc_box]
                ::alloc::boxed::Box::new(["Af".into()]),
            ),
        ),
        ..Default::default()
    });
    schemars::_private::apply_metadata(
        schema,
        schemars::schema::Metadata {
            description: Some("Afghanistan".to_owned()),
            ..Default::default()
        },
    )
},

to:

{
    let schema = schemars::schema::Schema::Object(
        schemars::schema::SchemaObject::new_unit_enum("Af"),
    );
    schema.with_description("Afghanistan")
},

I still have a crate that takes forever to generate the schema for so there are still plenty of room for improvement.

@icewind1991 icewind1991 changed the title simplify the code generated for unit enums simplify the code generated for unit, externally tagged and internally tagged enums Jan 31, 2024
@icewind1991
Copy link
Contributor Author

icewind1991 commented Jan 31, 2024

  • Added optimizations for externally and internally tagged enums

From:

schemars::schema::Schema::Object(schemars::schema::SchemaObject {
    instance_type: Some(
        schemars::schema::InstanceType::Object.into(),
    ),
    object: Some(
        Box::new(schemars::schema::ObjectValidation {
            properties: {
                let mut props = schemars::Map::new();
                props
                    .insert(
                        "type".to_owned(),
                        schemars::schema::Schema::Object(schemars::schema::SchemaObject {
                            instance_type: Some(
                                schemars::schema::InstanceType::String.into(),
                            ),
                            enum_values: Some(
                                <[_]>::into_vec(
                                    #[rustc_box]
                                    ::alloc::boxed::Box::new(["ServerSpawn".into()]),
                                ),
                            ),
                            ..Default::default()
                        }),
                    );
                props
            },
            required: {
                let mut required = schemars::Set::new();
                required.insert("type".to_owned());
                required
            },
            ..Default::default()
        }),
    ),
    ..Default::default()
})
.flatten(
    <Box<
        ServerSpawnEvent,
    > as schemars::JsonSchema>::json_schema(gen),
),

for a single field in an internally tagged enum

to:

schemars::schema::Schema::new_internally_tagged_enum(
    "type",
    "ServerSpawn",
    false,
)
.flatten(
    <Box<
        ServerSpawnEvent,
    > as schemars::JsonSchema>::json_schema(gen),
),
  • Added optimization for property validations for structs

from:

{
    object_validation
        .properties
        .insert("hostname".to_owned(), gen.subschema_for::<String>());
    if !<String as schemars::JsonSchema>::_schemars_private_is_option() {
        object_validation.required.insert("hostname".to_owned());
    }
}

per (non-required, non-default) field

to:

 {
    object_validation
        .insert_property::<
            String,
        >("address", false, false, gen.subschema_for::<String>());
}

Given https://gist.github.com/icewind1991/4908eaf88173559c3623b74bfef45640 this goes from 829858 lines of MIT to 70405.

For my full crate the compile time with schema goes from "giving up and killing the build after 3 hours" to 5 seconds.

@GREsau
Copy link
Owner

GREsau commented May 6, 2024

Thanks for the PR! I'd rather not expand the public API with the new functions, so I moved them into the _private module and merged it in #286

@GREsau GREsau closed this May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants