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
{{ message }}
Repository navigation
Client-side reduction faster than server-side reduction #3408
My biggest gripe is with (2) because most of the time it's faster to pull all the rows down and do the reduction in code. I assume this is because we have to map over every row before starting the reduction.
@marshall007: Could you find out how many rows match the getAll, and send us an example document from the standard_terms table? That would make it easier for us to reproduce and investigate the server slowness.
I doubt this is because of the map itself. My wild guess would be that either r.object or merge is slow, but in any case it needs testing.
I would bet that this is because the client code is destructively modifying a single document, while the server code is repeatedly constructing new documents. This is because in general terms aren't smart enough to figure out when they are the only ones with a reference to an object, and thus when it is safe to modify that object destructively. If we fixed that (and I think we have to fix it at some point), a lot of queries (including this one) would probably get much faster. Another way we could fix this particular problem would be to move to functional objects so that we don't have to copy the whole thing every time we construct a new one.
@deontologician: They share keys values but not object structure. If you merge an object with 1000 keys and an object with 1 key, we create a new object with 1001 keys. We don't copy any of the keys or values, which are reference counted, but we do have to build up a new 1001-key object structure. If we used functional objects, we could re-use part of the old object structure and do O(log(1000)) work. If we were able to detect when it's safe to destructively modify objects, we could just patch the big object and do O(1) work.
If we were to go with the mutability approach, here's one way we could do it:
Remove the copy constructor and assign operators from datum_t, so it's move-only, but add an explicit .copy() method. This would make it obvious where datums were being copied and make it easy to eliminate unnecessary copies.
When running an operation that can be implemented destructively, check if the underlying counted_t has a refcount of 1, and if so, operate destructively.
In general lambdas would use .copy(), but we should special-case the situation where a lambda uses its argument exactly once so it doesn't do any copying.
I'm always a little wary of the "check whether the refcount is 1" approach because small changes to the code that inadvertently produce a copy or keep an old copy around longer can cause huge, silent performance regressions. That said, it might be the easiest way to actually implement mutability in a reasonable amount of time.
They did something similar in GHC called stream-fusion, where it detects a pipeline of folds/maps etc and optimizes it into a single pass on a mutable data structure to avoid copying. I doubt any of the specific techniques they used would be applicable though.
In general lambdas would use .copy(), but we should special-case the situation where a lambda uses its argument exactly once so it doesn't do any copying.
You'd still miss cases like r.branch(right.hasFields('foo'), left.merge(right), left), right? Instead of trying to disambiguate the cases where it's safe, you could make all merge operations destructive by default and introduce something like r.clone() or r.copy() for when it's needed.
Activity
thelinuxlich commentedon Dec 4, 2014
The same thing happens to me in queries using r.object and merge too
marshall007 commentedon Dec 4, 2014
Here's an example document. The
synonymsarray contains on average about 2.65 elements but in a handful of cases it's 100-300.{ "id": "f99c2a99-4951-4405-b03d-61fe9bc6e73d", "field": "disorders", "term": "101000", "synonyms": [ "Neurofibromatosis, type 2", "Neurofibromatosis Type 2", "Autosomal Dominant Neurofibromatosis, type 2 (NF2)", "Neurofibromatosis, type 2 (NF2)," ] }marshall007 commentedon Dec 4, 2014
@danielmewes if you need more sample data let me know and I'll shoot you an email.
danielmewes commentedon Dec 4, 2014
Thanks @marshall007 , we'll look into it.
mlucy commentedon Dec 4, 2014
I would bet that this is because the client code is destructively modifying a single document, while the server code is repeatedly constructing new documents. This is because in general terms aren't smart enough to figure out when they are the only ones with a reference to an object, and thus when it is safe to modify that object destructively. If we fixed that (and I think we have to fix it at some point), a lot of queries (including this one) would probably get much faster. Another way we could fix this particular problem would be to move to functional objects so that we don't have to copy the whole thing every time we construct a new one.
deontologician commentedon Dec 4, 2014
@mlucy So query data structures don't do any sharing currently?
mlucy commentedon Dec 4, 2014
@deontologician: They share keys values but not object structure. If you merge an object with 1000 keys and an object with 1 key, we create a new object with 1001 keys. We don't copy any of the keys or values, which are reference counted, but we do have to build up a new 1001-key object structure. If we used functional objects, we could re-use part of the old object structure and do O(log(1000)) work. If we were able to detect when it's safe to destructively modify objects, we could just patch the big object and do O(1) work.
timmaxw commentedon Dec 4, 2014
If we were to go with the mutability approach, here's one way we could do it:
datum_t, so it's move-only, but add an explicit.copy()method. This would make it obvious where datums were being copied and make it easy to eliminate unnecessary copies.counted_thas a refcount of 1, and if so, operate destructively..copy(), but we should special-case the situation where a lambda uses its argument exactly once so it doesn't do any copying.mlucy commentedon Dec 4, 2014
I'm always a little wary of the "check whether the refcount is 1" approach because small changes to the code that inadvertently produce a copy or keep an old copy around longer can cause huge, silent performance regressions. That said, it might be the easiest way to actually implement mutability in a reasonable amount of time.
deontologician commentedon Dec 5, 2014
They did something similar in GHC called stream-fusion, where it detects a pipeline of folds/maps etc and optimizes it into a single pass on a mutable data structure to avoid copying. I doubt any of the specific techniques they used would be applicable though.
marshall007 commentedon Dec 5, 2014
You'd still miss cases like
r.branch(right.hasFields('foo'), left.merge(right), left), right? Instead of trying to disambiguate the cases where it's safe, you could make all merge operations destructive by default and introduce something liker.clone()orr.copy()for when it's needed.thelinuxlich commentedon Feb 17, 2015
The recommendation engine I've ported to RethinkDB can benefit a lot from this, I guess:
https://github.com/grahamjenson/ger/blob/master/lib/rethinkdb_esm.coffee#L288