Skip to content

Commit 09df8c0

Browse files
committed
Move timestampValue and oidValue into the Value union
This reduces sizeof(Value) by an additional 16 bytes on top of the 8 saved by moving dateValue into the union
1 parent 07e2bbe commit 09df8c0

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

src/mongo/bson/oid.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ namespace mongo {
4646
/** init from a 24 char hex string */
4747
explicit OID(const std::string &s) { init(s); }
4848

49+
/** init from a reference to a 12-byte array */
50+
explicit OID(const unsigned char (&arr)[12]) {
51+
memcpy(data, arr, 12);
52+
}
53+
4954
/** initialize to 'null' */
5055
void clear() { a = 0; b = 0; }
5156

src/mongo/db/pipeline/value.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ namespace mongo {
6969
break;
7070

7171
case Timestamp:
72-
timestampValue = OpTime();
72+
timestampValue = 0;
7373
break;
7474

7575
default:
@@ -136,7 +136,8 @@ namespace mongo {
136136
}
137137

138138
case jstOID:
139-
oidValue = pBsonElement->OID();
139+
BOOST_STATIC_ASSERT(sizeof(oidValue) == sizeof(OID));
140+
memcpy(oidValue, pBsonElement->OID().getData(), sizeof(oidValue));
140141
break;
141142

142143
case Bool:
@@ -158,7 +159,8 @@ namespace mongo {
158159
break;
159160

160161
case Timestamp:
161-
timestampValue = pBsonElement->_opTime();
162+
// asDate is a poorly named function that returns a ReplTime
163+
timestampValue = pBsonElement->_opTime().asDate();
162164
break;
163165

164166
case NumberLong:
@@ -232,12 +234,10 @@ namespace mongo {
232234
return pValue;
233235
}
234236

235-
Value::Value(const OpTime& value):
236-
type(Timestamp),
237-
pDocumentValue(),
238-
vpValue() {
239-
timestampValue = value;
240-
}
237+
Value::Value(const OpTime& value)
238+
: type(Timestamp)
239+
, timestampValue(value.asDate())
240+
{}
241241

242242
intrusive_ptr<const Value> Value::createTimestamp(const OpTime& value) {
243243
intrusive_ptr<const Value> pValue(new Value(value));
@@ -332,7 +332,7 @@ namespace mongo {
332332

333333
OID Value::getOid() const {
334334
verify(getType() == jstOID);
335-
return oidValue;
335+
return OID(oidValue);
336336
}
337337

338338
bool Value::getBool() const {
@@ -616,7 +616,7 @@ namespace mongo {
616616
return dateValue;
617617

618618
case Timestamp:
619-
return timestampValue.getSecs() * 1000LL;
619+
return getTimestamp().getSecs() * 1000LL;
620620

621621
default:
622622
uassert(16006, str::stream() <<
@@ -687,7 +687,7 @@ namespace mongo {
687687
return stringValue;
688688

689689
case Timestamp:
690-
ss << timestampValue.toStringPretty();
690+
ss << getTimestamp().toStringPretty();
691691
return ss.str();
692692

693693
case Date:
@@ -858,9 +858,9 @@ namespace mongo {
858858
break;
859859

860860
case jstOID:
861-
if (rL->oidValue < rR->oidValue)
861+
if (rL->getOid() < rR->getOid())
862862
return -1;
863-
if (rL->oidValue == rR->oidValue)
863+
if (rL->getOid() == rR->getOid())
864864
return 0;
865865
return 1;
866866

@@ -958,7 +958,7 @@ namespace mongo {
958958
break;
959959

960960
case jstOID:
961-
oidValue.hash_combine(seed);
961+
getOid().hash_combine(seed);
962962
break;
963963

964964
case Bool:
@@ -974,7 +974,7 @@ namespace mongo {
974974
break;
975975

976976
case Timestamp:
977-
boost::hash_combine(seed, timestampValue.asLL());
977+
boost::hash_combine(seed, timestampValue);
978978
break;
979979

980980
case Undefined:

src/mongo/db/pipeline/value.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,16 +331,16 @@ namespace mongo {
331331
bool boolValue;
332332
int intValue;
333333
long long longValue;
334-
// The members below are redundant, but useful for clarity and searchability.
334+
ReplTime timestampValue;
335+
unsigned char oidValue[12];
336+
// The member below is redundant, but useful for clarity and searchability.
335337
long long dateValue;
336338
};
337-
OID oidValue;
339+
338340
string stringValue; // String, Regex, Symbol
339-
OpTime timestampValue;
340341
intrusive_ptr<Document> pDocumentValue;
341342
vector<intrusive_ptr<const Value> > vpValue; // for arrays
342343

343-
344344
/*
345345
These are often used as the result of boolean or comparison
346346
expressions.

0 commit comments

Comments
 (0)