diff options
author | James Wahlin <james.wahlin@10gen.com> | 2016-12-19 14:02:02 -0500 |
---|---|---|
committer | James Wahlin <james.wahlin@10gen.com> | 2016-12-27 16:22:21 -0500 |
commit | ac7b0469bdb57a3593dc8b97e5a7045db0efbb24 (patch) | |
tree | c759fe0f6c4dff381ac08944f603851db6492c16 /src | |
parent | 72112a72ec3ee48cb883fc02b2904079d11f4954 (diff) | |
download | mongo-ac7b0469bdb57a3593dc8b97e5a7045db0efbb24.tar.gz |
SERVER-27175 Improve performance of planSummary string generation
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/db/keypattern.cpp | 30 | ||||
-rw-r--r-- | src/mongo/db/keypattern.h | 8 | ||||
-rw-r--r-- | src/mongo/db/query/explain.cpp | 34 |
3 files changed, 57 insertions, 15 deletions
diff --git a/src/mongo/db/keypattern.cpp b/src/mongo/db/keypattern.cpp index 2c1bf09497d..ffc2a1f8e71 100644 --- a/src/mongo/db/keypattern.cpp +++ b/src/mongo/db/keypattern.cpp @@ -31,7 +31,6 @@ #include "mongo/db/keypattern.h" #include "mongo/db/index_names.h" -#include "mongo/util/mongoutils/str.h" namespace mongo { @@ -55,6 +54,35 @@ bool KeyPattern::isHashedKeyPattern(const BSONObj& pattern) { return IndexNames::HASHED == IndexNames::findPluginName(pattern); } +StringBuilder& operator<<(StringBuilder& sb, const KeyPattern& keyPattern) { + // Rather than return BSONObj::toString() we construct a keyPattern string manually. This allows + // us to avoid the cost of writing numeric direction to the str::stream which will then undergo + // expensive number to string conversion. + sb << "{ "; + + bool first = true; + for (auto&& elem : keyPattern._pattern) { + if (first) { + first = false; + } else { + sb << ", "; + } + + if (mongo::String == elem.type()) { + sb << elem; + } else if (elem.number() >= 0) { + // The canonical check as to whether a key pattern element is "ascending" or + // "descending" is (elem.number() >= 0). This is defined by the Ordering class. + sb << elem.fieldNameStringData() << ": 1"; + } else { + sb << elem.fieldNameStringData() << ": -1"; + } + } + + sb << " }"; + return sb; +} + BSONObj KeyPattern::extendRangeBound(const BSONObj& bound, bool makeUpperInclusive) const { BSONObjBuilder newBound(bound.objsize()); diff --git a/src/mongo/db/keypattern.h b/src/mongo/db/keypattern.h index f9ac83983e3..cfa24da5df2 100644 --- a/src/mongo/db/keypattern.h +++ b/src/mongo/db/keypattern.h @@ -29,7 +29,9 @@ #pragma once #include "mongo/base/string_data.h" +#include "mongo/bson/util/builder.h" #include "mongo/db/jsobj.h" +#include "mongo/util/mongoutils/str.h" namespace mongo { @@ -80,9 +82,13 @@ public: * Returns a string representation of this KeyPattern */ std::string toString() const { - return toBSON().toString(); + return str::stream() << *this; } + /** + * Writes to 'sb' a string representation of this KeyPattern. + */ + friend StringBuilder& operator<<(StringBuilder& sb, const KeyPattern& keyPattern); /* Takes a BSONObj whose field names are a prefix of the fields in this keyPattern, and * outputs a new bound with MinKey values appended to match the fields in this keyPattern diff --git a/src/mongo/db/query/explain.cpp b/src/mongo/db/query/explain.cpp index 929950dc38a..7d83021dbd5 100644 --- a/src/mongo/db/query/explain.cpp +++ b/src/mongo/db/query/explain.cpp @@ -31,6 +31,7 @@ #include "mongo/db/query/explain.h" #include "mongo/base/owned_pointer_vector.h" +#include "mongo/bson/util/builder.h" #include "mongo/db/exec/cached_plan.h" #include "mongo/db/exec/count_scan.h" #include "mongo/db/exec/distinct_scan.h" @@ -41,6 +42,7 @@ #include "mongo/db/exec/pipeline_proxy.h" #include "mongo/db/exec/text.h" #include "mongo/db/exec/working_set_common.h" +#include "mongo/db/keypattern.h" #include "mongo/db/query/get_executor.h" #include "mongo/db/query/plan_executor.h" #include "mongo/db/query/plan_summary_stats.h" @@ -166,33 +168,39 @@ size_t getDocsExamined(StageType type, const SpecificStats* specific) { } /** - * Adds to the plan summary string being built by 'ss' for the execution stage 'stage'. + * Adds to the plan summary string being built by 'sb' for the execution stage 'stage'. */ -void addStageSummaryStr(const PlanStage* stage, mongoutils::str::stream& ss) { +void addStageSummaryStr(const PlanStage* stage, StringBuilder& sb) { // First add the stage type string. const CommonStats* common = stage->getCommonStats(); - ss << common->stageTypeStr; + sb << common->stageTypeStr; // Some leaf nodes also provide info about the index they used. const SpecificStats* specific = stage->getSpecificStats(); if (STAGE_COUNT_SCAN == stage->stageType()) { const CountScanStats* spec = static_cast<const CountScanStats*>(specific); - ss << " " << spec->keyPattern; + const KeyPattern keyPattern{spec->keyPattern}; + sb << " " << keyPattern; } else if (STAGE_DISTINCT_SCAN == stage->stageType()) { const DistinctScanStats* spec = static_cast<const DistinctScanStats*>(specific); - ss << " " << spec->keyPattern; + const KeyPattern keyPattern{spec->keyPattern}; + sb << " " << keyPattern; } else if (STAGE_GEO_NEAR_2D == stage->stageType()) { const NearStats* spec = static_cast<const NearStats*>(specific); - ss << " " << spec->keyPattern; + const KeyPattern keyPattern{spec->keyPattern}; + sb << " " << keyPattern; } else if (STAGE_GEO_NEAR_2DSPHERE == stage->stageType()) { const NearStats* spec = static_cast<const NearStats*>(specific); - ss << " " << spec->keyPattern; + const KeyPattern keyPattern{spec->keyPattern}; + sb << " " << keyPattern; } else if (STAGE_IXSCAN == stage->stageType()) { const IndexScanStats* spec = static_cast<const IndexScanStats*>(specific); - ss << " " << spec->keyPattern; + const KeyPattern keyPattern{spec->keyPattern}; + sb << " " << keyPattern; } else if (STAGE_TEXT == stage->stageType()) { const TextStats* spec = static_cast<const TextStats*>(specific); - ss << " " << spec->indexPrefix; + const KeyPattern keyPattern{spec->indexPrefix}; + sb << " " << keyPattern; } } @@ -801,7 +809,7 @@ std::string Explain::getPlanSummary(const PlanStage* root) { flattenExecTree(root, &stages); // Use this stream to build the plan summary string. - mongoutils::str::stream ss; + StringBuilder sb; bool seenLeaf = false; for (size_t i = 0; i < stages.size(); i++) { @@ -809,15 +817,15 @@ std::string Explain::getPlanSummary(const PlanStage* root) { // This is a leaf node. Add to the plan summary string accordingly. Unless // this is the first leaf we've seen, add a delimiting string first. if (seenLeaf) { - ss << ", "; + sb << ", "; } else { seenLeaf = true; } - addStageSummaryStr(stages[i], ss); + addStageSummaryStr(stages[i], sb); } } - return ss; + return sb.str(); } // static |