Skip to content

perf: compute identity for each AST node (fix #422) #446

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

Closed
wants to merge 1 commit into from
Closed
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
44 changes: 25 additions & 19 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
TIntersection,
TNamedInterface,
TUnion,
T_UNKNOWN
T_UNKNOWN,
TInterfaceParam
} from './types/AST'
import {log, toSafeString} from './utils'

Expand Down Expand Up @@ -156,7 +157,7 @@ function declareNamedTypes(ast: AST, options: Options, rootASTName: string, proc
return type
}

function generateType(ast: AST, options: Options): string {
export function generateType(ast: AST, options: Options): string {
const type = generateRawType(ast, options)

if (options.strictIndexSignatures && ast.keyName === '[k: string]') {
Expand All @@ -176,15 +177,16 @@ function generateRawType(ast: AST, options: Options): string {
switch (ast.type) {
case 'ANY':
return 'any'
case 'ARRAY':
return (() => {
const type = generateType(ast.params, options)
return type.endsWith('"') ? '(' + type + ')[]' : type + '[]'
})()
case 'ARRAY': {
const type = generateType(ast.params, options)
return type.endsWith('"') ? '(' + type + ')[]' : type + '[]'
}
case 'BOOLEAN':
return 'boolean'
case 'INTERFACE':
return generateInterface(ast, options)
case 'INTERFACE_PARAM':
return '' // handled by INTERFACE. TODO: thro
case 'INTERSECTION':
return generateSetOperation(ast, options)
case 'LITERAL':
Expand All @@ -210,13 +212,15 @@ function generateRawType(ast: AST, options: Options): string {
// this is a valid state, and JSONSchema doesn't care about the item type
if (maxItems < 0) {
// no max items and no spread param, so just spread any
// @ts-ignore
spreadParam = options.unknownAny ? T_UNKNOWN : T_ANY
}
}
if (maxItems > astParams.length && ast.spreadParam === undefined) {
// this is a valid state, and JSONSchema doesn't care about the item type
// fill the tuple with any elements
for (let i = astParams.length; i < maxItems; i += 1) {
// @ts-ignore
astParams.push(options.unknownAny ? T_UNKNOWN : T_ANY)
}
}
Expand Down Expand Up @@ -301,24 +305,24 @@ function generateInterface(ast: TInterface, options: Options): string {
'\n' +
ast.params
.filter(_ => !_.isPatternProperty && !_.isUnreachableDefinition)
.map(
({isRequired, keyName, ast}) =>
[isRequired, keyName, ast, generateType(ast, options)] as [boolean, string, AST, string]
)
.map(
([isRequired, keyName, ast, type]) =>
(hasComment(ast) && !ast.standaloneName ? generateComment(ast.comment) + '\n' : '') +
escapeKeyName(keyName) +
(isRequired ? '' : '?') +
': ' +
(hasStandaloneName(ast) ? toSafeString(type) : type)
)
.map(_ => generateInterfaceParam(_, options))
.join('\n') +
'\n' +
'}'
)
}

export function generateInterfaceParam({isRequired, keyName, ast}: TInterfaceParam, options: Options): string {
const type = generateType(ast, options)
return (
(hasComment(ast) && !ast.standaloneName ? generateComment(ast.comment) + '\n' : '') +
escapeKeyName(keyName) +
(isRequired ? '' : '?') +
': ' +
(hasStandaloneName(ast) ? toSafeString(type) : type)
)
}

function generateComment(comment: string): string {
return ['/**', ...comment.split('\n').map(_ => ' * ' + _), ' */'].join('\n')
}
Expand Down Expand Up @@ -349,6 +353,7 @@ function generateStandaloneInterface(ast: TNamedInterface, options: Options): st

function generateStandaloneType(ast: ASTWithStandaloneName, options: Options): string {
return (
// @ts-ignore
(hasComment(ast) ? generateComment(ast.comment) + '\n' : '') +
`export type ${toSafeString(ast.standaloneName)} = ${generateType(
omit<AST>(ast, 'standaloneName') as AST /* TODO */,
Expand All @@ -368,5 +373,6 @@ function escapeKeyName(keyName: string): string {
}

function getSuperTypesAndParams(ast: TInterface): AST[] {
// @ts-ignore
return ast.params.map(param => param.ast).concat(ast.superTypes)
}
37 changes: 37 additions & 0 deletions src/hasher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {Options} from '.'
import {generateInterfaceParam, generateType} from './generator'
import {AST} from './types/AST'

export function hash(ast: AST, options: Options): string {
switch (ast.type) {
case 'ANY':
return 'any'
case 'BOOLEAN':
return 'boolean'
case 'INTERSECTION':
case 'UNION':
return `(${ast.params.map(_ => _.hashCode).join(', ')})`
case 'CUSTOM_TYPE':
return ast.params
case 'ENUM':
return `(${ast.params.map(_ => `${_.keyName}:${_.ast.hashCode}`).join(', ')})`
case 'INTERFACE':
return `(${generateType(ast, options)})`
case 'INTERFACE_PARAM':
return generateInterfaceParam(ast, options)
case 'ARRAY':
return hash(ast.params, options)
case 'NUMBER':
return 'number'
case 'NULL':
return 'null'
case 'OBJECT':
return 'object'
case 'LITERAL':
case 'STRING':
case 'UNKNOWN':
case 'REFERENCE':
case 'TUPLE':
return generateType(ast, options)
}
}
20 changes: 10 additions & 10 deletions src/optimizer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import stringify = require('json-stringify-safe')
import {uniqBy} from 'lodash'
import {AST, T_ANY, T_UNKNOWN} from './types/AST'
import {log} from './utils'
Expand Down Expand Up @@ -31,16 +30,17 @@ export function optimize(ast: AST, processed = new Set<AST>()): AST {
return T_UNKNOWN
}

// [A (named), A] -> [A (named)]
if (
ast.params.every(_ => _.hashCode === ast.params[0].hashCode) &&
ast.params.some(_ => _.standaloneName !== undefined)
) {
log('cyan', 'optimizer', '[A, B, B] -> [A, B]', ast)
ast.params = ast.params.filter(_ => _.standaloneName !== undefined)
}

// [A, B, B] -> [A, B]
const shouldTakeStandaloneNameIntoAccount = ast.params.filter(_ => _.standaloneName).length > 1
const params = uniqBy(
ast.params,
_ => `
${_.type}-
${shouldTakeStandaloneNameIntoAccount ? _.standaloneName : ''}-
${stringify((_ as any).params)}
`
)
const params = uniqBy(ast.params, _ => `${_.standaloneName}:${_.hashCode}`)
if (params.length !== ast.params.length) {
log('cyan', 'optimizer', '[A, B, B] -> [A, B]', ast)
ast.params = params
Expand Down
Loading