Skip to content

Avoid double structuredClone in generate-schema-types compile path #1034

@l2ysho

Description

@l2ysho

Every schema compilation in actor generate-schema-types performs two full deep clones:

  1. clearAllRequired(schema) / makePropertiesRequired(schema) — each does structuredClone internally
  2. stripTitles(schemaToCompile) — does another structuredClone at the top of the function

All three functions guarantee immutability (tested), so the second clone in stripTitles is redundant when the caller already holds a disposable copy from step 1.

For the current schemas this has zero practical impact, but it is wasteful and worth cleaning up.

Options

Plan A — { mutate: true } flag on stripTitles

Minimal API change. When the caller already has a disposable clone, it opts out of the internal clone:

export function stripTitles(schema, { mutate = false } = {}) {
    const result = mutate ? schema : structuredClone(schema);
    // all internal recursive calls pass { mutate: true }
    ...
}

// call site — schemaToCompile is already a clone, skip second structuredClone
stripTitles(schemaToCompile, { mutate: true })

Plan B — combined prepareForCompilation(schema, allOptional) helper

Single clone, applies both the required-field transformation and title-stripping in one pass. Requires extracting private mutating helpers for each transform. Call sites become a single function call but the internal refactor is larger.

Reference

Introduced in #1000.

Metadata

Metadata

Assignees

Labels

t-toolingIssues with this label are in the ownership of the tooling team.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions