Skip to content

Commit

Permalink
SERVER-68281 Improve ValueGuard performance
Browse files Browse the repository at this point in the history
  • Loading branch information
parker-felix authored and Evergreen Agent committed Aug 10, 2022
1 parent 1a94ea9 commit 3f2f1e8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 55 deletions.
41 changes: 1 addition & 40 deletions src/mongo/db/exec/sbe/values/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ std::pair<TypeTags, Value> makeCopyIndexBounds(const IndexBounds& bounds) {
}


void releaseValue(TypeTags tag, Value val) noexcept {
void releaseValueDeep(TypeTags tag, Value val) noexcept {
switch (tag) {
case TypeTags::RecordId:
delete getRecordIdView(val);
Expand Down Expand Up @@ -393,45 +393,6 @@ BSONType tagToType(TypeTags tag) noexcept {
}
}

bool isShallowType(TypeTags tag) noexcept {
switch (tag) {
case TypeTags::Nothing:
case TypeTags::Null:
case TypeTags::NumberInt32:
case TypeTags::NumberInt64:
case TypeTags::NumberDouble:
case TypeTags::Date:
case TypeTags::Timestamp:
case TypeTags::Boolean:
case TypeTags::StringSmall:
case TypeTags::MinKey:
case TypeTags::MaxKey:
case TypeTags::bsonUndefined:
case TypeTags::LocalLambda:
return true;
case TypeTags::RecordId:
case TypeTags::NumberDecimal:
case TypeTags::StringBig:
case TypeTags::bsonString:
case TypeTags::bsonSymbol:
case TypeTags::Array:
case TypeTags::ArraySet:
case TypeTags::Object:
case TypeTags::ObjectId:
case TypeTags::bsonObjectId:
case TypeTags::bsonObject:
case TypeTags::bsonArray:
case TypeTags::bsonBinData:
case TypeTags::ksValue:
case TypeTags::bsonRegex:
case TypeTags::bsonJavascript:
case TypeTags::bsonDBPointer:
return false;
default:
MONGO_UNREACHABLE;
}
}

inline std::size_t hashObjectId(const uint8_t* objId) noexcept {
auto dataView = ConstDataView(reinterpret_cast<const char*>(objId));
return abslHash(dataView.read<LittleEndian<uint64_t>>()) ^
Expand Down
45 changes: 30 additions & 15 deletions src/mongo/db/exec/sbe/values/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ enum class TypeTags : uint8_t {
NumberInt32,
NumberInt64,
NumberDouble,
NumberDecimal,

// Date data types.
Date,
Expand All @@ -99,6 +98,15 @@ enum class TypeTags : uint8_t {
Boolean,
Null,
StringSmall,

MinKey,
MaxKey,

// Special marker
EndOfShallowValues = MaxKey,

// Heap values
NumberDecimal,
StringBig,
Array,
ArraySet,
Expand All @@ -107,9 +115,6 @@ enum class TypeTags : uint8_t {
ObjectId,
RecordId,

MinKey,
MaxKey,

// Raw bson values.
bsonObject,
bsonArray,
Expand Down Expand Up @@ -205,8 +210,11 @@ inline constexpr bool isCollatableType(TypeTags tag) noexcept {
return isString(tag) || isArray(tag) || isObject(tag);
}

inline constexpr bool isShallowType(TypeTags tag) noexcept {
return tag <= TypeTags::EndOfShallowValues;
}

BSONType tagToType(TypeTags tag) noexcept;
bool isShallowType(TypeTags tag) noexcept;

/**
* This function takes an SBE TypeTag, looks up the corresponding BSONType t, and then returns a
Expand All @@ -232,12 +240,28 @@ enum class SortDirection : uint8_t { Descending, Ascending };
/**
* Forward declarations.
*/
void releaseValue(TypeTags tag, Value val) noexcept;

/**
* Releases memory allocated for the value. If the value does not have any memory allocated for it,
* does nothing.
*
* NOTE: This function is intentionally marked as 'noexcept' and must not throw. It is used in the
* destructors of several classes to implement RAII concept for values.
*/
void releaseValueDeep(TypeTags tag, Value val) noexcept;
std::pair<TypeTags, Value> copyValue(TypeTags tag, Value val);
std::size_t hashValue(TypeTags tag,
Value val,
const CollatorInterface* collator = nullptr) noexcept;

inline void releaseValue(TypeTags tag, Value val) noexcept {
if (!isShallowType(tag)) {
releaseValueDeep(tag, val);
} else {
// No action is needed to release "shallow" values.
}
}

/**
* Overloads for writing values and tags to stream.
*/
Expand Down Expand Up @@ -1316,15 +1340,6 @@ std::pair<TypeTags, Value> makeCopyCollator(const CollatorInterface& collator);

std::pair<TypeTags, Value> makeCopyIndexBounds(const IndexBounds& collator);

/**
* Releases memory allocated for the value. If the value does not have any memory allocated for it,
* does nothing.
*
* NOTE: This function is intentionally marked as 'noexcept' and must not throw. It is used in the
* destructors of several classes to implement RAII concept for values.
*/
void releaseValue(TypeTags tag, Value val) noexcept;

inline std::pair<TypeTags, Value> copyValue(TypeTags tag, Value val) {
switch (tag) {
case TypeTags::RecordId:
Expand Down

0 comments on commit 3f2f1e8

Please sign in to comment.