Skip to content

N^M algorithm in ModelToArkType + Fix #56

@JonWolfeDrata

Description

@JonWolfeDrata

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.

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')
}

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions