Skip to content

Avoid storing Module/SemContext in non-debug global eval#2090

Merged
avp merged 1 commit into
facebook:250829098.0.0-stablefrom
avp:memory-fix
Jun 25, 2026
Merged

Avoid storing Module/SemContext in non-debug global eval#2090
avp merged 1 commit into
facebook:250829098.0.0-stablefrom
avp:memory-fix

Conversation

@avp

@avp avp commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Summary:
Pull Request resolved: #2053

BCProviderFromSrc is typically used in debug and dev scenarios, but some users also invoke it via production global eval.

We don't want to keep the M and semCtx alive for these cases, because the debugger doesn't exist and there's no lazy compilation to require this data. It just bloats memory usage and is never accessed.

Note that this won't reduce memory usage for large eval strings when smart compilation is enabled, because that still uses lazy compilation. To fix it in those scenarios, the user must also pass CompilationMode::ForceEagerCompilation to RuntimeConfig.

Ran a simple eval benchmark:

(function() {
  globalThis.arr = [];
  var N = 10_000;
  for (var i = 0; i < N; ++i) {
    var f = (1,eval)(`
    function foo_${i}() {}
    `);
    globalThis.arr.push(f);
  }

  // loop while we check the memory
  gc();
  for (;;) {}
})();

This diff takes the total memory usage from 950 MB to 140 MB (measured with Activity Monitor on M4 Max MacBook).

Reviewed By: lavenzg

Differential Revision: D108782017

fbshipit-source-id: 526f1ab1e3c4347d312270f11c6ba3ca5813fb4a

Summary:
Pull Request resolved: facebook#2053

`BCProviderFromSrc` is typically used in debug and dev scenarios,
but some users also invoke it via production global `eval`.

We don't want to keep the `M` and `semCtx` alive for these cases,
because the debugger doesn't exist and there's no lazy compilation to
require this data. It just bloats memory usage and is never accessed.

Note that this won't reduce memory usage for large `eval` strings when
smart compilation is enabled, because that still uses lazy compilation.
To fix it in those scenarios, the user must also pass
`CompilationMode::ForceEagerCompilation` to `RuntimeConfig`.

Ran a simple `eval` benchmark:
```
(function() {
  globalThis.arr = [];
  var N = 10_000;
  for (var i = 0; i < N; ++i) {
    var f = (1,eval)(`
    function foo_${i}() {}
    `);
    globalThis.arr.push(f);
  }

  // loop while we check the memory
  gc();
  for (;;) {}
})();
```

This diff takes the total memory usage from 950 MB to 140 MB
(measured with Activity Monitor on M4 Max MacBook).

Reviewed By: lavenzg

Differential Revision: D108782017

fbshipit-source-id: 526f1ab1e3c4347d312270f11c6ba3ca5813fb4a
@avp avp changed the title Avoid storing Module/SemContext in non-debug global eval (#2053) Avoid storing Module/SemContext in non-debug global eval Jun 25, 2026
@meta-cla meta-cla Bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Jun 25, 2026
@avp avp merged commit 5f6be85 into facebook:250829098.0.0-stable Jun 25, 2026
25 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Do not delete this pull request or issue due to inactivity.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant