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

High CPU usage after upgrading to v0.31.1 #642

Closed
seahindeniz opened this issue Jun 11, 2024 · 2 comments
Closed

High CPU usage after upgrading to v0.31.1 #642

seahindeniz opened this issue Jun 11, 2024 · 2 comments
Assignees
Labels
external Problem has external origin question Further information is requested

Comments

@seahindeniz
Copy link
Sponsor

After following the docs and migrating the code, I have noticed that high CPU usage while coding and the auto-completion service was getting stuck on loading state.

Repro case: https://github.com/seahindeniz/valibot-issue

Steps:

  1. Create a new project
    1. SolidStart pnpm create solid
    2. Vanilla pnpm create vite valibot-test --template vanilla
  2. Copy utils folder from the repo mentioned above
  3. Open valibotUtils.ts and try to type something like v.Gen
  4. Observe the high CPU usage

Record:

980d1c54-2022-478b-b00d-ce20a4c0f2f8.mp4
@fabian-hiller fabian-hiller self-assigned this Jun 11, 2024
@fabian-hiller fabian-hiller added question Further information is requested external Problem has external origin labels Jun 11, 2024
@fabian-hiller
Copy link
Owner

Does this problem still occur when you restart your computer? Are you sure it did not happen with the previous version? Can you double check? I do not think this is related to our source code.

@seahindeniz
Copy link
Sponsor Author

Yes, you are right. The odd part is, I was a bit sure that I got this issue after the upgrade but seems like I got the timeline wrong 😓 I have downgraded the versions on the minimal repo, and it is still reproducible.

I have tracked down to this point

export function createForm<TFieldValues extends FieldValues>(props: {
  initialValues?: FormOptions<TFieldValues>['initialValues'];
}) {
  const scope = createFormModular<TFieldValues>({
    initialValues: props.initialValues,
  });

  return scope;
}

If I comment out the return line in this function, there's no CPU issue. But when I return scope from the function, then the TS Server goes crazy.

After converting the function as the same in the type definition file, problem gone. However, if I don't specify the return type manually, it still causes TS server issue, strange.

Final trial

export function createForm<TFieldValues extends FieldValues, TResponseData extends ResponseData = undefined>(options?: FormOptions<TFieldValues>): [
  FormStore<TFieldValues, TResponseData>,
  {
      Form: (props: Omit<FormProps<TFieldValues, TResponseData>, 'of'>) => JSX.Element;
      Field: <TFieldName extends FieldPath<TFieldValues>>(props: FieldPathValue<TFieldValues, TFieldName> extends MaybeValue<string> ? PartialKey<Omit<FieldProps<TFieldValues, TResponseData, TFieldName>, 'of'>, 'type'> : Omit<FieldProps<TFieldValues, TResponseData, TFieldName>, 'of'>) => JSX.Element;
      FieldArray: <TFieldArrayName extends FieldArrayPath<TFieldValues>>(props: Omit<FieldArrayProps<TFieldValues, TResponseData, TFieldArrayName>, 'of'>) => JSX.Element;
  }
] {
  const scope = createFormModular<TFieldValues, TResponseData>(options);

  return scope;
}

The main point of doing this is to create a reusable facilitator function that creates a form but with a bit of sugar to trigger the validate function when the lang/language gets a reactive update.

Since the above version solves the issue, I guess it is safe to close the issue.
Thank you for leading it to double-check @fabian-hiller 🙏🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external Problem has external origin question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants