sveltejs / kit

web development, streamlined
https://kit.svelte.dev
MIT License
18.42k stars 1.88k forks source link

escape_json_string_in_html causes ~23 MB of allocations for 1 MB of JSON #3649

Closed tv42 closed 2 years ago

tv42 commented 2 years ago

Describe the bug

I was troubleshooting why my SvelteKit app SSR was consuming a lot of RAM. There's likely other reasons, but this jumped out of the profile:

image

escape_json_string_in_html (which is just a wrapper for escape, https://github.com/sveltejs/kit/blob/59badb515755a9294365e8084f9beefa10a90773/packages/kit/src/utils/escape.js#L53) allocates 23 MB for a 1MB input.

Reproduction

From a Page's load function, make a fetch to a 1 MB JSON file, and return it as a prop.

The SSR HTML will end with something like

 <script type="application/json" data-type="svelte-data" data-url="/api/graphql" data-body="foo">...</script>

containing said HTML, but creating that HTML apparently takes about 23x the size of the JSON in allocations.

Logs

No response

System Info

System:
    OS: Linux 5.15 NixOS 21.11 (Porcupine) 21.11 (Porcupine)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
    Memory: 6.33 GB / 62.61 GB
    Container: Yes
    Shell: 5.1.8 - /nix/store/xdxv22yb7qnjfyf997r9fx804761z14c-bash-interactive-5.1-p8/bin/bash
  Binaries:
    Node: 16.13.0 - /nix/store/26lslfmvwyhspppbinznh111fyaxx62z-nodejs-16.13.0/bin/node
    npm: 8.1.0 - /nix/store/26lslfmvwyhspppbinznh111fyaxx62z-nodejs-16.13.0/bin/npm
  Browsers:
    Firefox: 96.0.2
  npmPackages:
    @sveltejs/adapter-cloudflare-workers: ^1.0.0-next.30 => 1.0.0-next.30
    @sveltejs/kit: ^1.0.0-next.239 => 1.0.0-next.239
    svelte: ^3.46.1 => 3.46.2

Severity

annoyance

Additional Information

No response

bluwy commented 2 years ago

@tv42 there has been changes to the function (renamed escape_json_in_html). Can you confirm that it still takes up large amount of memory with the latest version?

benmccann commented 2 years ago

@tv42 can you please confirm whether this issue is still present? Otherwise we will close this issue shortly

tv42 commented 2 years ago

This seems fixed.

In @sveltejs/kit@1.0.0-next.294, the memory use looks to be roughly equal to size of the JSON response (~1 MB JSON, ~1.1 MB allocated in render_json_payload_script).

In older @sveltejs/kit@1.0.0-next.267, that was ~34 MB allocated in escape.

How to see SvelteKit SSR memory profiles

npm run build && node --inspect node_modules/.bin/svelte-kit preview

Follow instructions at https://nodejs.org/en/docs/guides/debugging-getting-started/ to open inspector in browser. I'm assuming Chrome here for the instructions.