Skip to content

Commit 013cbac

Browse files
committed
performance optimisation
Signed-off-by: JCW <[email protected]>
1 parent 52becff commit 013cbac

File tree

4 files changed

+188
-192
lines changed

4 files changed

+188
-192
lines changed

include/xrpl/basics/Log.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,10 @@ class Logs
156156
private:
157157
std::unique_ptr<std::ofstream> m_stream;
158158
boost::filesystem::path m_path;
159+
std::mutex mutable fileMutex_;
159160
};
160161

161-
std::mutex mutable mutex_;
162+
std::mutex mutable sinkSetMutex_;
162163
std::map<
163164
std::string,
164165
std::unique_ptr<beast::Journal::Sink>,
@@ -205,7 +206,7 @@ class Logs
205206
write(
206207
beast::severities::Severity level,
207208
std::string const& partition,
208-
std::string const& text,
209+
std::string text,
209210
bool console);
210211

211212
std::string
@@ -243,7 +244,7 @@ class Logs
243244
static void
244245
format(
245246
std::string& output,
246-
std::string const& message,
247+
std::string message,
247248
beast::severities::Severity severity,
248249
std::string const& partition);
249250

include/xrpl/beast/utility/Journal.h

Lines changed: 65 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ class Journal
144144
void
145145
setModuleName(std::string const& name);
146146

147-
[[nodiscard]] static JsonLogAttributes
148-
combine(AttributeFields const& a, AttributeFields const& b);
147+
void
148+
combine(AttributeFields const& from);
149149

150150
AttributeFields&
151151
contextValues()
@@ -172,22 +172,37 @@ class Journal
172172
friend class Journal;
173173
};
174174

175-
struct JsonLogContext
175+
class JsonLogContext
176176
{
177-
std::source_location location = {};
178-
rapidjson::Value messageParams;
179-
rapidjson::MemoryPoolAllocator<> allocator;
177+
rapidjson::Value attributes_;
178+
rapidjson::MemoryPoolAllocator<> allocator_;
180179

180+
public:
181181
JsonLogContext() = default;
182182

183-
void
184-
reset(std::source_location location_) noexcept
183+
rapidjson::MemoryPoolAllocator<>&
184+
allocator()
185+
{
186+
return allocator_;
187+
}
188+
189+
rapidjson::Value&
190+
messageParams()
185191
{
186-
location = location_;
187-
messageParams = rapidjson::Value{};
188-
messageParams.SetObject();
189-
allocator.Clear();
192+
return attributes_["Params"];
190193
}
194+
195+
rapidjson::Value&
196+
attributes()
197+
{
198+
return attributes_;
199+
}
200+
201+
void
202+
reset(
203+
std::source_location location,
204+
severities::Severity severity,
205+
std::optional<JsonLogAttributes> const& attributes) noexcept;
191206
};
192207

193208
private:
@@ -204,14 +219,13 @@ class Journal
204219
// Invariant: m_sink always points to a valid Sink
205220
Sink* m_sink = nullptr;
206221

207-
static void
208-
initMessageContext(std::source_location location);
222+
void
223+
initMessageContext(
224+
std::source_location location,
225+
severities::Severity severity) const;
209226

210227
static std::string
211-
formatLog(
212-
std::string const& message,
213-
severities::Severity severity,
214-
std::optional<JsonLogAttributes> const& attributes = std::nullopt);
228+
formatLog(std::string const& message);
215229

216230
public:
217231
//--------------------------------------------------------------------------
@@ -304,25 +318,16 @@ class Journal
304318
{
305319
public:
306320
ScopedStream(ScopedStream const& other)
307-
: ScopedStream(other.m_attributes, other.m_sink, other.m_level)
321+
: ScopedStream(other.m_sink, other.m_level)
308322
{
309323
}
310324

311-
ScopedStream(
312-
std::optional<JsonLogAttributes> attributes,
313-
Sink& sink,
314-
Severity level);
325+
ScopedStream(Sink& sink, Severity level);
315326

316327
template <typename T>
317-
ScopedStream(
318-
std::optional<JsonLogAttributes> attributes,
319-
Stream const& stream,
320-
T const& t);
328+
ScopedStream(Stream const& stream, T const& t);
321329

322-
ScopedStream(
323-
std::optional<JsonLogAttributes> attributes,
324-
Stream const& stream,
325-
std::ostream& manip(std::ostream&));
330+
ScopedStream(Stream const& stream, std::ostream& manip(std::ostream&));
326331

327332
ScopedStream&
328333
operator=(ScopedStream const&) = delete;
@@ -343,7 +348,6 @@ class Journal
343348
operator<<(T const& t) const;
344349

345350
private:
346-
std::optional<JsonLogAttributes> m_attributes;
347351
Sink& m_sink;
348352
Severity const m_level;
349353
std::ostringstream mutable m_ostream;
@@ -378,20 +382,15 @@ class Journal
378382
379383
Constructor is inlined so checking active() very inexpensive.
380384
*/
381-
Stream(
382-
std::optional<JsonLogAttributes> attributes,
383-
Sink& sink,
384-
Severity level)
385-
: m_attributes(std::move(attributes)), m_sink(sink), m_level(level)
385+
Stream(Sink& sink, Severity level) : m_sink(sink), m_level(level)
386386
{
387387
XRPL_ASSERT(
388388
m_level < severities::kDisabled,
389389
"beast::Journal::Stream::Stream : maximum level");
390390
}
391391

392392
/** Construct or copy another Stream. */
393-
Stream(Stream const& other)
394-
: Stream(other.m_attributes, other.m_sink, other.m_level)
393+
Stream(Stream const& other) : Stream(other.m_sink, other.m_level)
395394
{
396395
}
397396

@@ -438,7 +437,6 @@ class Journal
438437
/** @} */
439438

440439
private:
441-
std::optional<JsonLogAttributes> m_attributes;
442440
Sink& m_sink;
443441
Severity m_level;
444442
};
@@ -467,9 +465,8 @@ class Journal
467465
if (other.m_attributes.has_value())
468466
{
469467
if (m_attributes.has_value())
470-
m_attributes = JsonLogAttributes::combine(
471-
other.m_attributes->contextValues_,
472-
m_attributes->contextValues_);
468+
m_attributes->combine(
469+
other.m_attributes->contextValues_);
473470
else
474471
m_attributes = other.m_attributes;
475472
}
@@ -516,9 +513,11 @@ class Journal
516513

517514
/** Returns a stream for this sink, with the specified severity level. */
518515
Stream
519-
stream(Severity level) const
516+
stream(Severity level, std::source_location location = std::source_location::current()) const
520517
{
521-
return Stream(m_attributes, *m_sink, level);
518+
if (m_jsonLogsEnabled)
519+
initMessageContext(location, level);
520+
return Stream(*m_sink, level);
522521
}
523522

524523
/** Returns `true` if any message would be logged at this severity level.
@@ -537,24 +536,24 @@ class Journal
537536
trace(std::source_location location = std::source_location::current()) const
538537
{
539538
if (m_jsonLogsEnabled)
540-
initMessageContext(location);
541-
return {m_attributes, *m_sink, severities::kTrace};
539+
initMessageContext(location, severities::kTrace);
540+
return {*m_sink, severities::kTrace};
542541
}
543542

544543
Stream
545544
debug(std::source_location location = std::source_location::current()) const
546545
{
547546
if (m_jsonLogsEnabled)
548-
initMessageContext(location);
549-
return {m_attributes, *m_sink, severities::kDebug};
547+
initMessageContext(location, severities::kDebug);
548+
return {*m_sink, severities::kDebug};
550549
}
551550

552551
Stream
553552
info(std::source_location location = std::source_location::current()) const
554553
{
555554
if (m_jsonLogsEnabled)
556-
initMessageContext(location);
557-
return {m_attributes, *m_sink, severities::kInfo};
555+
initMessageContext(location, severities::kInfo);
556+
return {*m_sink, severities::kInfo};
558557
}
559558

560559
Stream
@@ -563,24 +562,24 @@ class Journal
563562
char const* a = "a";
564563
rapidjson::Value v{a, 1};
565564
if (m_jsonLogsEnabled)
566-
initMessageContext(location);
567-
return {m_attributes, *m_sink, severities::kWarning};
565+
initMessageContext(location, severities::kWarning);
566+
return {*m_sink, severities::kWarning};
568567
}
569568

570569
Stream
571570
error(std::source_location location = std::source_location::current()) const
572571
{
573572
if (m_jsonLogsEnabled)
574-
initMessageContext(location);
575-
return {m_attributes, *m_sink, severities::kError};
573+
initMessageContext(location, severities::kError);
574+
return {*m_sink, severities::kError};
576575
}
577576

578577
Stream
579578
fatal(std::source_location location = std::source_location::current()) const
580579
{
581580
if (m_jsonLogsEnabled)
582-
initMessageContext(location);
583-
return {m_attributes, *m_sink, severities::kFatal};
581+
initMessageContext(location, severities::kFatal);
582+
return {*m_sink, severities::kFatal};
584583
}
585584
/** @} */
586585

@@ -599,8 +598,7 @@ class Journal
599598
{
600599
globalLogAttributes_ = JsonLogAttributes{};
601600
}
602-
globalLogAttributes_ = JsonLogAttributes::combine(
603-
globalLogAttributes_->contextValues(),
601+
globalLogAttributes_->combine(
604602
globalLogAttributes.contextValues());
605603
}
606604
};
@@ -617,11 +615,8 @@ static_assert(std::is_nothrow_destructible<Journal>::value == true, "");
617615
//------------------------------------------------------------------------------
618616

619617
template <typename T>
620-
Journal::ScopedStream::ScopedStream(
621-
std::optional<JsonLogAttributes> attributes,
622-
Stream const& stream,
623-
T const& t)
624-
: ScopedStream(std::move(attributes), stream.sink(), stream.level())
618+
Journal::ScopedStream::ScopedStream(Stream const& stream, T const& t)
619+
: ScopedStream(stream.sink(), stream.level())
625620
{
626621
m_ostream << t;
627622
}
@@ -640,7 +635,7 @@ template <typename T>
640635
Journal::ScopedStream
641636
Journal::Stream::operator<<(T const& t) const
642637
{
643-
return {m_attributes, *this, t};
638+
return {*this, t};
644639
}
645640

646641
namespace detail {
@@ -766,6 +761,7 @@ setJsonValue(
766761
}
767762
}
768763

764+
object.RemoveMember(name);
769765
object.AddMember(
770766
rapidjson::StringRef(name), std::move(jsonValue), allocator);
771767
}
@@ -778,8 +774,8 @@ operator<<(std::ostream& os, LogParameter<T> const& param)
778774
if (!beast::Journal::m_jsonLogsEnabled)
779775
return os;
780776
detail::setJsonValue(
781-
beast::Journal::currentJsonLogContext_.messageParams,
782-
beast::Journal::currentJsonLogContext_.allocator,
777+
beast::Journal::currentJsonLogContext_.messageParams(),
778+
beast::Journal::currentJsonLogContext_.allocator(),
783779
param.name_,
784780
param.value_,
785781
&os);
@@ -793,8 +789,8 @@ operator<<(std::ostream& os, LogField<T> const& param)
793789
if (!beast::Journal::m_jsonLogsEnabled)
794790
return os;
795791
detail::setJsonValue(
796-
beast::Journal::currentJsonLogContext_.messageParams,
797-
beast::Journal::currentJsonLogContext_.allocator,
792+
beast::Journal::currentJsonLogContext_.messageParams(),
793+
beast::Journal::currentJsonLogContext_.allocator(),
798794
param.name_,
799795
param.value_,
800796
nullptr);

0 commit comments

Comments
 (0)