-
Notifications
You must be signed in to change notification settings - Fork 24
Description
Just tried this use this project on my huge swagger file with ~25,000 types (huge thanks btw!) and while it worked, it took 2.5 hours to process my model.
I looked into it and it seems to be a simple case of an N^M
algorithm here when checking for invalid references.
typebox-codegen/src/model/model-to-arktype.ts
Lines 237 to 247 in 35000c1
function GenerateType(schema: Types.TSchema, references: Types.TSchema[]) { | |
const buffer: string[] = [] | |
for (const reference of references) { | |
if (reference.$id === undefined) return UnsupportedType(schema) | |
reference_map.set(reference.$id, reference) | |
} | |
const type = Collect(schema) | |
buffer.push(`${schema.$id || `T`}: ${type}`) | |
if (schema.$id) emitted_types.add(schema.$id) | |
return buffer.join('\n') | |
} |
typebox-codegen/src/model/model-to-arktype.ts
Lines 255 to 257 in 35000c1
for (const type of model.types.filter((type) => Types.TypeGuard.IsSchema(type))) { | |
buffer.push(`${GenerateType(type, model.types)},`) | |
} |
You're checking every reference for every type, but references don't need to be re-checked every time since they in the same scope.
Changing those lines to below reduced my runtime to 3 seconds.
function GenerateType(schema: Types.TSchema) {
const buffer: string[] = []
if (!reference_map.has(schema.$id)) return UnsupportedType(schema)
const type = Collect(schema)
buffer.push(`${schema.$id || `T`}: ${type}`)
if (schema.$id) emitted_types.add(schema.$id)
return buffer.join('\n')
}
for (const reference of references) {
reference_map.set(reference.$id, reference)
}
for (const type of model.types.filter((type) => Types.TypeGuard.IsSchema(type))) {
buffer.push(`${GenerateType(type)},`)
}
This pre-checks all references, then we just do a lookup in the generation to see if it's invalid instead of re-checking everything.
Let me know if you want a PR!