Skip to content

Commit da20a19

Browse files
bakkotv8-scoped@luci-project-accounts.iam.gserviceaccount.com
authored andcommitted
[builtins] Make Promise resolvers not keep resolved Promises alive
Currently a Promise's resolve/reject functions unconditionally hold the Promise, plus a separate bit to track whether the Promise has been resolved. Instead, hold a single field with Promise|Undefined. This allows GC'ing a resolved Promise even if its resolvers are still alive. [email protected] Fixed: 42213031 Change-Id: Ice645dbabb79e63dfcac8ae843cad95439d7a1f1 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/7646250 Reviewed-by: Darius Mercadier <[email protected]> Commit-Queue: Kevin Gibbons <[email protected]> Reviewed-by: Olivier Flückiger <[email protected]> Cr-Commit-Position: refs/heads/main@{#108183}
1 parent 03a9a9e commit da20a19

8 files changed

Lines changed: 85 additions & 57 deletions

File tree

‎src/builtins/builtins-promise.h‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ namespace internal {
1313
class PromiseBuiltins {
1414
public:
1515
enum PromiseResolvingFunctionContextSlot {
16-
// The promise which resolve/reject callbacks fulfill.
17-
kPromiseSlot = Context::MIN_CONTEXT_SLOTS,
18-
19-
// Whether the callback was already invoked.
20-
kAlreadyResolvedSlot,
16+
// The promise which resolve/reject callbacks fulfill, or Undefined
17+
// if already resolved.
18+
kPromiseIfNotResolvedSlot = Context::MIN_CONTEXT_SLOTS,
2119

2220
// Whether to trigger a debug event or not. Used in catch
2321
// prediction.

‎src/builtins/promise-abstract-operations.tq‎

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ const kPromiseCapabilitySize:
278278
type PromiseResolvingFunctionContext extends FunctionContext;
279279
extern enum PromiseResolvingFunctionContextSlot extends intptr
280280
constexpr 'PromiseBuiltins::PromiseResolvingFunctionContextSlot' {
281-
kPromiseSlot: Slot<PromiseResolvingFunctionContext, JSPromise>,
282-
kAlreadyResolvedSlot: Slot<PromiseResolvingFunctionContext, Boolean>,
281+
kPromiseIfNotResolvedSlot:
282+
Slot<PromiseResolvingFunctionContext, JSPromise|Undefined>,
283283
kDebugEventSlot: Slot<PromiseResolvingFunctionContext, Boolean>,
284284
kPromiseContextLength
285285
}
@@ -403,25 +403,29 @@ transitioning builtin NewPromiseCapability(
403403
transitioning javascript builtin PromiseCapabilityDefaultReject(
404404
js-implicit context: Context, receiver: JSAny)(reason: JSAny): JSAny {
405405
const context = %RawDownCast<PromiseResolvingFunctionContext>(context);
406-
// 2. Let promise be F.[[Promise]].
407-
const promise =
408-
*ContextSlot(context, PromiseResolvingFunctionContextSlot::kPromiseSlot);
409-
410-
// 3. Let alreadyResolved be F.[[AlreadyResolved]].
411-
const alreadyResolved = *ContextSlot(
412-
context, PromiseResolvingFunctionContextSlot::kAlreadyResolvedSlot);
413-
414-
// 4. If alreadyResolved.[[Value]] is true, return undefined.
415-
if (alreadyResolved == True) {
416-
return Undefined;
406+
// 2. Let promise be promiseOrEmpty.[[Value]].
407+
const promiseOrEmpty =
408+
*ContextSlot(
409+
context, PromiseResolvingFunctionContextSlot::kPromiseIfNotResolvedSlot);
410+
411+
// 1. If promiseOrEmpty.[[Value]] is ~empty~, return undefined.
412+
let promise: JSPromise;
413+
typeswitch (promiseOrEmpty) {
414+
case (Undefined): {
415+
return Undefined;
416+
}
417+
case (p: JSPromise): {
418+
promise = p;
419+
}
417420
}
418421

419-
// 5. Set alreadyResolved.[[Value]] to true.
422+
// 3. Set promiseOrEmpty.[[Value]] to ~empty~.
420423
*ContextSlot(
421-
context, PromiseResolvingFunctionContextSlot::kAlreadyResolvedSlot) =
422-
True;
424+
context, PromiseResolvingFunctionContextSlot::kPromiseIfNotResolvedSlot) =
425+
Undefined;
423426

424-
// 6. Return RejectPromise(promise, reason).
427+
// 4. Perform RejectPromise(promise, reason).
428+
// 5. Return undefined.
425429
const debugEvent = *ContextSlot(
426430
context, PromiseResolvingFunctionContextSlot::kDebugEventSlot);
427431
return RejectPromise(promise, reason, debugEvent);
@@ -431,23 +435,26 @@ transitioning javascript builtin PromiseCapabilityDefaultReject(
431435
transitioning javascript builtin PromiseCapabilityDefaultResolve(
432436
js-implicit context: Context, receiver: JSAny)(resolution: JSAny): JSAny {
433437
const context = %RawDownCast<PromiseResolvingFunctionContext>(context);
434-
// 2. Let promise be F.[[Promise]].
435-
const promise: JSPromise =
436-
*ContextSlot(context, PromiseResolvingFunctionContextSlot::kPromiseSlot);
437-
438-
// 3. Let alreadyResolved be F.[[AlreadyResolved]].
439-
const alreadyResolved: Boolean = *ContextSlot(
440-
context, PromiseResolvingFunctionContextSlot::kAlreadyResolvedSlot);
441-
442-
// 4. If alreadyResolved.[[Value]] is true, return undefined.
443-
if (alreadyResolved == True) {
444-
return Undefined;
438+
// 2. Let promise be promiseOrEmpty.[[Value]].
439+
const promiseOrEmpty =
440+
*ContextSlot(
441+
context, PromiseResolvingFunctionContextSlot::kPromiseIfNotResolvedSlot);
442+
443+
// 1. If promiseOrEmpty.[[Value]] is ~empty~, return undefined.
444+
let promise: JSPromise;
445+
typeswitch (promiseOrEmpty) {
446+
case (Undefined): {
447+
return Undefined;
448+
}
449+
case (p: JSPromise): {
450+
promise = p;
451+
}
445452
}
446453

447-
// 5. Set alreadyResolved.[[Value]] to true.
454+
// 3. Set promiseOrEmpty.[[Value]] to ~empty~.
448455
*ContextSlot(
449-
context, PromiseResolvingFunctionContextSlot::kAlreadyResolvedSlot) =
450-
True;
456+
context, PromiseResolvingFunctionContextSlot::kPromiseIfNotResolvedSlot) =
457+
Undefined;
451458

452459
// The rest of the logic (and the catch prediction) is
453460
// encapsulated in the dedicated ResolvePromise builtin.

‎src/builtins/promise-all.tq‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,14 @@ macro CreatePromiseResolvingFunctionsContext(
6363
nativeContext,
6464
PromiseResolvingFunctionContextSlot::kPromiseContextLength));
6565
InitContextSlot(
66-
resolveContext, PromiseResolvingFunctionContextSlot::kPromiseSlot,
67-
promise);
68-
InitContextSlot(
69-
resolveContext, PromiseResolvingFunctionContextSlot::kAlreadyResolvedSlot,
70-
False);
66+
resolveContext,
67+
PromiseResolvingFunctionContextSlot::kPromiseIfNotResolvedSlot, promise);
7168
InitContextSlot(
7269
resolveContext, PromiseResolvingFunctionContextSlot::kDebugEventSlot,
7370
debugEvent);
7471
static_assert(
7572
PromiseResolvingFunctionContextSlot::kPromiseContextLength ==
76-
ContextSlot::MIN_CONTEXT_SLOTS + 3);
73+
ContextSlot::MIN_CONTEXT_SLOTS + 2);
7774
return resolveContext;
7875
}
7976

‎src/compiler/js-call-reducer.cc‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2887,10 +2887,8 @@ TNode<Object> PromiseBuiltinReducerAssembler::ReducePromiseConstructor(
28872887
// Allocate a promise context for the closures below.
28882888
TNode<Context> promise_context = CreateFunctionContext(
28892889
native_context, context, PromiseBuiltins::kPromiseContextLength);
2890-
StoreContextNoCellSlot(promise_context, PromiseBuiltins::kPromiseSlot,
2891-
promise);
2892-
StoreContextNoCellSlot(promise_context, PromiseBuiltins::kAlreadyResolvedSlot,
2893-
FalseConstant());
2890+
StoreContextNoCellSlot(promise_context,
2891+
PromiseBuiltins::kPromiseIfNotResolvedSlot, promise);
28942892
StoreContextNoCellSlot(promise_context, PromiseBuiltins::kDebugEventSlot,
28952893
TrueConstant());
28962894

‎src/execution/isolate.cc‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,9 +1378,12 @@ void CaptureAsyncStackTrace(Isolate* isolate, DirectHandle<JSPromise> promise,
13781378
DirectHandle<JSFunction> function(
13791379
Cast<JSFunction>(reaction->fulfill_handler()), isolate);
13801380
DirectHandle<Context> context(function->context(), isolate);
1381-
promise = direct_handle(
1382-
Cast<JSPromise>(context->GetNoCell(PromiseBuiltins::kPromiseSlot)),
1383-
isolate);
1381+
Tagged<Object> promise_or_undefined =
1382+
context->GetNoCell(PromiseBuiltins::kPromiseIfNotResolvedSlot);
1383+
if (!TryCast(direct_handle(promise_or_undefined, isolate), &promise)) {
1384+
DCHECK(IsUndefined(promise_or_undefined));
1385+
return;
1386+
}
13841387
} else {
13851388
// We have some generic promise chain here, so try to
13861389
// continue with the chained promise on the reaction

‎src/heap/factory.cc‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5154,18 +5154,15 @@ DirectHandle<Context> Factory::CreatePromiseResolvingFunctionsContext(
51545154
isolate()->native_context(),
51555155
PromiseBuiltins::PromiseResolvingFunctionContextSlot::
51565156
kPromiseContextLength);
5157-
context->SetNoCell(
5158-
PromiseBuiltins::PromiseResolvingFunctionContextSlot::kPromiseSlot,
5159-
*promise);
51605157
context->SetNoCell(PromiseBuiltins::PromiseResolvingFunctionContextSlot::
5161-
kAlreadyResolvedSlot,
5162-
*false_value());
5158+
kPromiseIfNotResolvedSlot,
5159+
*promise);
51635160
context->SetNoCell(
51645161
PromiseBuiltins::PromiseResolvingFunctionContextSlot::kDebugEventSlot,
51655162
*false_value());
51665163
DCHECK_EQ(PromiseBuiltins::PromiseResolvingFunctionContextSlot::
51675164
kPromiseContextLength,
5168-
Context::MIN_CONTEXT_SLOTS + 3);
5165+
Context::MIN_CONTEXT_SLOTS + 2);
51695166
return context;
51705167
}
51715168

‎test/cctest/test-code-stub-assembler.cc‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3038,7 +3038,8 @@ TEST(CreatePromiseResolvingFunctionsContext) {
30383038
DirectHandle<Context> context_js = Cast<Context>(result);
30393039
CHECK_EQ(isolate->root(RootIndex::kEmptyScopeInfo), context_js->scope_info());
30403040
CHECK_EQ(*isolate->native_context(), context_js->native_context());
3041-
CHECK(IsJSPromise(context_js->GetNoCell(PromiseBuiltins::kPromiseSlot)));
3041+
CHECK(IsJSPromise(
3042+
context_js->GetNoCell(PromiseBuiltins::kPromiseIfNotResolvedSlot)));
30423043
CHECK_EQ(ReadOnlyRoots(isolate).false_value(),
30433044
context_js->GetNoCell(PromiseBuiltins::kDebugEventSlot));
30443045
}
@@ -3247,7 +3248,8 @@ TEST(NewPromiseCapability) {
32473248
CHECK_EQ(*isolate->native_context(), callback_context->native_context());
32483249
CHECK_EQ(PromiseBuiltins::kPromiseContextLength,
32493250
callback_context->length());
3250-
CHECK_EQ(callback_context->GetNoCell(PromiseBuiltins::kPromiseSlot),
3251+
CHECK_EQ(callback_context->GetNoCell(
3252+
PromiseBuiltins::kPromiseIfNotResolvedSlot),
32513253
result->promise());
32523254
}
32533255
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2026 the V8 project authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// Flags: --expose-gc
6+
7+
const pending = new Promise(() => {});
8+
9+
(async function () {
10+
let wr;
11+
12+
await (async function () {
13+
const payload = { };
14+
wr = new WeakRef(payload);
15+
const resolved = Promise.resolve(payload);
16+
// The pending Promise should not prevent GC of the race Promise once the race settles.
17+
await Promise.race([pending, resolved]);
18+
})();
19+
20+
await gc({ type: 'major', execution: 'async' });
21+
22+
assertEquals(undefined, wr.deref());
23+
})().catch((e) => {
24+
console.error(e);
25+
quit(1);
26+
});

0 commit comments

Comments
 (0)