Skip to content

Commit ac480bc

Browse files
committed
src: ffi: create fast-call metadata Symbols lazily
The FFI fast-call wrappers key per-function metadata on raw FFI functions using two per-isolate Symbols (kFastArguments / kFastBufferInvoke) that were declared in src/env_properties.h. Everything in env_properties.h is allocated while the startup snapshot is built, so each Symbol advances the isolate's identity-hash RNG before Object.prototype / Function.prototype receive their snapshot identity hashes. In the snapshot produced for Node 26.4.0+ this shifted those hashes so a function map (a function whose `length` was redefined) and a plain-object map (an object literal with an accessor) collide in V8's 64-slot NormalizedMapCache. Every store into such objects then misses the inline cache, and the repro reported in the linked issue is roughly 7x slower. Create the two Symbols lazily in the FFI binding's Initialize, on the first run of internalBinding('ffi') at runtime, instead of declaring them in env_properties.h. They are therefore not allocated during snapshot serialization and no longer bias the snapshot's prototype identity hashes. Their export, property layout, and the fast-call feature behavior are unchanged. Refs: nodejs#66011 Signed-off-by: Matteo Collina <[email protected]> Assisted-by: Pi
1 parent 7aaf9b4 commit ac480bc

4 files changed

Lines changed: 70 additions & 3 deletions

File tree

‎src/env-inl.h‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,24 @@ void Environment::set_process_exit_handler(
866866
#undef V
867867
#undef VM
868868

869+
inline v8::Local<v8::Symbol> IsolateData::ffi_fast_arguments_symbol() const {
870+
return ffi_fast_arguments_symbol_.Get(isolate_);
871+
}
872+
inline void IsolateData::set_ffi_fast_arguments_symbol(
873+
v8::Local<v8::Symbol> value) {
874+
CHECK(ffi_fast_arguments_symbol_.IsEmpty());
875+
ffi_fast_arguments_symbol_.Set(isolate_, value);
876+
}
877+
inline v8::Local<v8::Symbol> IsolateData::ffi_fast_buffer_invoke_symbol()
878+
const {
879+
return ffi_fast_buffer_invoke_symbol_.Get(isolate_);
880+
}
881+
inline void IsolateData::set_ffi_fast_buffer_invoke_symbol(
882+
v8::Local<v8::Symbol> value) {
883+
CHECK(ffi_fast_buffer_invoke_symbol_.IsEmpty());
884+
ffi_fast_buffer_invoke_symbol_.Set(isolate_, value);
885+
}
886+
869887
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
870888
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
871889
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
@@ -881,6 +899,22 @@ void Environment::set_process_exit_handler(
881899
#undef VY
882900
#undef VP
883901

902+
inline v8::Local<v8::Symbol> Environment::ffi_fast_arguments_symbol() const {
903+
return isolate_data()->ffi_fast_arguments_symbol();
904+
}
905+
inline void Environment::set_ffi_fast_arguments_symbol(
906+
v8::Local<v8::Symbol> value) {
907+
isolate_data()->set_ffi_fast_arguments_symbol(value);
908+
}
909+
inline v8::Local<v8::Symbol> Environment::ffi_fast_buffer_invoke_symbol()
910+
const {
911+
return isolate_data()->ffi_fast_buffer_invoke_symbol();
912+
}
913+
inline void Environment::set_ffi_fast_buffer_invoke_symbol(
914+
v8::Local<v8::Symbol> value) {
915+
isolate_data()->set_ffi_fast_buffer_invoke_symbol(value);
916+
}
917+
884918
#define V(Name, label, _, __) \
885919
inline v8::Local<v8::String> Environment::Name##_permission_string() const { \
886920
return isolate_data()->Name##_permission_string(); \

‎src/env.h‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,17 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
205205

206206
inline v8::Local<v8::String> async_wrap_provider(int index) const;
207207

208+
// Symbols used by the FFI fast-call API to key per-function metadata on raw
209+
// FFI functions. Kept out of env_properties.h so they are created lazily at
210+
// runtime, not while the startup snapshot is built (allocating Symbols during
211+
// serialization advances the isolate's identity-hash RNG, which can shift the
212+
// snapshot hashes for Object.prototype/Function.prototype and make a function
213+
// map and a plain-object map collide in V8's NormalizedMapCache).
214+
inline v8::Local<v8::Symbol> ffi_fast_arguments_symbol() const;
215+
inline void set_ffi_fast_arguments_symbol(v8::Local<v8::Symbol> value);
216+
inline v8::Local<v8::Symbol> ffi_fast_buffer_invoke_symbol() const;
217+
inline void set_ffi_fast_buffer_invoke_symbol(v8::Local<v8::Symbol> value);
218+
208219
size_t max_young_gen_size = 1;
209220
std::unordered_map<const char*, v8::Eternal<v8::String>> static_str_map;
210221

@@ -245,6 +256,9 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
245256
PERMISSIONS(V)
246257
#undef V
247258

259+
v8::Eternal<v8::Symbol> ffi_fast_arguments_symbol_;
260+
v8::Eternal<v8::Symbol> ffi_fast_buffer_invoke_symbol_;
261+
248262
// Keep a list of all Persistent strings used for AsyncWrap Provider types.
249263
std::array<v8::Eternal<v8::String>, AsyncWrap::PROVIDERS_LENGTH>
250264
async_wrap_providers_;
@@ -892,6 +906,12 @@ class Environment final : public MemoryRetainer {
892906
#undef VY
893907
#undef VP
894908

909+
// Runtime-created FFI fast-call API Symbols (see IsolateData).
910+
inline v8::Local<v8::Symbol> ffi_fast_arguments_symbol() const;
911+
inline void set_ffi_fast_arguments_symbol(v8::Local<v8::Symbol> value);
912+
inline v8::Local<v8::Symbol> ffi_fast_buffer_invoke_symbol() const;
913+
inline void set_ffi_fast_buffer_invoke_symbol(v8::Local<v8::Symbol> value);
914+
895915
#define V(Name, label, _, __) \
896916
inline v8::Local<v8::String> Name##_permission_string() const;
897917
PERMISSIONS(V)

‎src/env_properties.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@
5050
V(ffi_sb_invoke_slow_symbol, "ffi_sb_invoke_slow_symbol") \
5151
V(ffi_sb_arguments_symbol, "ffi_sb_arguments_symbol") \
5252
V(ffi_sb_return_symbol, "ffi_sb_return_symbol") \
53-
V(ffi_fast_arguments_symbol, "ffi_fast_arguments_symbol") \
54-
V(ffi_fast_buffer_invoke_symbol, "ffi_fast_buffer_invoke_symbol") \
5553
V(constructor_key_symbol, "constructor_key_symbol") \
5654
V(handle_onclose_symbol, "handle_onclose") \
5755
V(no_message_symbol, "no_message_symbol") \

‎src/node_ffi.cc‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,22 @@ static void Initialize(Local<Object> target,
13781378
env->ffi_sb_return_symbol())
13791379
.Check();
13801380
// Fast API wrappers use separate metadata Symbols so pointer-conversion
1381-
// routing does not depend on SharedBuffer internals.
1381+
// routing does not depend on SharedBuffer internals. These are created here
1382+
// (at runtime, on first `internalBinding('ffi')`) instead of being declared
1383+
// in env_properties.h, so they are not allocated while the startup snapshot
1384+
// is being built. Allocating Symbols during snapshot serialization advances
1385+
// the isolate's identity-hash RNG and shifts the identity hashes baked into
1386+
// the snapshot for Object.prototype / Function.prototype, which can make a
1387+
// function map and a plain-object map collide in V8's NormalizedMapCache.
1388+
if (env->ffi_fast_arguments_symbol().IsEmpty()) {
1389+
env->set_ffi_fast_arguments_symbol(v8::Symbol::New(
1390+
isolate, FIXED_ONE_BYTE_STRING(isolate, "ffi_fast_arguments_symbol")));
1391+
}
1392+
if (env->ffi_fast_buffer_invoke_symbol().IsEmpty()) {
1393+
env->set_ffi_fast_buffer_invoke_symbol(v8::Symbol::New(
1394+
isolate,
1395+
FIXED_ONE_BYTE_STRING(isolate, "ffi_fast_buffer_invoke_symbol")));
1396+
}
13821397
target
13831398
->Set(context,
13841399
FIXED_ONE_BYTE_STRING(isolate, "kFastArguments"),

0 commit comments

Comments
 (0)