diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2020-11-18 16:47:50 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2020-11-18 18:22:16 +0000 |
commit | 78bfefbf3fd11a655560c7237bf4106aca2458b2 (patch) | |
tree | 77573fff526bf8f4842fc7a2a5dc0d2af0b2db3c /vespalib | |
parent | eb28759a307ff298e2125d6d3deff260c941ec45 (diff) |
Use a std:.unique_ptr to make Trace a thin wrapper for TraceNode to make the happy path fast.
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/tests/trace/trace.cpp | 20 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/trace/trace.cpp | 32 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/trace/trace.h | 26 |
3 files changed, 52 insertions, 26 deletions
diff --git a/vespalib/src/tests/trace/trace.cpp b/vespalib/src/tests/trace/trace.cpp index 9c8660fe395..46ebaf95114 100644 --- a/vespalib/src/tests/trace/trace.cpp +++ b/vespalib/src/tests/trace/trace.cpp @@ -146,25 +146,25 @@ TEST("testTraceLevel") t.setLevel(4); EXPECT_EQUAL(4u, t.getLevel()); t.trace(9, "no"); - EXPECT_EQUAL(0u, t.getRoot().getNumChildren()); + EXPECT_EQUAL(0u, t.getNumChildren()); t.trace(8, "no"); - EXPECT_EQUAL(0u, t.getRoot().getNumChildren()); + EXPECT_EQUAL(0u, t.getNumChildren()); t.trace(7, "no"); - EXPECT_EQUAL(0u, t.getRoot().getNumChildren()); + EXPECT_EQUAL(0u, t.getNumChildren()); t.trace(6, "no"); - EXPECT_EQUAL(0u, t.getRoot().getNumChildren()); + EXPECT_EQUAL(0u, t.getNumChildren()); t.trace(5, "no"); - EXPECT_EQUAL(0u, t.getRoot().getNumChildren()); + EXPECT_EQUAL(0u, t.getNumChildren()); t.trace(4, "yes"); - EXPECT_EQUAL(1u, t.getRoot().getNumChildren()); + EXPECT_EQUAL(1u, t.getNumChildren()); t.trace(3, "yes"); - EXPECT_EQUAL(2u, t.getRoot().getNumChildren()); + EXPECT_EQUAL(2u, t.getNumChildren()); t.trace(2, "yes"); - EXPECT_EQUAL(3u, t.getRoot().getNumChildren()); + EXPECT_EQUAL(3u, t.getNumChildren()); t.trace(1, "yes"); - EXPECT_EQUAL(4u, t.getRoot().getNumChildren()); + EXPECT_EQUAL(4u, t.getNumChildren()); t.trace(0, "yes"); - EXPECT_EQUAL(5u, t.getRoot().getNumChildren()); + EXPECT_EQUAL(5u, t.getNumChildren()); } TEST("testCompact") diff --git a/vespalib/src/vespa/vespalib/trace/trace.cpp b/vespalib/src/vespa/vespalib/trace/trace.cpp index 2a9afc9b36b..ea93f574ed0 100644 --- a/vespalib/src/vespa/vespalib/trace/trace.cpp +++ b/vespalib/src/vespa/vespalib/trace/trace.cpp @@ -6,6 +6,15 @@ namespace vespalib { +Trace::Trace(const Trace &rhs) + : _root(), + _level(rhs._level) +{ + if (!rhs.isEmpty()) { + _root = std::make_unique<TraceNode>(rhs.getRoot()); + } +} + bool Trace::trace(uint32_t level, const string ¬e, bool addTime) { @@ -15,11 +24,30 @@ Trace::trace(uint32_t level, const string ¬e, bool addTime) if (addTime) { struct timeval tv; gettimeofday(&tv, nullptr); - _root.addChild(make_string("[%ld.%06ld] %s", tv.tv_sec, static_cast<long>(tv.tv_usec), note.c_str())); + ensureRoot().addChild(make_string("[%ld.%06ld] %s", tv.tv_sec, static_cast<long>(tv.tv_usec), note.c_str())); } else { - _root.addChild(note); + ensureRoot().addChild(note); } return true; } +string +Trace::toString() const { + return _root ? _root->toString(31337) : ""; +} + +void +Trace::clear() { + _level = 0; + _root.reset(); +} + +TraceNode & +Trace::ensureRoot() { + if (!_root) { + _root = std::make_unique<TraceNode>(); + } + return *_root; +} + } // namespace vespalib diff --git a/vespalib/src/vespa/vespalib/trace/trace.h b/vespalib/src/vespa/vespalib/trace/trace.h index 5be9b662c89..1300f121404 100644 --- a/vespalib/src/vespa/vespalib/trace/trace.h +++ b/vespalib/src/vespa/vespalib/trace/trace.h @@ -27,7 +27,7 @@ public: explicit Trace(uint32_t level) : _root(), _level(level) { } Trace & operator = (Trace &&) = default; Trace(Trace &&) = default; - Trace(const Trace &) = default; + Trace(const Trace &); Trace & operator = (const Trace &) = delete; ~Trace() = default; @@ -36,11 +36,7 @@ public: * * @return This, to allow chaining. */ - Trace &clear() { - _level = 0; - _root.clear(); - return *this; - } + void clear(); /** * Swap the internals of this with another. @@ -97,7 +93,9 @@ public: bool trace(uint32_t level, const string ¬e, bool addTime = true); void normalize() { - _root.normalize(); + if (_root) { + _root->normalize(); + } } /** @@ -115,7 +113,7 @@ public: } void addChild(Trace && child) { if (!child.isEmpty()) { - addChild(std::move(child._root)); + addChild(std::move(*child._root)); } } @@ -124,11 +122,11 @@ public: * * @return The root. */ - const TraceNode &getRoot() const { return _root; } + const TraceNode &getRoot() const { return *_root; } - bool isEmpty() const { return _root.isEmpty(); } + bool isEmpty() const { return !_root || _root->isEmpty(); } - uint32_t getNumChildren() const { return _root.getNumChildren(); } + uint32_t getNumChildren() const { return _root ? _root->getNumChildren() : 0; } const TraceNode & getChild(uint32_t child) const { return getRoot().getChild(child); } /** @@ -137,11 +135,11 @@ public: * * @return Readable trace string. */ - string toString() const { return _root.toString(31337); } + string toString() const; private: - TraceNode &ensureRoot() { return _root; } + TraceNode &ensureRoot(); - TraceNode _root; + std::unique_ptr<TraceNode> _root; uint32_t _level; }; |