From 841df6a9b68f431129343c49fc9bb6ed4d83f89b Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 14 Aug 2019 12:06:43 +0200 Subject: 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: https://github.com/nodejs/node/pull/29116 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Rich Trott --- src/node_union_bytes.h | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/node_union_bytes.h b/src/node_union_bytes.h index 996d9880a4..d296e67f00 100644 --- a/src/node_union_bytes.h +++ b/src/node_union_bytes.h @@ -59,47 +59,40 @@ class NonOwningExternalTwoByteResource class UnionBytes { public: UnionBytes(const uint16_t* data, size_t length) - : is_one_byte_(false), two_bytes_(data), length_(length) {} + : one_bytes_(nullptr), two_bytes_(data), length_(length) {} UnionBytes(const uint8_t* data, size_t length) - : is_one_byte_(true), one_bytes_(data), length_(length) {} + : one_bytes_(data), two_bytes_(nullptr), length_(length) {} UnionBytes(const UnionBytes&) = default; UnionBytes& operator=(const UnionBytes&) = default; UnionBytes(UnionBytes&&) = default; UnionBytes& operator=(UnionBytes&&) = default; - bool is_one_byte() const { return is_one_byte_; } + bool is_one_byte() const { return one_bytes_ != nullptr; } const uint16_t* two_bytes_data() const { - CHECK(!is_one_byte_); CHECK_NOT_NULL(two_bytes_); return two_bytes_; } const uint8_t* one_bytes_data() const { - CHECK(is_one_byte_); CHECK_NOT_NULL(one_bytes_); return one_bytes_; } v8::Local ToStringChecked(v8::Isolate* isolate) const { - if (is_one_byte_) { - CHECK_NOT_NULL(one_bytes_); + if (is_one_byte()) { NonOwningExternalOneByteResource* source = - new NonOwningExternalOneByteResource(one_bytes_, length_); + new NonOwningExternalOneByteResource(one_bytes_data(), length_); return v8::String::NewExternalOneByte(isolate, source).ToLocalChecked(); } else { - CHECK_NOT_NULL(two_bytes_); NonOwningExternalTwoByteResource* source = - new NonOwningExternalTwoByteResource(two_bytes_, length_); + new NonOwningExternalTwoByteResource(two_bytes_data(), length_); return v8::String::NewExternalTwoByte(isolate, source).ToLocalChecked(); } } size_t length() { return length_; } private: - bool is_one_byte_; - union { - const uint8_t* one_bytes_; - const uint16_t* two_bytes_; - }; + const uint8_t* one_bytes_; + const uint16_t* two_bytes_; size_t length_; }; -- cgit v1.2.3