Skip to content

Commit

Permalink
Shrink sizeof Value struct (vesoft-inc#540)
Browse files Browse the repository at this point in the history
* Shrink sizeof Value struct

* suppress warnings

* suppress warnings

Co-authored-by: Yee <[email protected]>
  • Loading branch information
dutor and yixinglu authored May 26, 2021
1 parent 9bc1da9 commit 7d0cfe0
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 38 deletions.
20 changes: 10 additions & 10 deletions src/common/datatypes/Date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,15 @@ std::string Time::toString() const {

std::string DateTime::toString() const {
// It's in current timezone already
return folly::stringPrintf(
"%d-%02d-%02dT%02d:%02d:%02d.%d", year, month, day, hour, minute, sec, microsec);
return folly::stringPrintf("%hd-%02hhu-%02hhu"
"T%02hhu:%02hhu:%02hhu.%u",
static_cast<int16_t>(year),
static_cast<uint8_t>(month),
static_cast<uint8_t>(day),
static_cast<uint8_t>(hour),
static_cast<uint8_t>(minute),
static_cast<uint8_t>(sec),
static_cast<uint32_t>(microsec));
}

} // namespace nebula
Expand All @@ -131,14 +138,7 @@ std::size_t hash<nebula::Time>::operator()(const nebula::Time& h) const noexcept
}

std::size_t hash<nebula::DateTime>::operator()(const nebula::DateTime& h) const noexcept {
size_t hv = folly::hash::fnv64_buf(reinterpret_cast<const void*>(&h.year), sizeof(h.year));
hv = folly::hash::fnv64_buf(reinterpret_cast<const void*>(&h.month), sizeof(h.month), hv);
hv = folly::hash::fnv64_buf(reinterpret_cast<const void*>(&h.day), sizeof(h.day), hv);
hv = folly::hash::fnv64_buf(reinterpret_cast<const void*>(&h.hour), sizeof(h.hour), hv);
hv = folly::hash::fnv64_buf(reinterpret_cast<const void*>(&h.minute), sizeof(h.minute), hv);
hv = folly::hash::fnv64_buf(reinterpret_cast<const void*>(&h.sec), sizeof(h.sec), hv);
return folly::hash::fnv64_buf(
reinterpret_cast<const void*>(&h.microsec), sizeof(h.microsec), hv);
return h.qword;
}

} // namespace std
49 changes: 37 additions & 12 deletions src/common/datatypes/Date.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,45 @@ inline std::ostream &operator<<(std::ostream& os, const Time& d) {
}

struct DateTime {
int16_t year;
int8_t month;
int8_t day;
int8_t hour;
int8_t minute;
int8_t sec;
int32_t microsec;
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#endif // defined(__GNUC__)
union {
struct {
int64_t year:16;
uint64_t month:4;
uint64_t day:5;
uint64_t hour:5;
uint64_t minute:6;
uint64_t sec:6;
uint64_t microsec:22;
};
uint64_t qword;
};
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif // defined(__GNUC__)

DateTime() : year{0}, month{1}, day{1}, hour{0}, minute{0}, sec{0}, microsec{0} {}
DateTime(int16_t y, int8_t m, int8_t d, int8_t h, int8_t min, int8_t s, int32_t us)
: year{y}, month{m}, day{d}, hour{h}, minute{min}, sec{s}, microsec{us} {}
explicit DateTime(const Date &date)
: year{date.year}, month{date.month}, day{date.day},
hour{0}, minute{0}, sec{0}, microsec{0} {}
DateTime(int16_t y, int8_t m, int8_t d, int8_t h, int8_t min, int8_t s, int32_t us) {
year = y;
month = m;
day = d;
hour = h;
minute = min;
sec = s;
microsec = us;
}
explicit DateTime(const Date &date) {
year = date.year;
month = date.month;
day = date.day;
hour = 0;
minute = 0;
sec = 0;
microsec = 0;
}

void clear() {
year = 0;
Expand Down
28 changes: 21 additions & 7 deletions src/common/datatypes/DateOps.inl
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,9 @@ void Cpp2Ops<nebula::DateTime>::read(Protocol* proto, nebula::DateTime* obj) {

_readField_year:
{
detail::pm::protocol_methods<type_class::integral, int16_t>::read(*proto, obj->year);
int16_t year;
detail::pm::protocol_methods<type_class::integral, int16_t>::read(*proto, year);
obj->year = year;
}

if (UNLIKELY(!readState.advanceToNextField(proto, 1, 2, protocol::T_BYTE))) {
Expand All @@ -560,7 +562,9 @@ _readField_year:

_readField_month:
{
detail::pm::protocol_methods<type_class::integral, int8_t>::read(*proto, obj->month);
int8_t month;
detail::pm::protocol_methods<type_class::integral, int8_t>::read(*proto, month);
obj->month = month;
}

if (UNLIKELY(!readState.advanceToNextField(proto, 2, 3, protocol::T_BYTE))) {
Expand All @@ -569,7 +573,9 @@ _readField_month:

_readField_day:
{
detail::pm::protocol_methods<type_class::integral, int8_t>::read(*proto, obj->day);
int8_t day;
detail::pm::protocol_methods<type_class::integral, int8_t>::read(*proto, day);
obj->day = day;
}

if (UNLIKELY(!readState.advanceToNextField(proto, 3, 4, protocol::T_BYTE))) {
Expand All @@ -578,7 +584,9 @@ _readField_day:

_readField_hour:
{
detail::pm::protocol_methods<type_class::integral, int8_t>::read(*proto, obj->hour);
int8_t hour;
detail::pm::protocol_methods<type_class::integral, int8_t>::read(*proto, hour);
obj->hour = hour;
}

if (UNLIKELY(!readState.advanceToNextField(proto, 4, 5, protocol::T_BYTE))) {
Expand All @@ -587,7 +595,9 @@ _readField_hour:

_readField_minute:
{
detail::pm::protocol_methods<type_class::integral, int8_t>::read(*proto, obj->minute);
int8_t minute;
detail::pm::protocol_methods<type_class::integral, int8_t>::read(*proto, minute);
obj->minute = minute;
}

if (UNLIKELY(!readState.advanceToNextField(proto, 5, 6, protocol::T_BYTE))) {
Expand All @@ -596,7 +606,9 @@ _readField_minute:

_readField_sec:
{
detail::pm::protocol_methods<type_class::integral, int8_t>::read(*proto, obj->sec);
int8_t sec;
detail::pm::protocol_methods<type_class::integral, int8_t>::read(*proto, sec);
obj->sec = sec;
}

if (UNLIKELY(!readState.advanceToNextField(proto, 6, 7, protocol::T_I32))) {
Expand All @@ -605,8 +617,10 @@ _readField_sec:

_readField_microsec:
{
int32_t microsec;
detail::pm::protocol_methods<type_class::integral, int32_t>
::read(*proto, obj->microsec);
::read(*proto, microsec);
obj->microsec = microsec;
}

if (UNLIKELY(!readState.advanceToNextField(proto, 7, 8, protocol::T_I32))) {
Expand Down
21 changes: 13 additions & 8 deletions src/common/datatypes/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ Value::Value(const Value& rhs) : type_(Value::Type::__EMPTY__) {
}
case Type::STRING:
{
setS(rhs.value_.sVal);
setS(*rhs.value_.sVal);
break;
}
case Type::DATE:
Expand Down Expand Up @@ -713,7 +713,7 @@ const double& Value::getFloat() const {

const std::string& Value::getStr() const {
CHECK_EQ(type_, Type::STRING);
return value_.sVal;
return *value_.sVal;
}

const Date& Value::getDate() const {
Expand Down Expand Up @@ -824,7 +824,7 @@ double& Value::mutableFloat() {

std::string& Value::mutableStr() {
CHECK_EQ(type_, Type::STRING);
return value_.sVal;
return *value_.sVal;
}

Date& Value::mutableDate() {
Expand Down Expand Up @@ -908,7 +908,7 @@ double Value::moveFloat() {

std::string Value::moveStr() {
CHECK_EQ(type_, Type::STRING);
std::string v = std::move(value_.sVal);
std::string v = std::move(*value_.sVal);
clear();
return v;
}
Expand Down Expand Up @@ -1187,7 +1187,7 @@ Value& Value::operator=(const Value& rhs) {
}
case Type::STRING:
{
setS(rhs.value_.sVal);
setS(*rhs.value_.sVal);
break;
}
case Type::DATE:
Expand Down Expand Up @@ -1291,19 +1291,24 @@ void Value::setF(double&& v) {
new (std::addressof(value_.fVal)) double(std::move(v)); // NOLINT
}

void Value::setS(std::unique_ptr<std::string> v) {
type_ = Type::STRING;
new (std::addressof(value_.sVal)) std::unique_ptr<std::string>(std::move(v));
}

void Value::setS(const std::string& v) {
type_ = Type::STRING;
new (std::addressof(value_.sVal)) std::string(v);
new (std::addressof(value_.sVal)) std::unique_ptr<std::string>(new std::string(v));
}

void Value::setS(std::string&& v) {
type_ = Type::STRING;
new (std::addressof(value_.sVal)) std::string(std::move(v));
new (std::addressof(value_.sVal)) std::unique_ptr<std::string>(new std::string(std::move(v)));
}

void Value::setS(const char* v) {
type_ = Type::STRING;
new (std::addressof(value_.sVal)) std::string(v);
new (std::addressof(value_.sVal)) std::unique_ptr<std::string>(new std::string(v));
}

void Value::setD(const Date& v) {
Expand Down
5 changes: 4 additions & 1 deletion src/common/datatypes/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ struct Value {
bool bVal;
int64_t iVal;
double fVal;
std::string sVal;
std::unique_ptr<std::string>sVal;
Date dVal;
Time tVal;
DateTime dtVal;
Expand Down Expand Up @@ -368,6 +368,7 @@ struct Value {
void setS(const std::string& v);
void setS(std::string&& v);
void setS(const char* v);
void setS(std::unique_ptr<std::string> v);
// Date value
void setD(const Date& v);
void setD(Date&& v);
Expand Down Expand Up @@ -414,6 +415,8 @@ struct Value {
void setG(DataSet&& v);
};

static_assert(sizeof(Value) == 16UL);

void swap(Value& a, Value& b);

constexpr auto kEpsilon = 1e-8;
Expand Down

0 comments on commit 7d0cfe0

Please sign in to comment.