Visitar URL original
JS: faster TypeScript extraction by erik-krogh · Pull Request #15271 · github/codeql · GitHub
The Wayback Machine - https://web.archive.org/web/20240111011449/https://github.com/github/codeql/pull/15271
Skip to content
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

Merged
merged 2 commits into from Jan 10, 2024
Merged

JS: faster TypeScript extraction #15271

merged 2 commits into from Jan 10, 2024

Conversation

erik-krogh
Copy link
Contributor

@erik-krogh erik-krogh commented Jan 9, 2024 •

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 const type is speedup massively by this change.

Evaluations (default, nightly) were very uneventful.

@github-actions github-actions bot added the JS label Jan 9, 2024
@erik-krogh erik-krogh marked this pull request as ready for review January 9, 2024 21:14
@erik-krogh erik-krogh requested a review from a team as a code owner January 9, 2024 21:14
Copy link
Contributor

@asgerf asgerf left a 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.

Comment on lines 434 to 436
if (typeof cached !== "undefined") {
let value = cached[unfoldAlias ? 1 : 0];
if (typeof value !== "undefined") return value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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?

Copy link
Contributor Author

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) => {
Copy link
Contributor

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]>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);
Copy link
Contributor

@asgerf asgerf Jan 10, 2024 •

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.

Copy link
Contributor Author

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?

@erik-krogh erik-krogh merged commit 51fe477 into github:main Jan 10, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants