Commit 70a6df2
fix(script): drop AST after script execution (boa-dev#4876)
## Context
This PR extends the idea introduced in boa-dev#4855.
PR boa-dev#4855 fixed a memory retention issue for **modules**, where the
parsed AST and source text were kept in memory even after module
initialization.
This PR applies the same idea to **scripts**, ensuring that the parsed
AST is released once script execution has completed.
# Summary
This PR prevents scripts from keeping their parsed AST after
compilation.
Right now `Script::Inner` stores the full AST in the `source` field:
```rust
struct Inner {
realm: Realm,
source: boa_ast::Script,
source_text: SourceText,
codeblock: GcRefCell<Option<Gc<CodeBlock>>>,
}
```
The AST is only needed while generating bytecode in
`Script::codeblock()`.
After compilation it is not used anymore, but it still stays in
`Script::Inner`.
Function objects created from the script keep a reference to the script
(`OrdinaryFunction::script_or_module`). Because of this, the script can
stay alive for a long time, which also keeps the whole AST in memory.
# Steps to Reproduce
In a long-running program (for example a REPL or server), repeatedly run
scripts that define functions:
```rust
let script = Script::parse(Source::from_bytes(src), None, &mut context)?;
script.evaluate(&mut context)?;
```
If those functions are stored on the global object, they keep the script
alive.
Since the script holds the AST, memory usage keeps growing as more
scripts run.
# Impact
This mostly affects long-running programs that embed Boa.
Every script that defines functions can keep its AST in memory forever.
AST structures are usually much bigger than the source code, so memory
usage can grow over time.
Modules already drop their AST after linking, but scripts did not.
# Fix
`Script::Inner::source` is changed to:
```rust
source: RefCell<Option<boa_ast::Script>>
```
The AST is used during compilation and then removed once the `CodeBlock`
is created:
```rust
*self.inner.source.borrow_mut() = None;
```
# Result
After this change, the AST is dropped once compilation finishes.
Scripts now behave consistently with modules after the change introduced
in boa-dev#4855.
**The AST is only required during parsing and compilation. Once the
script finishes execution and the result is consumed, it can be safely
released to reduce memory usage in long-running runtimes.**
---------
Signed-off-by: ashnaaseth2325-oss <ashnaaseth2325@gmail.com>
Co-authored-by: José Julián Espina <julian.espina@canonical.com>
Co-authored-by: José Julián Espina <jedel0124@gmail.com>1 parent bef1acb commit 70a6df2
1 file changed
Lines changed: 58 additions & 49 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
12 | 15 | | |
13 | 16 | | |
14 | 17 | | |
| |||
37 | 40 | | |
38 | 41 | | |
39 | 42 | | |
40 | | - | |
| 43 | + | |
41 | 44 | | |
42 | 45 | | |
43 | 46 | | |
44 | 47 | | |
45 | | - | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
46 | 53 | | |
47 | 54 | | |
48 | 55 | | |
49 | 56 | | |
50 | | - | |
| 57 | + | |
51 | 58 | | |
52 | | - | |
53 | 59 | | |
54 | 60 | | |
55 | 61 | | |
| |||
102 | 108 | | |
103 | 109 | | |
104 | 110 | | |
105 | | - | |
| 111 | + | |
106 | 112 | | |
107 | | - | |
108 | 113 | | |
109 | 114 | | |
110 | 115 | | |
| |||
116 | 121 | | |
117 | 122 | | |
118 | 123 | | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
161 | 168 | | |
162 | 169 | | |
163 | 170 | | |
| |||
173 | 180 | | |
174 | 181 | | |
175 | 182 | | |
| 183 | + | |
176 | 184 | | |
177 | 185 | | |
178 | 186 | | |
| |||
205 | 213 | | |
206 | 214 | | |
207 | 215 | | |
| 216 | + | |
208 | 217 | | |
209 | 218 | | |
210 | 219 | | |
| |||
0 commit comments