Skip to content

Commit 436c4f2

Browse files
committed
Optimisation
Signed-off-by: JCW <[email protected]>
1 parent 7e359c2 commit 436c4f2

File tree

3 files changed

+48
-52
lines changed

3 files changed

+48
-52
lines changed

include/xrpl/beast/utility/Journal.h

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -80,46 +80,44 @@ namespace beast {
8080
class SimpleJsonWriter
8181
{
8282
public:
83-
explicit SimpleJsonWriter(std::ostringstream& stream) : stream_(stream)
83+
explicit SimpleJsonWriter(std::string& stream) : stream_(stream)
8484
{
85-
stream_.imbue(std::locale::classic());
8685
}
8786

8887
void
8988
startObject() const
9089
{
91-
stream_.put('{');
90+
stream_.append("{", 1);
9291
}
9392
void
9493
endObject() const
9594
{
96-
stream_.seekp(-1, std::ios_base::end);
97-
stream_.write("},", 2);
95+
stream_.pop_back();
96+
stream_.append("},", 2);
9897
}
9998
void
10099
writeKey(std::string_view key) const
101100
{
102101
writeString(key);
103-
stream_.seekp(-1, std::ios_base::end);
104-
stream_.put(':');
102+
*stream_.rbegin() = ':';
105103
}
106104
void
107105
startArray() const
108106
{
109-
stream_.put('[');
107+
stream_.append("[", 1);
110108
}
111109
void
112110
endArray() const
113111
{
114-
stream_.seekp(-1, std::ios_base::end);
115-
stream_.write("],", 2);
112+
stream_.pop_back();
113+
stream_.append("],", 2);
116114
}
117115
void
118116
writeString(std::string_view str) const
119117
{
120-
stream_.put('"');
118+
stream_.push_back('"');
121119
escape(str, stream_);
122-
stream_.write("\",", 2);
120+
stream_.append("\",", 2);
123121
}
124122
std::string_view
125123
writeInt(std::int64_t val) const
@@ -140,44 +138,43 @@ class SimpleJsonWriter
140138
writeBool(bool val) const
141139
{
142140
auto str = val ? "true," : "false,";
143-
stream_.write(str, std::strlen(str));
141+
stream_.append(str, std::strlen(str));
144142
return str;
145143
}
146144
void
147145
writeNull() const
148146
{
149-
stream_.write("null", std::strlen("null"));
150-
stream_.put(',');
147+
stream_.append("null", std::strlen("null"));
148+
stream_.push_back(',');
151149
}
152150
void
153151
writeRaw(std::string_view str) const
154152
{
155-
stream_.write(str.data(), str.length());
153+
stream_.append(str);
156154
}
157155

158156
[[nodiscard]] std::string
159-
str() const
157+
finish()
160158
{
161-
auto result = stream_.str();
162-
result.pop_back();
163-
return result;
159+
stream_.pop_back();
160+
return stream_;
164161
}
165162

166163
private:
167164
template <typename T>
168165
static std::string_view
169-
pushNumber(T val, std::ostringstream& stream)
166+
pushNumber(T val, std::string& stream)
170167
{
171168
static char buffer[128];
172169
auto result = std::to_chars(std::begin(buffer), std::end(buffer), val);
173170
*result.ptr = ',';
174171
auto len = result.ptr - std::begin(buffer);
175-
stream.write(buffer, len + 1);
172+
stream.append(buffer, len + 1);
176173
return {buffer, static_cast<size_t>(len)};
177174
}
178175

179176
static void
180-
escape(std::string_view str, std::ostringstream& os)
177+
escape(std::string_view str, std::string& os)
181178
{
182179
static constexpr char HEX[] = "0123456789ABCDEF";
183180

@@ -198,24 +195,24 @@ class SimpleJsonWriter
198195

199196
// Flush the preceding safe run in one go.
200197
if (chunk != p)
201-
os.write(chunk, p - chunk);
198+
os.append(chunk, p - chunk);
202199

203200
switch (c) {
204-
case '"': os.write("\\\"", 2); break;
205-
case '\\': os.write("\\\\", 2); break;
206-
case '\b': os.write("\\b", 2); break;
207-
case '\f': os.write("\\f", 2); break;
208-
case '\n': os.write("\\n", 2); break;
209-
case '\r': os.write("\\r", 2); break;
210-
case '\t': os.write("\\t", 2); break;
201+
case '"': os.append("\\\"", 2); break;
202+
case '\\': os.append("\\\\", 2); break;
203+
case '\b': os.append("\\b", 2); break;
204+
case '\f': os.append("\\f", 2); break;
205+
case '\n': os.append("\\n", 2); break;
206+
case '\r': os.append("\\r", 2); break;
207+
case '\t': os.append("\\t", 2); break;
211208
default: {
212209
// Other C0 controls -> \u00XX (JSON compliant)
213210
char buf[6]{
214211
'\\','u','0','0',
215212
HEX[(c >> 4) & 0xF],
216213
HEX[c & 0xF]
217214
};
218-
os.write(buf, 6);
215+
os.append(buf, 6);
219216
break;
220217
}
221218
}
@@ -226,10 +223,10 @@ class SimpleJsonWriter
226223

227224
// Flush trailing safe run
228225
if (chunk != p)
229-
os.write(chunk, p - chunk);
226+
os.append(chunk, p - chunk);
230227
}
231228

232-
std::ostringstream& stream_;
229+
std::string& stream_;
233230
};
234231

235232
/** A namespace for easy access to logging severity values. */
@@ -284,7 +281,7 @@ class Journal
284281

285282
class JsonLogContext
286283
{
287-
std::ostringstream buffer_;
284+
std::string buffer_;
288285
SimpleJsonWriter messageParamsWriter_;
289286

290287
public:
@@ -567,14 +564,14 @@ class Journal
567564
Journal(Journal const& other, TAttributesFactory&& attributesFactory)
568565
: m_name(other.m_name), m_sink(other.m_sink)
569566
{
570-
std::ostringstream stream{other.m_attributesJson, std::ios_base::app};
567+
std::string stream{other.m_attributesJson};
571568
SimpleJsonWriter writer{stream};
572569
if (other.m_attributesJson.empty())
573570
{
574571
writer.startObject();
575572
}
576573
attributesFactory(writer);
577-
m_attributesJson = stream.str();
574+
m_attributesJson = std::move(stream);
578575
}
579576

580577
/** Create a journal that writes to the specified sink. */
@@ -591,11 +588,11 @@ class Journal
591588
TAttributesFactory&& attributesFactory)
592589
: m_name(name), m_sink(&sink)
593590
{
594-
std::ostringstream stream;
591+
std::string stream;
595592
SimpleJsonWriter writer{stream};
596593
writer.startObject();
597594
attributesFactory(writer);
598-
m_attributesJson = stream.str();
595+
m_attributesJson = std::move(stream);
599596
}
600597

601598
Journal&
@@ -712,15 +709,14 @@ class Journal
712709
std::lock_guard lock(globalLogAttributesMutex_);
713710

714711
auto isEmpty = globalLogAttributesJson_.empty();
715-
std::ostringstream stream{
716-
std::move(globalLogAttributesJson_), std::ios_base::app};
712+
std::string stream{std::move(globalLogAttributesJson_)};
717713
SimpleJsonWriter writer{stream};
718714
if (isEmpty)
719715
{
720716
writer.startObject();
721717
}
722718
factory(writer);
723-
globalLogAttributesJson_ = stream.str();
719+
globalLogAttributesJson_ = std::move(stream);
724720
}
725721
};
726722

src/libxrpl/beast/utility/beast_Journal.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ Journal::JsonLogContext::reset(
138138
};
139139
thread_local ThreadIdStringInitializer const threadId;
140140

141-
buffer_.str("");
141+
buffer_.clear();
142142

143143
writer().startObject();
144144

@@ -206,7 +206,7 @@ Journal::formatLog(std::string&& message)
206206

207207
writer.endObject();
208208

209-
return writer.str();
209+
return writer.finish();
210210
}
211211

212212
void

src/tests/libxrpl/basics/log.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,35 +212,35 @@ TEST_CASE("Global attributes inheritable")
212212
TEST_CASE("Test JsonWriter")
213213
{
214214
{
215-
std::ostringstream stream;
215+
std::string stream;
216216
beast::SimpleJsonWriter writer{stream};
217217

218218
writer.writeString("\n");
219-
CHECK(writer.str() == "\"\\n\"");
219+
CHECK(writer.finish() == "\"\\n\"");
220220
}
221221

222222
{
223-
std::ostringstream stream;
223+
std::string stream;
224224
beast::SimpleJsonWriter writer{stream};
225225

226226
writer.writeString("\t");
227-
CHECK(writer.str() == "\"\\t\"");
227+
CHECK(writer.finish() == "\"\\t\"");
228228
}
229229

230230
{
231-
std::ostringstream stream;
231+
std::string stream;
232232
beast::SimpleJsonWriter writer{stream};
233233

234234
writer.writeString(std::string_view{"\0", 1});
235-
CHECK(writer.str() == "\"\\u0000\"");
235+
CHECK(writer.finish() == "\"\\u0000\"");
236236
}
237237

238238
{
239-
std::ostringstream stream;
239+
std::string stream;
240240
beast::SimpleJsonWriter writer{stream};
241241

242242
writer.writeString("\"\\");
243-
CHECK(writer.str() == "\"\\\"\\\\\"");
243+
CHECK(writer.finish() == "\"\\\"\\\\\"");
244244
}
245245
}
246246

0 commit comments

Comments
 (0)