Skip to content

Client-side reduction faster than server-side reduction #3408

Open

Description

@danielmewes

@marshall007 reported the following case over at #2629 (comment) :

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.

## code: 183ms ##
r.table 'standard_terms'
.getAll 'disorders', index: 'field'
.run().then (data) ->
  dict = data.reduce (right, left) ->
    right[left.term] = left
    return right
  , {}

## db: 595ms ##
r.table 'standard_terms'
.getAll 'disorders', index: 'field'
.map (row) -> r.object row('term'), row
.reduce (l, r) -> l.merge r
.run()

@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.

Activity

  1. added this to the subsequent milestone on Dec 4, 2014
  2. thelinuxlich commented on Dec 4, 2014

    @thelinuxlich

    The same thing happens to me in queries using r.object and merge too

  3. marshall007 commented on Dec 4, 2014

    @marshall007
    Contributor
    r.table('standard_terms').count() # => 19556
    r.table('standard_terms').getAll('disorders', index: 'field').count() # => 3192

    Here's an example document. The synonyms array 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),"
      ]
    }
  4. marshall007 commented on Dec 4, 2014

    @marshall007
    Contributor

    @danielmewes if you need more sample data let me know and I'll shoot you an email.

  5. danielmewes commented on Dec 4, 2014

    @danielmewes
    MemberAuthor

    Thanks @marshall007 , we'll look into it.

  6. mlucy commented on Dec 4, 2014

    @mlucy
    Member

    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.

  7. deontologician commented on Dec 4, 2014

    @deontologician
    Contributor

    @mlucy So query data structures don't do any sharing currently?

  8. mlucy commented on Dec 4, 2014

    @mlucy
    Member

    @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.

  9. timmaxw commented on Dec 4, 2014

    @timmaxw
    Member

    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.
  10. mlucy commented on Dec 4, 2014

    @mlucy
    Member

    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.

  11. deontologician commented on Dec 5, 2014

    @deontologician
    Contributor

    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.

  12. marshall007 commented on Dec 5, 2014

    @marshall007
    Contributor

    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.

  13. thelinuxlich commented on Feb 17, 2015

    @thelinuxlich
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions