Skip to content

Commit b83a71c

Browse files
committed
json-writer: optimize to reduce memory consumption
... by creating a temporary std::queue to delay the actual construction of the property tree. massif's output for a chosen sample data follows. Before this commit ================== MB 999.4^ ## | # | # | # | # | # | @# @:::@ | @# ::@:::@: | @# ::@:::@: | @# ::@:::@: | @@@# ::@:::@: :@:::: | @ @# ::@:::@: :@:::::@:::::@:::: | @ @# ::@:::@::::@:::::@:::::@:::: | @ @# ::@:::@:: :@:::::@:::::@:::: | :@@@@ @# ::@:::@:: :@:::::@:::::@:::: | @@::@@:@@@@ @# ::@:::@:: :@:::::@:::::@:::: | @:@@@:@@::@ :@@@@ @# ::@:::@:: :@:::::@:::::@:::: | ::@@::@:@ @:@@::@ :@@@@ @# ::@:::@:: :@:::::@:::::@:::: | :@@:::@@::@:@ @:@@::@ :@@@@ @# ::@:::@:: :@:::::@:::::@:::: | @@@@@@@:@@:::@@::@:@ @:@@::@ :@@@@ @# ::@:::@:: :@:::::@:::::@:::: 0 +----------------------------------------------------------------------->Gi 0 24.67 After this commit ================= MB 432.1^ # | # | #::: | ::#::: | :::#::: | @@::::#::: | @@@@::::#::: | @@@@ @@::::#::: | @@ @@ @@::::#::: | @@@@@ @@ @@::::#::: | @@@@@@@ @@ @@::::#::: | @@@@@@@@@ @@ @@::::#::: :::@:: | ::@@@@@@@@@ @@ @@::::#::: :::@::: | @@: @@@@@@@@@ @@ @@::::#::: ::@:::::@::: | @@@@: @@@@@@@@@ @@ @@::::#::: ::@::::@:::::@::: | @@@@@@: @@@@@@@@@ @@ @@::::#::: ::::@::::@:::::@::: | @@@@@@@@: @@@@@@@@@ @@ @@::::#::: :::::@::::@:::::@::: | @@@@@@@@@: @@@@@@@@@ @@ @@::::#:::::::::@::::@:::::@::: | @@@@@@@@@@@: @@@@@@@@@ @@ @@::::#:::::::::@::::@:::::@::: | @@@@::@@@@@@@@@@@@: @@@@@@@@@ @@ @@::::#:::::::::@::::@:::::@::: 0 +----------------------------------------------------------------------->Gi 0 23.56
1 parent b07ff80 commit b83a71c

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

json-writer.cc

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@
1919

2020
#include "json-writer.hh"
2121

22+
#include <queue>
23+
2224
#include <boost/foreach.hpp>
2325
#include <boost/iostreams/filtering_stream.hpp>
2426
#include <boost/iostreams/filter/regex.hpp>
2527
#include <boost/property_tree/json_parser.hpp>
2628

2729
struct JsonWriter::Private {
2830
std::ostream &str;
29-
boost::property_tree::ptree defList;
31+
std::queue<Defect> defQueue;
3032
TScanProps scanProps;
3133

3234
Private(std::ostream &str_):
@@ -97,7 +99,7 @@ void appendDefectNode(boost::property_tree::ptree &dst, const Defect &def) {
9799
}
98100

99101
void JsonWriter::handleDef(const Defect &def) {
100-
appendDefectNode(d->defList, def);
102+
d->defQueue.push(def);
101103
}
102104

103105
void JsonWriter::flush() {
@@ -130,7 +132,14 @@ void JsonWriter::flush() {
130132
root.put_child("scan", scan);
131133
}
132134

133-
// append the list of defects and stream it out
134-
root.put_child("defects", d->defList);
135+
// node representing the list of defects
136+
root.put_child("defects", boost::property_tree::ptree());
137+
boost::property_tree::ptree &defects = root.get_child("defects");
138+
139+
// go through the queue and move defects one by one to the property tree
140+
for (; !d->defQueue.empty(); d->defQueue.pop())
141+
appendDefectNode(defects, d->defQueue.front());
142+
143+
// finally encode the tree as JSON
135144
write_json(str, root);
136145
}

0 commit comments

Comments
 (0)