Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Fix memory leak in @shopify/dates when SSR #1277

Merged
merged 1 commit into from
Feb 14, 2020

Conversation

helloneele
Copy link
Contributor

@helloneele helloneele commented Feb 12, 2020

Description

This fixes the memory leak in the dates package that we were seeing when upgrading to any version after v 0.1.26 in Web.

More info is here: https://github.com/Shopify/web/issues/23454

Type of change

  • Patch: Bug/ Documentation fix (non-breaking change which fixes an issue or adds documentation)
  • Minor: New feature (non-breaking change which adds functionality)
  • Major: Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

  • I have added a changelog entry, prefixed by the type of change noted above

dateTimeFormatCacheKey,
);
const intl = new Map();
const memoizedGetDateTimeFormat = function(locale, options) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does this not cause leaks but the implementation from function-enhancers does? Could we fix it there so that we don't risk this happening again in the future?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The function enhancers implementation doesn't memoize when server side rendering. https://github.com/Shopify/quilt/blob/master/packages/function-enhancers/src/memoize.ts#L18

Copy link
Contributor Author

@helloneele helloneele Feb 12, 2020

Choose a reason for hiding this comment

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

I don't know what the reasoning was behind not memoizing when SSRing, I mostly just wanted to try this out if that fixes things. It does locally, but wanted to see it in production. Happy to change this later on. Hence my question for a beta release.

Copy link
Contributor

Choose a reason for hiding this comment

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

SSR means the react app only render the one time.
This mean memoize on server actually does not enhance performance, but leave behind memozing that will never be clear off.

this is the reasoning in theory, but maybe in practice there are some thing we are missing?

Copy link
Contributor

Choose a reason for hiding this comment

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

In the past we've had memory leaks from memoizing on the server, so that was the idea between not doing so. I fear this might make things worse instead of better.

Copy link
Contributor

@marutypes marutypes Feb 12, 2020

Choose a reason for hiding this comment

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

That said, if it does work locally I suppose it's worth a shot. It worries me that we have no real rationale for why it would work though :(

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Explanation for not memoizing on the server makes sense. 👍

It worries me that we have no real rationale for why it would work though :(

I think the rationale for this is that there is a v8/Node.js issue which leads to Intl.DateTimeFormat not being properly cleaned up by the GC, and since we don't memoize we create more and more of those objects. So memoizing those objects minimizes the overall amount of objects we'll create which ends up in less unreachable objects for the GC.

Copy link
Contributor

@michenly michenly Feb 12, 2020

Choose a reason for hiding this comment

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

if we have legit reasons to not skip over server memorization (which I assume could happen else where), then lets add an option to memoize to specifically memorize no matter what.

export default function memoize<Method extends (...args: any[]) => any>(
  method: Method,
  resolver?: (...args: Parameters<Method>) => any,
  skipOnServer = true
): Method {
if (skipOnServer && typeof window === 'undefined') {
      return method.apply(this, args);
    }

Copy link
Contributor Author

@helloneele helloneele Feb 12, 2020

Choose a reason for hiding this comment

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

I'll try what I have now in production, if that works I'm happy to change the memoizer. 👍
Though, I still think this is a very rare case just related to Intl.DateTimeFormat.

@helloneele helloneele force-pushed the fix-memory-leak-in-dates-pkg branch from de00656 to 3530497 Compare February 13, 2020 18:17
@helloneele helloneele deployed to beta-test-memory-leak-fix February 13, 2020 19:05 Active
@helloneele helloneele force-pushed the fix-memory-leak-in-dates-pkg branch 2 times, most recently from aa207a8 to 01d619d Compare February 14, 2020 15:50
@helloneele helloneele force-pushed the fix-memory-leak-in-dates-pkg branch from 01d619d to d398942 Compare February 14, 2020 15:56
@michenly michenly merged commit a81e9a8 into master Feb 14, 2020
@michenly michenly deleted the fix-memory-leak-in-dates-pkg branch February 14, 2020 16:36
@helloneele helloneele temporarily deployed to production February 14, 2020 16:54 Inactive
@vsumner vsumner deployed to just-use-act-beta February 19, 2020 20:05 Active
@helloneele helloneele deployed to beta-test-reacti18n-memory-leak-fix February 20, 2020 20:41 Active
@carysmills carysmills deployed to beta-test-formatting-12-hr-dates February 26, 2020 19:32 Active
@helloneele helloneele deployed to test-use-memoized-number-format March 6, 2020 19:25 Active
@michenly michenly temporarily deployed to gem March 24, 2020 21:43 Inactive
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants