GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — src: use an array for faster binding data lookup · nodejs/node@629fc77

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • node.gyp
  • src
    • README.md
    • base_object.h
    • base_object_types.h
    • node_blob.h
    • node_file.h
    • node_http2_state.h
    • node_http_parser.cc
    • node_process.h
    • node_realm-inl.h
    • node_realm.cc
    • node_realm.h
    • node_snapshotable.cc
    • node_snapshotable.h
    • node_url.h
    • node_util.h
    • node_v8.h
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎node.gyp‎

Copy file name to clipboardExpand all lines: node.gyp
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@
585585
'src/async_wrap-inl.h',
586586
'src/base_object.h',
587587
'src/base_object-inl.h',
588+
'src/base_object_types.h',
588589
'src/base64.h',
589590
'src/base64-inl.h',
590591
'src/callback_queue.h',
Collapse file

‎src/README.md‎

Copy file name to clipboardExpand all lines: src/README.md
+19-11Lines changed: 19 additions & 11 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -528,18 +528,32 @@ that state is through the use of `Realm::AddBindingData`, which gives
528528
binding functions access to an object for storing such state.
529529
That object is always a [`BaseObject`][].
530530

531-
Its class needs to have a static `type_name` field based on a
532-
constant string, in order to disambiguate it from other classes of this type,
533-
and which could e.g. match the binding's name (in the example above, that would
534-
be `cares_wrap`).
531+
In the binding, call `SET_BINDING_ID()` with an identifier for the binding
532+
type. For example, for `http_parser::BindingData`, the identifier can be
533+
`http_parser_binding_data`.
534+
535+
If the binding should be supported in a snapshot, the id and the
536+
fully-specified class name should be added to the `SERIALIZABLE_BINDING_TYPES`
537+
list in `base_object_types.h`, and the class should implement the serialization
538+
and deserialization methods. See the comments of `SnapshotableObject` on how to
539+
implement them. Otherwise, add the id and the class name to the
540+
`UNSERIALIZABLE_BINDING_TYPES` list instead.
535541

536542
```cpp
543+
// In base_object_types.h, add the binding to either
544+
// UNSERIALIZABLE_BINDING_TYPES or SERIALIZABLE_BINDING_TYPES.
545+
// The second parameter is a descriptive name of the class, which is
546+
// usually the fully-specified class name.
547+
548+
#define UNSERIALIZABLE_BINDING_TYPES(V) \
549+
V(http_parser_binding_data, http_parser::BindingData)
550+
537551
// In the HTTP parser source code file:
538552
class BindingData : public BaseObject {
539553
public:
540554
BindingData(Realm* realm, Local<Object> obj) : BaseObject(realm, obj) {}
541555

542-
static constexpr FastStringKey type_name { "http_parser" };
556+
SET_BINDING_ID(http_parser_binding_data)
543557

544558
std::vector<char> parser_buffer;
545559
bool parser_buffer_in_use = false;
@@ -569,12 +583,6 @@ void InitializeHttpParser(Local<Object> target,
569583
}
570584
```
571585
572-
If the binding is loaded during bootstrap, add it to the
573-
`SERIALIZABLE_OBJECT_TYPES` list in `src/node_snapshotable.h` and
574-
inherit from the `SnapshotableObject` class instead. See the comments
575-
of `SnapshotableObject` on how to implement its serialization and
576-
deserialization.
577-
578586
<a id="exception-handling"></a>
579587
580588
### Exception handling
Collapse file

‎src/base_object.h‎

Copy file name to clipboardExpand all lines: src/base_object.h
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
2626

2727
#include <type_traits> // std::remove_reference
28+
#include "base_object_types.h"
2829
#include "memory_tracker.h"
2930
#include "v8.h"
3031

Collapse file

‎src/base_object_types.h‎

Copy file name to clipboardExpand all lines: src/base_object_types.h
-2Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ namespace node {
1010
// what the class passes to SET_BINDING_ID(), the second argument should match
1111
// the C++ class name.
1212
#define SERIALIZABLE_BINDING_TYPES(V) \
13-
V(encoding_binding_data, encoding_binding::BindingData) \
1413
V(fs_binding_data, fs::BindingData) \
1514
V(v8_binding_data, v8_utils::BindingData) \
1615
V(blob_binding_data, BlobBindingData) \
1716
V(process_binding_data, process::BindingData) \
18-
V(timers_binding_data, timers::BindingData) \
1917
V(url_binding_data, url::BindingData)
2018

2119
#define UNSERIALIZABLE_BINDING_TYPES(V) \
Collapse file

‎src/node_blob.h‎

Copy file name to clipboardExpand all lines: src/node_blob.h
+1-3Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,7 @@ class BlobBindingData : public SnapshotableObject {
147147

148148
SERIALIZABLE_OBJECT_METHODS()
149149

150-
static constexpr FastStringKey type_name{"node::BlobBindingData"};
151-
static constexpr EmbedderObjectType type_int =
152-
EmbedderObjectType::k_blob_binding_data;
150+
SET_BINDING_ID(blob_binding_data)
153151

154152
void MemoryInfo(MemoryTracker* tracker) const override;
155153
SET_SELF_SIZE(BlobBindingData)
Collapse file

‎src/node_file.h‎

Copy file name to clipboardExpand all lines: src/node_file.h
+1-3Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ class BindingData : public SnapshotableObject {
6969

7070
using InternalFieldInfo = InternalFieldInfoBase;
7171
SERIALIZABLE_OBJECT_METHODS()
72-
static constexpr FastStringKey type_name{"node::fs::BindingData"};
73-
static constexpr EmbedderObjectType type_int =
74-
EmbedderObjectType::k_fs_binding_data;
72+
SET_BINDING_ID(fs_binding_data)
7573

7674
void MemoryInfo(MemoryTracker* tracker) const override;
7775
SET_SELF_SIZE(BindingData)
Collapse file

‎src/node_http2_state.h‎

Copy file name to clipboardExpand all lines: src/node_http2_state.h
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class Http2State : public BaseObject {
125125
SET_SELF_SIZE(Http2State)
126126
SET_MEMORY_INFO_NAME(Http2State)
127127

128-
static constexpr FastStringKey type_name { "http2" };
128+
SET_BINDING_ID(http2_binding_data)
129129

130130
private:
131131
struct http2_state_internal {
Collapse file

‎src/node_http_parser.cc‎

Copy file name to clipboardExpand all lines: src/node_http_parser.cc
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class BindingData : public BaseObject {
9595
public:
9696
BindingData(Realm* realm, Local<Object> obj) : BaseObject(realm, obj) {}
9797

98-
static constexpr FastStringKey type_name { "http_parser" };
98+
SET_BINDING_ID(http_parser_binding_data)
9999

100100
std::vector<char> parser_buffer;
101101
bool parser_buffer_in_use = false;
Collapse file

‎src/node_process.h‎

Copy file name to clipboardExpand all lines: src/node_process.h
+1-3Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ class BindingData : public SnapshotableObject {
5454
using InternalFieldInfo = InternalFieldInfoBase;
5555

5656
SERIALIZABLE_OBJECT_METHODS()
57-
static constexpr FastStringKey type_name{"node::process::BindingData"};
58-
static constexpr EmbedderObjectType type_int =
59-
EmbedderObjectType::k_process_binding_data;
57+
SET_BINDING_ID(process_binding_data)
6058

6159
BindingData(Realm* realm, v8::Local<v8::Object> object);
6260

Collapse file

‎src/node_realm-inl.h‎

Copy file name to clipboardExpand all lines: src/node_realm-inl.h
+9-5Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ inline T* Realm::GetBindingData(v8::Local<v8::Context> context) {
6262
static_cast<BindingDataStore*>(context->GetAlignedPointerFromEmbedderData(
6363
ContextEmbedderIndex::kBindingDataStoreIndex));
6464
DCHECK_NOT_NULL(map);
65-
auto it = map->find(T::type_name);
66-
if (UNLIKELY(it == map->end())) return nullptr;
67-
T* result = static_cast<T*>(it->second.get());
65+
constexpr size_t binding_index = static_cast<size_t>(T::binding_type_int);
66+
static_assert(binding_index < std::tuple_size_v<BindingDataStore>);
67+
auto ptr = (*map)[binding_index];
68+
if (UNLIKELY(!ptr)) return nullptr;
69+
T* result = static_cast<T*>(ptr.get());
6870
DCHECK_NOT_NULL(result);
6971
DCHECK_EQ(result->realm(), GetCurrent(context));
7072
return result;
@@ -80,8 +82,10 @@ inline T* Realm::AddBindingData(v8::Local<v8::Context> context,
8082
static_cast<BindingDataStore*>(context->GetAlignedPointerFromEmbedderData(
8183
ContextEmbedderIndex::kBindingDataStoreIndex));
8284
DCHECK_NOT_NULL(map);
83-
auto result = map->emplace(T::type_name, item);
84-
CHECK(result.second);
85+
constexpr size_t binding_index = static_cast<size_t>(T::binding_type_int);
86+
static_assert(binding_index < std::tuple_size_v<BindingDataStore>);
87+
CHECK(!(*map)[binding_index]); // Should not insert the binding twice.
88+
(*map)[binding_index] = item;
8589
DCHECK_EQ(GetBindingData<T>(context), item.get());
8690
return item.get();
8791
}

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.