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

VSCode and tsc very slow when using external tsplusTypes #252

Closed
patroza opened this issue Dec 27, 2022 · 3 comments · Fixed by #253
Closed

VSCode and tsc very slow when using external tsplusTypes #252

patroza opened this issue Dec 27, 2022 · 3 comments · Fixed by #253

Comments

@patroza
Copy link

patroza commented Dec 27, 2022

Initializing language features in editor, as well as compiling, takes very very long on my projects since using external tsPlusTypes definitions.
I mean I do have a lot of errors atm as im converting, but the process is very painful with such slow load times

Additionally I noticed the code that scans for the tsplusTypes is called multiple times for me during build for example. Not sure if it maybe needs caching. Will see if I can find out why

Perhaps also related: #248

Using these https://github.com/tim-smart/effect-rpc/tree/main/packages/router/vendor by @tim-smart
coming out of https://github.com/tim-smart/tsplus-gen

@patroza
Copy link
Author

patroza commented Dec 27, 2022

I've fixed the performance issue with 3 caches, but the last one seems to have made the most difference.
(initialize caches outside the Parser definition)

1:
const tsPlusTypeCache = {}

      for (const resolvedPath of resolvedPaths) {
        const json = resolvedPath in tsPlusTypeCache ? tsPlusTypeCache[resolvedPath] : (() => { const text = sys.readFile(resolvedPath); if (text) { const json = JSON.parse(text); tsPlusTypeCache[resolvedPath] = json; return json}})()
        if (json) {

2:
const resolvedPathsCache = {}

    if (options.configFilePath) {
      const resolvedPaths = getResolvedPaths(options)
      if (resolvedPaths.length === 0) {
        return;
      }
  function getResolvedPaths(options) {
    if (options.configFilePath in resolvedPathsCache) { return resolvedPathsCache[options.configFilePath] }
    let resolvedPaths = [];
      if (options.tsPlusTypes) {
        for (const path of options.tsPlusTypes) {
          if (pathIsRelative(path)) {
            resolvedPaths.push(resolvePath(options.configFilePath.split("/").slice(0, -1).join("/"), path));
          } else {
            const resolvedModule = resolveModuleName(path, options.configFilePath, options, sys).resolvedModule;
            if (resolvedModule) {
              resolvedPaths.push(resolvedModule.resolvedFileName);
              break;
            }
          }
        }
      }
      const packagePath = removeExtension(options.configFilePath.split("node_modules").slice(-1)[0].substring(1), ".d.ts");
      if (packagePath) {
        let packageName;
        if (packagePath.startsWith("@")) {
          packageName = packagePath.split(directorySeparator).slice(0, 2).join(directorySeparator);
        } else {
          packageName = packagePath.split(directorySeparator).slice(0, 1)[0];
        }
        const resolvedPackageJson = resolvePackageNameToPackageJson(packageName, options.configFilePath, options, sys, void 0);
        if (resolvedPackageJson) {
          const packageJsonText = sys.readFile(resolvePath(resolvedPackageJson.packageDirectory, "package.json"));
          if (packageJsonText) {
            const packageJson = JSON.parse(packageJsonText);
            if (packageJson.tsPlusTypes) {
              for (const path of toArray(packageJson.tsPlusTypes)) {
                resolvedPaths.push(resolvePath(resolvedPackageJson.packageDirectory, path));
              }
            }
          }
        }
        if (packagePath.startsWith("@")) {
          packageName = mangleScopedPackageName(packagePath.split(directorySeparator).slice(0, 2).join(directorySeparator));
        } else {
          packageName = packagePath.split(directorySeparator).slice(0, 1)[0];
        }
        const { resolvedModule } = resolveModuleName(`@tsplus-types/${packageName}`, options.configFilePath, { ...options, resolveJsonModule: true }, sys);
        if (resolvedModule) {
          resolvedPaths.push(resolvedModule.resolvedFileName);
        }
      }
      resolvedPathsCache[options.configFilePath] = resolvedPaths
      return resolvedPaths
  }

3:

const resolvedModuleCache = {}

const resolvedModule = key in resolvedModuleCache ? resolvedModuleCache[key] : (resolvedModuleCache[key] = resolveModuleName(moduleName, options.configFilePath, options, sys).resolvedModule);

@patroza
Copy link
Author

patroza commented Dec 27, 2022

@0x706b 0x706b mentioned this issue Dec 27, 2022
@patroza
Copy link
Author

patroza commented Dec 28, 2022

@0x706b this seems to work well now, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant