Skip to content

Commit 1fb569a

Browse files
committed
Optimise
Signed-off-by: JCW <[email protected]>
1 parent a2e2127 commit 1fb569a

File tree

6 files changed

+270
-259
lines changed

6 files changed

+270
-259
lines changed

cmake/RippledCore.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ target_link_libraries(xrpl.libpb
5151
# TODO: Clean up the number of library targets later.
5252
add_library(xrpl.imports.main INTERFACE)
5353

54-
find_package(RapidJSON)
55-
5654
target_link_libraries(xrpl.imports.main
5755
INTERFACE
5856
LibArchive::LibArchive
@@ -77,7 +75,6 @@ add_module(xrpl beast)
7775
target_link_libraries(xrpl.libxrpl.beast PUBLIC
7876
xrpl.imports.main
7977
xrpl.libpb
80-
rapidjson
8178
)
8279

8380
# Level 02

conanfile.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ class Xrpl(ConanFile):
2929
'nudb/2.0.9',
3030
'openssl/3.5.2',
3131
'soci/4.0.3',
32-
'zlib/1.3.1',
33-
"rapidjson/1.1.0"
32+
'zlib/1.3.1'
3433
]
3534

3635
test_requires = [

include/xrpl/beast/utility/Journal.h

Lines changed: 74 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
#include <xrpl/beast/utility/instrumentation.h>
2424

2525
#include <charconv>
26-
#include <string>
2726
#include <source_location>
2827
#include <sstream>
28+
#include <string>
29+
#include <string_view>
2930
#include <utility>
3031

3132
namespace ripple::log {
@@ -88,44 +89,57 @@ class SimpleJsonWriter
8889
void
8990
startObject() const
9091
{
91-
stream_.append("{", 1);
92+
stream_.push_back('{');
9293
}
9394
void
9495
endObject() const
9596
{
97+
using namespace std::string_view_literals;
9698
stream_.pop_back();
97-
stream_.append("},", 2);
99+
stream_.append("},"sv);
98100
}
99101
void
100102
writeKey(std::string_view key) const
101103
{
102104
writeString(key);
103-
*stream_.rbegin() = ':';
105+
stream_.back() = ':';
104106
}
105107
void
106108
startArray() const
107109
{
108-
stream_.append("[", 1);
110+
stream_.push_back('[');
109111
}
110112
void
111113
endArray() const
112114
{
115+
using namespace std::string_view_literals;
113116
stream_.pop_back();
114-
stream_.append("],", 2);
117+
stream_.append("],"sv);
115118
}
116119
void
117120
writeString(std::string_view str) const
118121
{
122+
using namespace std::string_view_literals;
119123
stream_.push_back('"');
120124
escape(str, stream_);
121-
stream_.append("\",", 2);
125+
stream_.append("\","sv);
126+
}
127+
std::string_view
128+
writeInt(std::int32_t val) const
129+
{
130+
return pushNumber(val, stream_);
122131
}
123132
std::string_view
124133
writeInt(std::int64_t val) const
125134
{
126135
return pushNumber(val, stream_);
127136
}
128137
std::string_view
138+
writeUInt(std::uint32_t val) const
139+
{
140+
return pushNumber(val, stream_);
141+
}
142+
std::string_view
129143
writeUInt(std::uint64_t val) const
130144
{
131145
return pushNumber(val, stream_);
@@ -138,38 +152,39 @@ class SimpleJsonWriter
138152
std::string_view
139153
writeBool(bool val) const
140154
{
141-
auto str = val ? "true," : "false,";
142-
stream_.append(str, std::strlen(str));
155+
using namespace std::string_view_literals;
156+
auto str = val ? "true,"sv : "false,"sv;
157+
stream_.append(str);
143158
return str;
144159
}
145160
void
146161
writeNull() const
147162
{
148-
stream_.append("null", std::strlen("null"));
149-
stream_.push_back(',');
163+
using namespace std::string_view_literals;
164+
stream_.append("null,"sv);
150165
}
151166
void
152167
writeRaw(std::string_view str) const
153168
{
154169
stream_.append(str);
155170
}
156171

157-
[[nodiscard]] std::string
172+
[[nodiscard]] std::string_view
158173
finish()
159174
{
160-
stream_.pop_back();
161-
return stream_;
175+
return std::string_view{stream_.c_str(), stream_.size() - 1};
162176
}
163177

164178
private:
165179
template <typename T>
166180
static std::string_view
167181
pushNumber(T val, std::string& stream)
168182
{
169-
static char buffer[128];
183+
thread_local char buffer[128];
170184
auto result = std::to_chars(std::begin(buffer), std::end(buffer), val);
171-
*result.ptr = ',';
172-
auto len = result.ptr - std::begin(buffer);
185+
auto ptr = result.ptr;
186+
*ptr = ',';
187+
auto len = ptr - std::begin(buffer);
173188
stream.append(buffer, len + 1);
174189
return {buffer, static_cast<size_t>(len)};
175190
}
@@ -179,17 +194,19 @@ class SimpleJsonWriter
179194
{
180195
static constexpr char HEX[] = "0123456789ABCDEF";
181196

182-
const char* p = str.data();
183-
const char* end = p + str.size();
184-
const char* chunk = p;
197+
char const* p = str.data();
198+
char const* end = p + str.size();
199+
char const* chunk = p;
185200

186-
while (p < end) {
201+
while (p < end)
202+
{
187203
auto c = static_cast<unsigned char>(*p);
188204

189205
// JSON requires escaping for <0x20 and the two specials below.
190206
bool needsEscape = (c < 0x20) || (c == '"') || (c == '\\');
191207

192-
if (!needsEscape) {
208+
if (!needsEscape)
209+
{
193210
++p;
194211
continue;
195212
}
@@ -198,21 +215,33 @@ class SimpleJsonWriter
198215
if (chunk != p)
199216
os.append(chunk, p - chunk);
200217

201-
switch (c) {
202-
case '"': os.append("\\\"", 2); break;
203-
case '\\': os.append("\\\\", 2); break;
204-
case '\b': os.append("\\b", 2); break;
205-
case '\f': os.append("\\f", 2); break;
206-
case '\n': os.append("\\n", 2); break;
207-
case '\r': os.append("\\r", 2); break;
208-
case '\t': os.append("\\t", 2); break;
218+
switch (c)
219+
{
220+
case '"':
221+
os.append("\\\"", 2);
222+
break;
223+
case '\\':
224+
os.append("\\\\", 2);
225+
break;
226+
case '\b':
227+
os.append("\\b", 2);
228+
break;
229+
case '\f':
230+
os.append("\\f", 2);
231+
break;
232+
case '\n':
233+
os.append("\\n", 2);
234+
break;
235+
case '\r':
236+
os.append("\\r", 2);
237+
break;
238+
case '\t':
239+
os.append("\\t", 2);
240+
break;
209241
default: {
210242
// Other C0 controls -> \u00XX (JSON compliant)
211243
char buf[6]{
212-
'\\','u','0','0',
213-
HEX[(c >> 4) & 0xF],
214-
HEX[c & 0xF]
215-
};
244+
'\\', 'u', '0', '0', HEX[(c >> 4) & 0xF], HEX[c & 0xF]};
216245
os.append(buf, 6);
217246
break;
218247
}
@@ -288,6 +317,7 @@ class Journal
288317
public:
289318
JsonLogContext() : messageParamsWriter_(buffer_)
290319
{
320+
buffer_.reserve(1024 * 5);
291321
}
292322

293323
SimpleJsonWriter&
@@ -300,7 +330,7 @@ class Journal
300330
reset(
301331
std::source_location location,
302332
severities::Severity severity,
303-
std::string const& journalAttributesJson) noexcept;
333+
std::string_view journalAttributesJson) noexcept;
304334
};
305335

306336
private:
@@ -323,7 +353,7 @@ class Journal
323353
std::source_location location,
324354
severities::Severity severity) const;
325355

326-
static std::string
356+
static std::string_view
327357
formatLog(std::string&& message);
328358

329359
public:
@@ -830,9 +860,10 @@ namespace ripple::log {
830860
namespace detail {
831861

832862
template <typename T>
833-
concept CanToChars = requires (T val)
834-
{
835-
{ to_chars(std::declval<char*>(), std::declval<char*>(), val) } -> std::convertible_to<std::to_chars_result>;
863+
concept CanToChars = requires(T val) {
864+
{
865+
to_chars(std::declval<char*>(), std::declval<char*>(), val)
866+
} -> std::convertible_to<std::to_chars_result>;
836867
};
837868

838869
template <typename T>
@@ -901,7 +932,8 @@ setJsonValue(
901932
if constexpr (CanToChars<ValueType>)
902933
{
903934
char buffer[1024];
904-
std::to_chars_result result = to_chars(std::begin(buffer), std::end(buffer), value);
935+
std::to_chars_result result =
936+
to_chars(std::begin(buffer), std::end(buffer), value);
905937
if (result.ec == std::errc{})
906938
{
907939
std::string_view sv;
@@ -925,7 +957,8 @@ setJsonValue(
925957

926958
if (outStream)
927959
{
928-
outStream->write(str.c_str(), static_cast<std::streamsize>(str.size()));
960+
outStream->write(
961+
str.c_str(), static_cast<std::streamsize>(str.size()));
929962
}
930963
}
931964
}

src/libxrpl/beast/utility/beast_Journal.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void
123123
Journal::JsonLogContext::reset(
124124
std::source_location location,
125125
severities::Severity severity,
126-
std::string const& journalAttributesJson) noexcept
126+
std::string_view journalAttributesJson) noexcept
127127
{
128128
struct ThreadIdStringInitializer
129129
{
@@ -152,7 +152,9 @@ Journal::JsonLogContext::reset(
152152
if (!globalLogAttributesJson_.empty())
153153
{
154154
writer().writeKey("GlobalParams");
155-
writer().writeRaw(globalLogAttributesJson_);
155+
writer().writeRaw(std::string_view{
156+
std::begin(globalLogAttributesJson_),
157+
std::end(globalLogAttributesJson_)});
156158
writer().endObject();
157159
}
158160

@@ -166,7 +168,7 @@ Journal::JsonLogContext::reset(
166168
writer().writeString(location.file_name());
167169

168170
writer().writeKey("Line");
169-
writer().writeInt(location.line());
171+
writer().writeInt(static_cast<std::int32_t>(location.line()));
170172

171173
writer().writeKey("ThreadId");
172174
writer().writeString(threadId.value);
@@ -176,20 +178,24 @@ Journal::JsonLogContext::reset(
176178
writer().writeString(severityStr);
177179

178180
writer().writeKey("Time");
179-
writer().writeUInt(std::chrono::duration_cast<std::chrono::milliseconds>(
180-
std::chrono::system_clock::now().time_since_epoch())
181-
.count());
181+
writer().writeInt(std::chrono::duration_cast<std::chrono::milliseconds>(
182+
std::chrono::system_clock::now().time_since_epoch())
183+
.count());
182184
}
183185

184186
void
185187
Journal::initMessageContext(
186188
std::source_location location,
187189
severities::Severity severity) const
188190
{
189-
currentJsonLogContext_.reset(location, severity, m_attributesJson);
191+
currentJsonLogContext_.reset(
192+
location,
193+
severity,
194+
std::string_view{
195+
std::begin(m_attributesJson), std::end(m_attributesJson)});
190196
}
191197

192-
std::string
198+
std::string_view
193199
Journal::formatLog(std::string&& message)
194200
{
195201
if (!m_jsonLogsEnabled)
@@ -288,9 +294,9 @@ Journal::ScopedStream::~ScopedStream()
288294
if (!s.empty())
289295
{
290296
if (s == "\n")
291-
m_sink.write(m_level, formatLog(""));
297+
m_sink.write(m_level, std::string{formatLog("")});
292298
else
293-
m_sink.write(m_level, formatLog(std::move(s)));
299+
m_sink.write(m_level, std::string{formatLog(std::move(s))});
294300
}
295301
}
296302

src/tests/libxrpl/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ include(xrpl_add_test)
22

33
# Test requirements.
44
find_package(doctest REQUIRED)
5-
find_package(RapidJSON REQUIRED)
65

76
# Common library dependencies for the rest of the tests.
87
add_library(xrpl.imports.test INTERFACE)
9-
target_link_libraries(xrpl.imports.test INTERFACE doctest::doctest rapidjson xrpl.libxrpl)
8+
target_link_libraries(xrpl.imports.test INTERFACE doctest::doctest xrpl.libxrpl)
109

1110
# One test for each module.
1211
xrpl_add_test(basics)

0 commit comments

Comments
 (0)