Skip to content

Commit 2fb8553

Browse files
committed
Mark OpaqueWrappable specializations as final
For better perf optimization of dynamic_cast<T*>
1 parent 4a7a063 commit 2fb8553

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/workerd/jsg/promise.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,13 @@ struct OpaqueWrappableBase: public Wrappable {
3434
}
3535
};
3636

37+
// We declare OpaqueWrappable<T> specializations here as final to allow the compiler
38+
// to optimize dynamic_cast<T*> to simply check if the vtable is equal to T's vtable.
39+
// This provides a minor performance improvement when interacting with opaque-wrapped
40+
// values.
41+
3742
template <typename T>
38-
struct OpaqueWrappable<T, false>: public OpaqueWrappableBase {
43+
struct OpaqueWrappable<T, false> final: public OpaqueWrappableBase {
3944
// Used to implement wrapOpaque().
4045

4146
OpaqueWrappable(T&& value): value(kj::mv(value)) {}
@@ -49,10 +54,17 @@ struct OpaqueWrappable<T, false>: public OpaqueWrappableBase {
4954
};
5055

5156
template <typename T>
52-
struct OpaqueWrappable<T, true>: public OpaqueWrappable<T, false> {
57+
struct OpaqueWrappable<T, true> final: public OpaqueWrappableBase {
5358
// When T is GC-visitable, make sure to implement visitation.
5459

55-
using OpaqueWrappable<T, false>::OpaqueWrappable;
60+
OpaqueWrappable(T&& value): value(kj::mv(value)) {}
61+
62+
T value;
63+
bool movedAway = false;
64+
65+
size_t jsgGetMemorySelfSize() const override final {
66+
return sizeof(OpaqueWrappable);
67+
}
5668

5769
void jsgVisitForGc(GcVisitor& visitor) override {
5870
if (!this->movedAway) {

0 commit comments

Comments
 (0)