Skip to content

Commit 62a0e91

Browse 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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent edbe38d commit 62a0e91

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

src/node_union_bytes.h

+8-15
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)