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
fix: made type-metadata.storage.ts much faster #2212
Merged
kamilmysliwiec
merged 7 commits into
nestjs:master
from
roypeled:type-metadata-storage_with_maps
Jul 20, 2022
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fd060a8
fix: made type-metadata.storage.ts much faster
roypeled 316d442
fix: made type-metadata.storage.ts much faster
roypeled 5aaefdb
fix: made type-metadata.storage.ts much faster
roypeled 5b24aae
fix: made type-metadata.storage.ts much faster
roypeled 760f154
fix: made type-metadata.storage.ts much faster
roypeled 1589d6d
fix: made type-metadata.storage.ts much faster
roypeled f60ba1c
fix: made type-metadata.storage.ts much faster
roypeled File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
packages/graphql/lib/schema-builder/collections/array-with-global-cache.collection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export class ArrayWithGlobalCacheCollection<T> { | ||
private readonly array: T[] = []; | ||
|
||
constructor(private globalArray: Array<T>) {} | ||
|
||
getAll() { | ||
return this.array; | ||
} | ||
|
||
push(...items): number { | ||
this.globalArray.push(...items); | ||
return this.array.push(...items); | ||
} | ||
|
||
unshift(...items): number { | ||
this.globalArray.unshift(...items); | ||
return this.array.unshift(...items); | ||
} | ||
|
||
reverse() { | ||
return this.array.reverse(); | ||
} | ||
|
||
reduce<U>( | ||
callbackfn: ( | ||
previousValue: U, | ||
currentValue: T, | ||
currentIndex: number, | ||
array: T[], | ||
) => U, | ||
initialValue: U, | ||
): U { | ||
return this.array.reduce(callbackfn, initialValue); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/graphql/lib/schema-builder/collections/field-directive.collection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { MetadataListByNameCollection } from './metadata-list-by-name.collection'; | ||
import { PropertyDirectiveMetadata } from '../metadata'; | ||
|
||
export class FieldDirectiveCollection extends MetadataListByNameCollection<PropertyDirectiveMetadata> { | ||
sdls = new Set<string>(); | ||
fieldNames = new Set<string>(); | ||
|
||
add(value: PropertyDirectiveMetadata) { | ||
if (this.sdls.has(value.sdl) && this.fieldNames.has(value.fieldName)) { | ||
return; | ||
} | ||
|
||
super.add(value, value.fieldName); | ||
|
||
this.sdls.add(value.sdl); | ||
this.fieldNames.add(value.fieldName); | ||
this.globalArray?.push(value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export * from './metadata.storage.collection.list'; | ||
export * from './field-directive.collection'; | ||
export * from './array-with-global-cache.collection'; | ||
export * from './metada.collection.model.interface'; | ||
export * from './metadata-by-name.collection'; | ||
export * from './metadata-list-by-name.collection'; | ||
export * from './metadata.storage.collection'; |
14 changes: 14 additions & 0 deletions
14
packages/graphql/lib/schema-builder/collections/metada.collection.model.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { ClassMetadata, ResolverClassMetadata } from '../metadata'; | ||
import { ObjectTypeMetadata } from '../metadata/object-type.metadata'; | ||
|
||
export interface MetadataCollectionModelInterface { | ||
argumentType: ClassMetadata[]; | ||
interface: ClassMetadata[]; | ||
inputType: ClassMetadata[]; | ||
objectType: ObjectTypeMetadata[]; | ||
resolver: ResolverClassMetadata[]; | ||
classDirectives: []; | ||
classExtensions: []; | ||
fieldDirectives: []; | ||
fieldExtensions: []; | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/graphql/lib/schema-builder/collections/metadata-by-name.collection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export class MetadataByNameCollection<T> { | ||
protected map = new Map<string, T>(); | ||
protected all: (T extends any[] ? T[number] : T)[] = []; | ||
|
||
getAll() { | ||
return this.all; | ||
} | ||
|
||
getByName(name: string) { | ||
return this.map.get(name); | ||
} | ||
|
||
add(value: T extends any[] ? T[number] : T, name: string) { | ||
if (this.map.has(name)) return; | ||
|
||
this.map.set(name, value); | ||
this.all.push(value); | ||
} | ||
|
||
unshift(value: T extends any[] ? T[number] : T, name: string) { | ||
if (this.map.has(name)) return; | ||
|
||
this.map.set(name, value); | ||
this.all.unshift(value); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
packages/graphql/lib/schema-builder/collections/metadata-list-by-name.collection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { MetadataByNameCollection } from './metadata-by-name.collection'; | ||
|
||
export class MetadataListByNameCollection<T> extends MetadataByNameCollection< | ||
T[] | ||
> { | ||
constructor(protected globalArray: Array<T> = null) { | ||
super(); | ||
} | ||
|
||
getByName(name: string): T[] { | ||
return super.getByName(name) || []; | ||
} | ||
|
||
add(value: T, name: string) { | ||
let arrayResult = super.getByName(name); | ||
if (!arrayResult) { | ||
arrayResult = []; | ||
this.map.set(name, arrayResult); | ||
} | ||
|
||
arrayResult.push(value); | ||
this.all.push(value); | ||
this.globalArray && this.globalArray.push(value); | ||
} | ||
|
||
unshift(value: T, name: string) { | ||
let arrayResult = super.getByName(name); | ||
if (!arrayResult) { | ||
arrayResult = []; | ||
this.map.set(name, arrayResult); | ||
} | ||
|
||
arrayResult.unshift(value); | ||
this.all.push(value); | ||
this.globalArray && this.globalArray.unshift(value); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/graphql/lib/schema-builder/collections/metadata.storage.collection.list.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { MetadataStorageCollection } from './metadata.storage.collection'; | ||
import { MetadataCollectionModelInterface } from './metada.collection.model.interface'; | ||
|
||
export class MetadataStorageCollectionList { | ||
private storageMap = new Map<Function, MetadataStorageCollection>(); | ||
private storageList = new Array<MetadataStorageCollection>(); | ||
|
||
public all: MetadataCollectionModelInterface = { | ||
argumentType: [], | ||
interface: [], | ||
inputType: [], | ||
objectType: [], | ||
resolver: [], | ||
classDirectives: [], | ||
classExtensions: [], | ||
fieldDirectives: [], | ||
fieldExtensions: [], | ||
}; | ||
|
||
get(target: Function) { | ||
let metadata = this.storageMap.get(target); | ||
|
||
if (!metadata) { | ||
metadata = new MetadataStorageCollection(this.all); | ||
this.storageMap.set(target, metadata); | ||
this.storageList.push(metadata); | ||
} | ||
|
||
return metadata; | ||
} | ||
|
||
compile() { | ||
this.reversePredicate((t) => t.classDirectives.getAll()); | ||
this.reversePredicate((t) => t.classExtensions.getAll()); | ||
this.reversePredicate((t) => t.fieldDirectives.getAll()); | ||
this.reversePredicate((t) => t.fieldExtensions.getAll()); | ||
} | ||
|
||
private reversePredicate<V>( | ||
predicate: (t: MetadataStorageCollection) => Array<V>, | ||
) { | ||
this.storageList.forEach((t) => predicate(t).reverse()); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
packages/graphql/lib/schema-builder/collections/metadata.storage.collection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { MetadataByNameCollection } from './metadata-by-name.collection'; | ||
import { | ||
ClassDirectiveMetadata, | ||
ClassExtensionsMetadata, | ||
ClassMetadata, | ||
MethodArgsMetadata, | ||
PropertyExtensionsMetadata, | ||
PropertyMetadata, | ||
ResolverClassMetadata, | ||
} from '../metadata'; | ||
import { MetadataListByNameCollection } from './metadata-list-by-name.collection'; | ||
import { FieldDirectiveCollection } from './field-directive.collection'; | ||
import { ObjectTypeMetadata } from '../metadata/object-type.metadata'; | ||
import { ArrayWithGlobalCacheCollection } from './array-with-global-cache.collection'; | ||
import { MetadataCollectionModelInterface } from './metada.collection.model.interface'; | ||
|
||
export class MetadataStorageCollection { | ||
constructor(private all: MetadataCollectionModelInterface) {} | ||
|
||
fields = new MetadataByNameCollection<PropertyMetadata>(); | ||
params = new MetadataListByNameCollection<MethodArgsMetadata>(); | ||
fieldDirectives = new FieldDirectiveCollection(this.all.fieldDirectives); | ||
fieldExtensions = | ||
new MetadataListByNameCollection<PropertyExtensionsMetadata>( | ||
this.all.fieldExtensions, | ||
); | ||
classDirectives = new ArrayWithGlobalCacheCollection<ClassDirectiveMetadata>( | ||
this.all.classDirectives, | ||
); | ||
classExtensions = new ArrayWithGlobalCacheCollection<ClassExtensionsMetadata>( | ||
this.all.classExtensions, | ||
); | ||
|
||
set argumentType(val: ClassMetadata) { | ||
this._argumentType = val; | ||
this.all.argumentType.push(val); | ||
} | ||
|
||
get argumentType() { | ||
return this._argumentType; | ||
} | ||
|
||
set interface(val: ClassMetadata) { | ||
this._interface = val; | ||
this.all.interface.push(val); | ||
} | ||
|
||
get interface() { | ||
return this._interface; | ||
} | ||
|
||
set inputType(val: ClassMetadata) { | ||
this._inputType = val; | ||
this.all.inputType.push(val); | ||
} | ||
|
||
get inputType() { | ||
return this._inputType; | ||
} | ||
|
||
set objectType(val: ObjectTypeMetadata) { | ||
this._objectType = val; | ||
this.all.objectType.push(val); | ||
} | ||
|
||
get objectType() { | ||
return this._objectType; | ||
} | ||
|
||
set resolver(val: ResolverClassMetadata) { | ||
this._resolver = val; | ||
this.all.resolver.push(val); | ||
} | ||
|
||
get resolver() { | ||
return this._resolver; | ||
} | ||
|
||
_argumentType: ClassMetadata; | ||
_interface: ClassMetadata; | ||
_inputType: ClassMetadata; | ||
_objectType: ObjectTypeMetadata; | ||
_resolver: ResolverClassMetadata; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: missing blank lines between getters and setters