Repository navigation
Commit c800cf0
committed
unlock the gvl when calculating hashes / salts
Holding on to the GVL means we can't do anything in parallel. Since
these are CPU-only operations that do not involve the ruby interpreter,
we can safely unlock the GVL when calculating hashes.
This program demonstrates the difference:
```ruby
require 'bcrypt'
require 'thread'
GUESSES = (ENV['GUESSES'] || 100).to_i
THREADS = (ENV['THREADS'] || 1).to_i
p GUESSES: GUESSES, THREADS: THREADS
password = BCrypt::Password.create 'hello world!'
queue = Queue.new
GUESSES.times { queue << "x" * 90 }
THREADS.times { queue << nil }
THREADS.times.map {
Thread.new {
while guess = queue.pop
password == guess
end
}
}.each(&:join)
```
Without this patch:
```
[aaron@TC bcrypt-ruby (master)]$ time THREADS=4 ruby test.rb
{:GUESSES=>100, :THREADS=>4}
real 0m30.014s
user 0m29.739s
sys 0m0.153s
```
With the patch:
```
[aaron@TC bcrypt-ruby (master)]$ time THREADS=4 ruby -Ilib test.rb
{:GUESSES=>100, :THREADS=>4}
real 0m12.293s
user 0m42.382s
sys 0m0.169s
```
If you run this program with the patch, you can see Ruby use nearly 400% CPU,
as long as you have a 4 core machine.1 parent 0612960 commit c800cf0
2 files changed
Lines changed: 77 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
4 | 15 | | |
5 | 16 | | |
6 | 17 | | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
7 | 31 | | |
8 | 32 | | |
9 | 33 | | |
10 | 34 | | |
11 | 35 | | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
18 | 55 | | |
19 | 56 | | |
20 | 57 | | |
| |||
24 | 61 | | |
25 | 62 | | |
26 | 63 | | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
27 | 77 | | |
28 | 78 | | |
29 | 79 | | |
30 | 80 | | |
31 | | - | |
32 | | - | |
33 | 81 | | |
34 | 82 | | |
35 | | - | |
36 | | - | |
| 83 | + | |
37 | 84 | | |
38 | 85 | | |
39 | 86 | | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
45 | 104 | | |
46 | 105 | | |
47 | 106 | | |
48 | | - | |
| 107 | + | |
49 | 108 | | |
50 | | - | |
| 109 | + | |
51 | 110 | | |
52 | 111 | | |
53 | 112 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
0 commit comments