Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 62a0e91

Browse filesBrowse files
bnoordhuistargos
authored andcommitted
src: simplify UnionBytes
Before this commit it was using a tagged union to store the one-byte and two-byte pointers. From a `sizeof(UnionBytes)` perspective that makes no difference - there is a hole between the tag and the union - and it makes the code just a little harder to reason about, IMO. PR-URL: #29116 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent edbe38d commit 62a0e91
Copy full SHA for 62a0e91

File tree

Expand file treeCollapse file tree

1 file changed

+8
-15
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+8
-15
lines changed
Open diff view settings
Collapse file

‎src/node_union_bytes.h‎

Copy file name to clipboardExpand all lines: src/node_union_bytes.h
+8-15Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,47 +59,40 @@ class NonOwningExternalTwoByteResource
5959
class UnionBytes {
6060
public:
6161
UnionBytes(const uint16_t* data, size_t length)
62-
: is_one_byte_(false), two_bytes_(data), length_(length) {}
62+
: one_bytes_(nullptr), two_bytes_(data), length_(length) {}
6363
UnionBytes(const uint8_t* data, size_t length)
64-
: is_one_byte_(true), one_bytes_(data), length_(length) {}
64+
: one_bytes_(data), two_bytes_(nullptr), length_(length) {}
6565

6666
UnionBytes(const UnionBytes&) = default;
6767
UnionBytes& operator=(const UnionBytes&) = default;
6868
UnionBytes(UnionBytes&&) = default;
6969
UnionBytes& operator=(UnionBytes&&) = default;
7070

71-
bool is_one_byte() const { return is_one_byte_; }
71+
bool is_one_byte() const { return one_bytes_ != nullptr; }
7272
const uint16_t* two_bytes_data() const {
73-
CHECK(!is_one_byte_);
7473
CHECK_NOT_NULL(two_bytes_);
7574
return two_bytes_;
7675
}
7776
const uint8_t* one_bytes_data() const {
78-
CHECK(is_one_byte_);
7977
CHECK_NOT_NULL(one_bytes_);
8078
return one_bytes_;
8179
}
8280
v8::Local<v8::String> ToStringChecked(v8::Isolate* isolate) const {
83-
if (is_one_byte_) {
84-
CHECK_NOT_NULL(one_bytes_);
81+
if (is_one_byte()) {
8582
NonOwningExternalOneByteResource* source =
86-
new NonOwningExternalOneByteResource(one_bytes_, length_);
83+
new NonOwningExternalOneByteResource(one_bytes_data(), length_);
8784
return v8::String::NewExternalOneByte(isolate, source).ToLocalChecked();
8885
} else {
89-
CHECK_NOT_NULL(two_bytes_);
9086
NonOwningExternalTwoByteResource* source =
91-
new NonOwningExternalTwoByteResource(two_bytes_, length_);
87+
new NonOwningExternalTwoByteResource(two_bytes_data(), length_);
9288
return v8::String::NewExternalTwoByte(isolate, source).ToLocalChecked();
9389
}
9490
}
9591
size_t length() { return length_; }
9692

9793
private:
98-
bool is_one_byte_;
99-
union {
100-
const uint8_t* one_bytes_;
101-
const uint16_t* two_bytes_;
102-
};
94+
const uint8_t* one_bytes_;
95+
const uint16_t* two_bytes_;
10396
size_t length_;
10497
};
10598

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.