You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was profiling a query that contained a (not overly specific) getAll(), followed by a filter.
It turned out that approximately 46% of the time were spent in datum_t::init_json().
Virtually all of the calls to init_json() originated from the transform_visitor_t called directly from the btree traversal. In other words, reading the cJSON encoded documents from the btree is very expensive.
A few more details:
Inside of init_json(), 58% of its time were spent on datum_t::add() for objects, with the remaining processing time spread out over a number of different steps. Inside of add(), actually inserting into the std::map makes up for 41% of the time spent in add().
I'm not sure what's the best way to optimize it, and am hoping for suggestions.
The std::map insertion could probably be made a lot faster by just using an std::vector instead of a map. Usually, even for read operations, the tree used by std::map is only faster than a vector if there are a lot of entries, presumably due to its bad cache locality. Eliminating the std::map would however only solve part of the issue.
Even if we get rid of CJSON, we'll still have to instantiate datum_t objects from the serialized representations on disk. Since what seems to make this process slow is inserting fields into an std::map, simply replacing CJSON with something else will not solve this performance problem. This issue should really be about optimizing the construction of datum_t's.
I'm probably being completely uninformed since I don't know much about the code, but one high level option might be to load the binary data into a datum_t and only convert it to an std::map lazily when we actually need that (possibly even analyze the query and only convert the fields that we might need). Since a lot of times people don't actually need to do much evaluation on the data, even a naive solution would give a nice boost. A non-naive one would be even better.
std::map is pretty fast, so I can't think of much else we can do to optimize that away.
Actually, another option would be to use a hash table instead of a map. It might make a huge difference (both in construction and retrieval), since the keys are trivially hashable.
I think that relies on the assumption that a BSON library can just copy a serialized format into memory and make it accessible as a hash-table (and not require copying into a map or hash map). I don't know whether that's true (we should check), but that seems unlikely. A good memory format for random access is almost certainly different from a good disk format.
We should at least try switching to a hash map (basically unordered_map in C++). We might see a huge performance improvement with basically no work.
I've tried replacing the std::map with a boost::unordered_map already. It didn't help overall performance in a measurable way.
Also mind that the map insert in general takes only a fraction of the datum_t creation time (see original post).
I think that relies on the assumption that a BSON library can just copy a serialized format into memory and make it accessible as a hash-table (and not require copying into a map or hash map). I don't know whether that's true (we should check), but that seems unlikely. A good memory format for random access is almost certainly different from a good disk format.
We should at least try switching to a hash map (basically unordered_map in C++). We might see a huge performance improvement with basically no work.
Reply to this email directly or view it on GitHub: #1042 (comment)
If I read the original post correctly, we're only spending 41% of 58% of our time (about a quarter of the total) in std::map. Moreover, we go back and forth between datum_t and JSON a lot, so we're probably doing that work more times than we need to. I think we should just strip out cJSON, since we know that we need to do that to be fast, then run the benchmarks again and see what the second biggest problem is.
It is actually even just an eighth of the total, and a quarter of init_json().
Maybe my tests were not elaborate enough to expose any performance advantages of the hash map (I just ran the overall query 10 times in a row and averaged over the processing time. Each execution of the query took ~4.7 seconds for the pure getAll() query). But in any case, it would only be a small contribution to the overall performance.
The profiling results are on deadshot and can be viewed with
kcachegriend /home/daniel/rethinkdb/oprof/calltree-trakio.out
(requires X, use ssh -X)
A script which runs the corresponding query is in /home/daniel/php-tests/trakio-plain.php .
There is also a variant trakio-getAll.php which runs only the getAll (followed by a filter(false) to make sure that the measurements were not diluted by networking code). The data (for RethinkDB 1.5) is in /home/daniel/trak-io/rethinkdb_data also on deadshot.
@coffeemug I was thinking that was what BSON did but I'm not sure. There are things that do that though I believe or we could make something that does that ourselves.
@danielmewes I wouldn't really expect using an unordered_map to have much effect. std::map is very optimized and the difference between O(n) and O(n log n) isn't going to be that noticeable with the size of n we deal with here.
Objects in RQL are technically immutable right? We might be able to build an immutable map type with a low memory footprint that is quick to initialize and copy if we didn't have to support inserts. I suppose it depends on the user's data, but most RQL objects seem to have only a handful of keys. We could probably get away with linear lookup too.
A binary representation that we could just dump to the disk and operate on directly when in memory would certainly be perfect performance-wise (arguably less perfect from a software engineering standpoint)
I'm thinking of a datum_t having something like a single array, which we can reinterpret as a low-level struct. Nested datum_ts would be concatenated together in the same array and addressed through offsets.
Apart from saving cycles when constructing the datum_ts, we would also have considerably better memory locality.
I don't think we should be too concerned about asymptotic running time complexity of the data structures in the datum_t. In my experience, for small-ish data (say objects with ~100 keys), "smart" data structures like trees (std::map) or hash maps don't pay off, because
a) you loose a lot of CPU cache efficiency due to many random memory reads
b) you potentially interfere with speculative execution on the CPU
c) you get overhead for memory allocation and deallocation due to the many small objects in your data structure compared to a single array
Of course our decision should depend on the use case and which operations are run how often on the data structures in question.
So, I think that the cJSON issue was addressed by the fix for #1041 -- we don't use cJSON in the query language any more, except for r.json and some printing things. Would anyone object if I closed this, and possibly opened a new issue to discuss changing the data structures?
Just closing, then. I want to hold off on opening a new issue about the data structures until we have some new performance numbers indicating that they're still a big problem.
Activity
danielmewes commentedon Jun 19, 2013
PS: The getAll() was retrieving 390,373 documents.
jdoliner commentedon Jun 19, 2013
We're already planning to completely get rid of cJSON.
danielmewes commentedon Jun 19, 2013
@jdoliner Excellent. Then this might add some motivation for the enterprise. :-)
coffeemug commentedon Jun 19, 2013
Hopefully removing cJSON is a 1.8 worthy enterprise.
mlucy commentedon Jun 19, 2013
@coffeemug -- I think we're overdue for this and should schedule it in 1.8.
wmrowan commentedon Jun 20, 2013
Even if we get rid of CJSON, we'll still have to instantiate datum_t objects from the serialized representations on disk. Since what seems to make this process slow is inserting fields into an std::map, simply replacing CJSON with something else will not solve this performance problem. This issue should really be about optimizing the construction of datum_t's.
coffeemug commentedon Jun 20, 2013
I'm probably being completely uninformed since I don't know much about the code, but one high level option might be to load the binary data into a
datum_tand only convert it to anstd::maplazily when we actually need that (possibly even analyze the query and only convert the fields that we might need). Since a lot of times people don't actually need to do much evaluation on the data, even a naive solution would give a nice boost. A non-naive one would be even better.std::mapis pretty fast, so I can't think of much else we can do to optimize that away.coffeemug commentedon Jun 20, 2013
Actually, another option would be to use a hash table instead of a map. It might make a huge difference (both in construction and retrieval), since the keys are trivially hashable.
jdoliner commentedon Jun 20, 2013
The correct answer here is to just use a BSON equivalent. Then we don't need a conversion at all.
coffeemug commentedon Jun 20, 2013
I think that relies on the assumption that a BSON library can just copy a serialized format into memory and make it accessible as a hash-table (and not require copying into a map or hash map). I don't know whether that's true (we should check), but that seems unlikely. A good memory format for random access is almost certainly different from a good disk format.
We should at least try switching to a hash map (basically
unordered_mapin C++). We might see a huge performance improvement with basically no work.danielmewes commentedon Jun 20, 2013
I've tried replacing the std::map with a boost::unordered_map already. It didn't help overall performance in a measurable way.
Also mind that the map insert in general takes only a fraction of the datum_t creation time (see original post).
-----Original Message-----
From: coffeemug [email protected]
Date: Wed, 19 Jun 2013 18:29:30
To: rethinkdb/[email protected]
Reply-To: rethinkdb/rethinkdb [email protected]
Cc: Daniel [email protected]
Subject: Re: [rethinkdb] cJSON -> datum_t conversion is slooow (#1042)
I think that relies on the assumption that a BSON library can just copy a serialized format into memory and make it accessible as a hash-table (and not require copying into a map or hash map). I don't know whether that's true (we should check), but that seems unlikely. A good memory format for random access is almost certainly different from a good disk format.
We should at least try switching to a hash map (basically
unordered_mapin C++). We might see a huge performance improvement with basically no work.Reply to this email directly or view it on GitHub:
#1042 (comment)
mlucy commentedon Jun 20, 2013
Also, we kinda need an ordered map for when we compare two objects.
mlucy commentedon Jun 20, 2013
If I read the original post correctly, we're only spending 41% of 58% of our time (about a quarter of the total) in
std::map. Moreover, we go back and forth betweendatum_tand JSON a lot, so we're probably doing that work more times than we need to. I think we should just strip out cJSON, since we know that we need to do that to be fast, then run the benchmarks again and see what the second biggest problem is.danielmewes commentedon Jun 20, 2013
It is actually even just an eighth of the total, and a quarter of init_json().
Maybe my tests were not elaborate enough to expose any performance advantages of the hash map (I just ran the overall query 10 times in a row and averaged over the processing time. Each execution of the query took ~4.7 seconds for the pure getAll() query). But in any case, it would only be a small contribution to the overall performance.
The profiling results are on deadshot and can be viewed with
kcachegriend /home/daniel/rethinkdb/oprof/calltree-trakio.out
(requires X, use ssh -X)
A script which runs the corresponding query is in /home/daniel/php-tests/trakio-plain.php .
There is also a variant trakio-getAll.php which runs only the getAll (followed by a filter(false) to make sure that the measurements were not diluted by networking code). The data (for RethinkDB 1.5) is in /home/daniel/trak-io/rethinkdb_data also on deadshot.
The profiling was done against rethinkdb 1.5.2.
jdoliner commentedon Jun 20, 2013
@coffeemug I was thinking that was what BSON did but I'm not sure. There are things that do that though I believe or we could make something that does that ourselves.
@danielmewes I wouldn't really expect using an
unordered_mapto have much effect.std::mapis very optimized and the difference betweenO(n)andO(n log n)isn't going to be that noticeable with the size ofnwe deal with here.wmrowan commentedon Jun 20, 2013
Objects in RQL are technically immutable right? We might be able to build an immutable map type with a low memory footprint that is quick to initialize and copy if we didn't have to support inserts. I suppose it depends on the user's data, but most RQL objects seem to have only a handful of keys. We could probably get away with linear lookup too.
danielmewes commentedon Jun 20, 2013
A binary representation that we could just dump to the disk and operate on directly when in memory would certainly be perfect performance-wise (arguably less perfect from a software engineering standpoint)
I'm thinking of a datum_t having something like a single array, which we can reinterpret as a low-level struct. Nested datum_ts would be concatenated together in the same array and addressed through offsets.
Apart from saving cycles when constructing the datum_ts, we would also have considerably better memory locality.
I don't think we should be too concerned about asymptotic running time complexity of the data structures in the datum_t. In my experience, for small-ish data (say objects with ~100 keys), "smart" data structures like trees (std::map) or hash maps don't pay off, because
a) you loose a lot of CPU cache efficiency due to many random memory reads
b) you potentially interfere with speculative execution on the CPU
c) you get overhead for memory allocation and deallocation due to the many small objects in your data structure compared to a single array
Of course our decision should depend on the use case and which operations are run how often on the data structures in question.
mlucy commentedon Aug 24, 2013
So, I think that the cJSON issue was addressed by the fix for #1041 -- we don't use cJSON in the query language any more, except for
r.jsonand some printing things. Would anyone object if I closed this, and possibly opened a new issue to discuss changing the data structures?coffeemug commentedon Aug 27, 2013
It appears nobody minds. I, for one, am cool with that.
mlucy commentedon Aug 27, 2013
Just closing, then. I want to hold off on opening a new issue about the data structures until we have some new performance numbers indicating that they're still a big problem.