Skip to content

Ruby driver optimizations - #1842

Closed
nviennot wants to merge 2 commits into
rethinkdb:nextfrom
nviennot:rb_optimizations
Closed

nviennot wants to merge 2 commits into
rethinkdb:nextfrom
nviennot:rb_optimizations

Conversation

@nviennot

Copy link
Copy Markdown
Contributor

I'm trying to optimize the Ruby driver due to its poor performance: #1798

One of the issue is that building queries is really long.

Consider this benchmark:

require 'rethinkdb'
require 'benchmark'
extend RethinkDB::Shortcuts
puts Benchmark.measure { 10000.times { r.table("users").filter({:attr => "123"}).orderby(r.asc("id")).limit(10).to_pb } }

On my machine with ruby2.0p353:

On the next branch, the average time to build the query is 0.55ms.
With the "don't parse the backtrace all the time" commit, it drops to 0.31ms.
With the "Refactored the RQL methods" commit, which precompiles all the RQL commands, it drops to 0.27ms.
If we were to make no calls to caller to retrieve the backtrace, it would drop to 0.18ms.

Having the backtrace is nice (which is only used in the case of an error), but is ridiculously expensive.
Note that this test is running with a very small backtrace, but on a real application, the backtrace will be much larger. It's not uncommon to see a backtrace depth of 100 functions in a web application when hitting a controller with instrumentation all around the place.

Taking the 0.27ms baseline (with the two commits merged in), when I run the benchmark in a fresh Ruby-on-Rails console instead of its own process, the average time jumps to 1.2ms. This is really sad.
When removing references to caller, it goes down to 0.58ms which is still sad. I don't understand why I'm not getting the previous 0.18ms, it's probably due to all the crap that gets injected in some core classes.

Regardless, I think the nice to have backtrace feature is not worth the 0.6ms of overhead.

It might even be worth considering some sort of query caching, perhaps like redis: http://redis.io/commands/evalsha or postgresql: http://www.postgresql.org/docs/9.2/static/sql-prepare.html

@mlucy

mlucy commented Jan 10, 2014

Copy link
Copy Markdown
Member

Thanks @nviennot ! This looks awesome. I'll review it either today or Monday.

Could you fill out the CLA (http://rethinkdb.com/community/cla/) if you haven't already?

I agree we should seriously consider making the backtraces an optional feature you can turn on (or maybe they could default to on when you call .repl) rather than using them all the time. Does anybody else object strongly to that?

@nviennot

Copy link
Copy Markdown
Contributor Author

I wasn't sure if I filled the CLA, so I did it.

How can backtrace be turned on/off (globally)?

RethinkDB::RQL.full_backtraces = true somewhat makes sense.

The repl thing is not a good idea I think, because you don't need a backtrace in a repl, you just typed the query, you know where it's coming from.
I think this feature should be disabled by default. When there is an exception, you can have in the exception message "set r.full_backtraces = true if you want more precise backtraces"
If it's enabled by default, people may complain about rethinkdb being slow (esp. when publishing benchmark numbers), and will not know that this is a feature to turn off to get speed.

If it was up to me, I'd remove the funky backtrace feature entirely. Not worth the trouble. The benefits are really small IMHO.

@mlucy

mlucy commented Jan 11, 2014

Copy link
Copy Markdown
Member

If it was up to me, I'd remove the funky backtrace feature entirely. Not worth the trouble. The benefits are really small IMHO.

Depends a lot on your usage pattern. I frequently find myself doing things like this:

def get_date_range(tbl, left, right)
  tbl.between(r.epoch_time(left), r.epoch_time(right), index:'date')
end

def complete_entry_p(entry)
  entry['field1'] & entry['field2'] & entry['field3'] & entry['field4']
end

get_date_range(r.table('test'), t1, t2).filter{|row| r.not(complete_entry_p(row))}

If you're writing code like that, it helps a lot to have the backtrace tell you whether the error occured in the part of the query constructed in get_date_range, the part constructed in complete_entry_p, or the final construction of the query. (Which, in fact, is why it was initially added; I was having a lot of trouble tracking down errors in large programmatically-constructed queries.)

I would believe that most RethinkDB users don't do this, though, so it's probably worth turning it off by default.

@coffeemug

Copy link
Copy Markdown
Contributor

@mlucy -- would you mind opening a separate proposal for turning off backtraces since it's not a part of this pull request? (Also, I'd like to talk about it in person for a few mins to make sure I understand all implications of what's going on)

@mlucy

mlucy commented Jan 11, 2014

Copy link
Copy Markdown
Member

Actually, an argument for removing it entirely is that no other drivers (including the Python and JS drivers) have picked it up.

@mlucy

mlucy commented Jan 11, 2014

Copy link
Copy Markdown
Member

Sure. Let's move discussion of the backtrace issue to #1843 .

@mlucy

mlucy commented Jan 15, 2014

Copy link
Copy Markdown
Member

I did some benchmarking here. The backtraces seem to materially impact performance, but nviennot@f627a73 doesn't seem to change much. Using Daniel's RubyDatabaseBenchmarks in Ruby 2.0 on Electro, it seems to affect the benchmark by <1% (and possibly not at all; it certainly has a smaller effect than the variance between runs). I think the extra .03ms it saves isn't noticeable under normal conditions right now.

Since it makes the logic more complicated (and uses eval), I'd like to avoid merging it unless there's a common use case where we know it improves performance by a noticeable amount. What do other people think?

As for the backtraces, I think we should just drop them entirely (see #1843 ).

@nviennot

Copy link
Copy Markdown
Contributor Author

I'm fine with that decision. On the long run I think we should have all the
methods properly defined for rdoc.

@mlucy

mlucy commented Jan 15, 2014

Copy link
Copy Markdown
Member

Closing; backtraces will be removed entirely as part of #1843 .

@mlucy mlucy closed this Jan 15, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants