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
JS: faster TypeScript extraction #15271
Conversation
…g" for a type every time
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.
A few comments but otherwise looks great! I'm very happy to see these extraction-time issues dealt with.
| if (typeof cached !== "undefined") { | ||
| let value = cached[unfoldAlias ? 1 : 0]; | ||
| if (typeof value !== "undefined") return value; |
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.
| if (typeof cached !== "undefined") { | |
| let value = cached[unfoldAlias ? 1 : 0]; | |
| if (typeof value !== "undefined") return value; | |
| if (cached !== undefined) { | |
| let value = cached[unfoldAlias ? 1 : 0]; | |
| if (value !== undefined) return value; |
In modern JS engines I don't think there's a reason to write undefined-checks using typeof?
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.
You're right. There is not.
I still tend to use it, just an old habbit.
| let value = cached[unfoldAlias ? 1 : 0]; | ||
| if (typeof value !== "undefined") return value; | ||
| } | ||
| const setAndReturn = (v: number | null) => { |
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.
Instrumenting every return seems error-prone. I'd rather wrap getId with a function that does the cache check and then towards to the underlying getId.
public getId(type: ts.Type, unfoldAlias: boolean): number | null {
let cached = this.idCache.get(type);
if (cached !== undefined) {
let value = cached[unfoldAlias ? 1 : 0];
if (value !== undefined) return value;
}
let result = this.getIdRaw(type, unfoldAlias);
cached[unfoldAlias ? 1 : 0] = result;
return result;
}
private getIdRaw(type: ts.Type, unfoldAlias: boolean): number | null {
...
}| @@ -421,17 +421,32 @@ export class TypeTable { | |||
| return id; | |||
| } | |||
|
|
|||
| // type -> [id (not unfolded), id (unfolded)] | |||
| private idCache = new WeakMap<ts.Type, [number | undefined, number | undefined]>(); | |||
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.
| private idCache = new WeakMap<ts.Type, [number | undefined, number | undefined]>(); | |
| /** | |
| * Caches the result of `getId`: `type -> [id (not unfolded), id (unfolded)]`. | |
| * | |
| * A value of `undefined` means the value is not yet computed, | |
| * and `number | null` corresponds to the return value of `getId`. | |
| */ | |
| private idCache = new WeakMap<ts.Type, [number | null | undefined, number | null | undefined]>(); |
| if (this.typeRecursionDepth > 100) { | ||
| // Ignore infinitely nested anonymous types, such as `{x: {x: {x: ... }}}`. | ||
| // Such a type can't be written directly with TypeScript syntax (as it would need to be named), | ||
| // but it can occur rarely as a result of type inference. | ||
| return null; | ||
| return setAndReturn(null); |
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.
This is technically incorrect, as a type might be seen at depth 101 and then we cache the fact that it can't be extracted. Then later the type is seen at a lower depth and could be extracted, but then we immediately give up because of the cached failure. It causes the extraction result to depend on the other in which we visit the types.
I remember trying to cache the result of getId many times and always rediscovering the reason why it doesn't work. Could you leave a comment here explaining the above?
With that said, we really don't get much milage out of these types compared to how expensive they are to extract, and I'd like to move away from extracting types (but still augmenting the call graph). So I'm OK with letting it slide in order to speed up extraction, but we should start talking about disabling type-extraction altogether.
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.
With that said, we really don't get much milage out of these types compared to how expensive they are to extract, and I'd like to move away from extracting types (but still augmenting the call graph). So I'm OK with letting it slide in order to speed up extraction, but we should start talking about disabling type-extraction altogether.
Yeah, we don't get much from types now that we focus on security.
Our recognition of HTML elements is improved a bunch though (for client-side XSS).
So we would loose some results.
Is it something we should ask product about?


by not having to compute the "type-string" for a type every time.
I have a case (internal, see backref) where the extraction speed of a large
consttype is speedup massively by this change.Evaluations (default, nightly) were very uneventful.