ftrackhq / ftrack-ts-schema-generator

Gets the schema from an ftrack instance and generates typescript interfaces for all the entity types in the instance.
Apache License 2.0
2 stars 1 forks source link

Performance regression after the latest PR I made #44

Closed ffMathy closed 1 month ago

ffMathy commented 2 months ago

After merging #43, performance of TypeScript itself in VS Code has dropped significantly. For our project, we are seeing up to 130 seconds before I get autocomplete suggestions from the TypeScript Language Service.

I first tried removing the strong-typing of the custom attributes again to see if that made it faster. It did not.

Then I tried changing the way the TypedContext union type is defined.

Right now, it's defined as:

export type TypedContext = TypedContextSubtypeMap[TypedContextSubtype];

If I change it to:

export type TypedContext = TypedContextForSubtype<TypedContextSubtype>;

Then the performance problems go away 100%, and I get instant feedback. However, then I also lose the type discrimination capability.

Example:

const x: TypedContext = null!;
if(x.__entity_type__ === "Shot") {
   // with the slow approach, here, x is correctly of type Shot.
   // with the fast approach, here, x is of type TypedContext
}

Can we think of a type that isn't slow for TypeScript to handle?

We have 150+ custom attributes across around maybe ~30 object types. I don't feel like the object type count is a lot. The custom attributes is a bit high, but then again, that doesn't seem to be the cause of the performance issue either way.

gismya commented 2 months ago

What happens if we type TypedContext as a union type of all the different types in TypedContextSubtypeMap instead?

gismya commented 2 months ago

Are the performance issues only for the intellisense, or does it also happen when you run tsc? I'm failing to reproduce the issues in our code base.

ffMathy commented 2 months ago

What happens if we type TypedContext as a union type of all the different types in TypedContextSubtypeMap instead?

I tried this too today. Didn't work. If I then removed some of the unions (maybe 5 out of 30 or so), the performance got a little better.

Are the performance issues only for the intellisense, or does it also happen when you run tsc? I'm failing to reproduce the issues in our code base.

It's for both apparently. The weird thing is that if I just move my types to the TS Playground, I also can't get it to get slow. So I think it is because we are referencing TypedContext in a lot of places in our codebase.

gismya commented 2 months ago

Performance tracing could give some further insight into what the specific performance bottlenecks are: https://github.com/microsoft/TypeScript/wiki/Performance#performance-tracing

ffMathy commented 2 months ago

Yeah I looked into that too. Will have to use my spare time for that though, as we are very busy these days. Might take some time, but let's leave the issue open.

gismya commented 2 months ago

If you manage to create a reproduction example I can help, but as of now it's not presenting in any of our repos.

ffMathy commented 1 month ago

This can be closed. Ran a full trace (allocated 24 gigabytes of RAM and ran for 6 hours - that's why I thought it was stuck), and it revealed that the issue was actually somewhere else in our own custom types, although the issue would present itself in very certain situations that would lead me to believe it was related to the given PR.

The given PR definitely made it "worse", but it's not bad at all, and was just when used in combination with our weird custom types.

gismya commented 1 month ago

That's a massive trace!

Thank you for the update.