opt_ar 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.circleci/config.yml +51 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +2 -0
- data/Gemfile +13 -3
- data/Gemfile.lock +67 -19
- data/README.md +141 -7
- data/Rakefile +22 -0
- data/benchmarks/benchmark_1.rb +131 -0
- data/benchmarks/benchmark_2.rb +100 -0
- data/benchmarks/benchmark_3.rb +96 -0
- data/benchmarks/benchmark_4.rb +101 -0
- data/benchmarks/benchmark_5.rb +108 -0
- data/benchmarks/benchmark_6.rb +60 -0
- data/benchmarks/employee_creation.rb +16 -0
- data/lib/opt_ar.rb +9 -23
- data/lib/opt_ar/core_ext/active_record/relation.rb +43 -3
- data/lib/opt_ar/core_ext/array.rb +13 -3
- data/lib/opt_ar/errors.rb +6 -0
- data/lib/opt_ar/helpers/method_finder_helper.rb +3 -0
- data/lib/opt_ar/oar.rb +22 -4
- data/lib/opt_ar/optimal_ar/builder.rb +13 -22
- data/lib/opt_ar/version.rb +1 -1
- data/opt_ar.gemspec +4 -1
- metadata +13 -3
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'benchmark/memory'
|
2
|
+
|
3
|
+
def bm_time
|
4
|
+
old_logger = ActiveRecord::Base.logger
|
5
|
+
ActiveRecord::Base.logger = nil
|
6
|
+
Benchmark.bm(12) do |x|
|
7
|
+
x.report('AR') do
|
8
|
+
emps = Employee.male_employees.to_a
|
9
|
+
emps.last.emp_id
|
10
|
+
end
|
11
|
+
|
12
|
+
x.report('AR select') do
|
13
|
+
emps = Employee.male_employees.select(%i[emp_id first_name last_name]).to_a
|
14
|
+
emps.last.emp_id
|
15
|
+
end
|
16
|
+
|
17
|
+
x.report('OptAR') do
|
18
|
+
emps = Employee.male_employees.optars
|
19
|
+
emps.last.emp_id
|
20
|
+
end
|
21
|
+
|
22
|
+
x.report('OptAR req') do
|
23
|
+
emps = Employee.male_employees.optars(
|
24
|
+
req_attribute: %i[emp_id first_name last_name]
|
25
|
+
)
|
26
|
+
emps.last.emp_id
|
27
|
+
end
|
28
|
+
end
|
29
|
+
ActiveRecord::Base.logger = old_logger
|
30
|
+
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
|
34
|
+
def bm_mem
|
35
|
+
old_logger = ActiveRecord::Base.logger
|
36
|
+
ActiveRecord::Base.logger = nil
|
37
|
+
|
38
|
+
Benchmark.memory do |x|
|
39
|
+
x.compare!
|
40
|
+
x.report('AR') do
|
41
|
+
emps = Employee.male_employees.to_a
|
42
|
+
emps.last.emp_id
|
43
|
+
end
|
44
|
+
|
45
|
+
x.report('AR select') do
|
46
|
+
emps = Employee.male_employees.select(%i[emp_id first_name last_name]).to_a
|
47
|
+
emps.last.emp_id
|
48
|
+
end
|
49
|
+
|
50
|
+
x.report('OptAR') do
|
51
|
+
emps = Employee.male_employees.optars
|
52
|
+
emps.last.emp_id
|
53
|
+
end
|
54
|
+
|
55
|
+
x.report('OptAR req') do
|
56
|
+
emps = Employee.male_employees.optars(
|
57
|
+
req_attribute: %i[emp_id first_name last_name]
|
58
|
+
)
|
59
|
+
emps.last.emp_id
|
60
|
+
end
|
61
|
+
end
|
62
|
+
ActiveRecord::Base.logger = old_logger
|
63
|
+
|
64
|
+
nil
|
65
|
+
end
|
66
|
+
|
67
|
+
bm_time
|
68
|
+
|
69
|
+
bm_mem
|
70
|
+
|
71
|
+
# AR 11.375M memsize ( 1.208k retained)
|
72
|
+
# 116.427k objects ( 1.000 retained)
|
73
|
+
# 50.000 strings ( 0.000 retained)
|
74
|
+
# AR select 5.716M memsize ( 1.208k retained)
|
75
|
+
# 66.721k objects ( 1.000 retained)
|
76
|
+
# 50.000 strings ( 0.000 retained)
|
77
|
+
# OptAR 5.522M memsize ( 1.208k retained)
|
78
|
+
# 77.744k objects ( 1.000 retained)
|
79
|
+
# 24.000 strings ( 0.000 retained)
|
80
|
+
# OptAR req 5.522M memsize ( 1.208k retained)
|
81
|
+
# 77.745k objects ( 1.000 retained)
|
82
|
+
# 24.000 strings ( 0.000 retained)
|
83
|
+
|
84
|
+
# Comparison:
|
85
|
+
# OptAR: 5521999 allocated
|
86
|
+
# OptAR req: 5522231 allocated - 1.00x more
|
87
|
+
# AR select: 5715872 allocated - 1.04x more
|
88
|
+
# AR: 11374672 allocated - 2.06x more
|
89
|
+
|
90
|
+
# user system total real
|
91
|
+
# AR 0.120000 0.000000 0.120000 ( 0.129684)
|
92
|
+
# AR select 0.070000 0.010000 0.080000 ( 0.081367)
|
93
|
+
# OptAR 0.050000 0.000000 0.050000 ( 0.056168)
|
94
|
+
# OptAR req 0.040000 0.000000 0.040000 ( 0.057154)
|
95
|
+
|
96
|
+
# user system total real
|
97
|
+
# AR 0.180000 0.010000 0.190000 ( 0.218083)
|
98
|
+
# AR select 0.090000 0.000000 0.090000 ( 0.101246)
|
99
|
+
# OptAR 0.050000 0.000000 0.050000 ( 0.061082)
|
100
|
+
# OptAR req 0.050000 0.000000 0.050000 ( 0.056118)
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'benchmark/memory'
|
2
|
+
|
3
|
+
def bm_time
|
4
|
+
old_logger = ActiveRecord::Base.logger
|
5
|
+
ActiveRecord::Base.logger = nil
|
6
|
+
Benchmark.bm(12) do |x|
|
7
|
+
x.report('AR') do
|
8
|
+
emps = Employee.male_employees.to_a
|
9
|
+
Marshal.dump(emps)
|
10
|
+
end
|
11
|
+
|
12
|
+
x.report('AR select') do
|
13
|
+
emps = Employee.male_employees.select(%i[emp_id first_name last_name]).to_a
|
14
|
+
Marshal.dump(emps)
|
15
|
+
end
|
16
|
+
|
17
|
+
x.report('OptAR') do
|
18
|
+
emps = Employee.male_employees.optars
|
19
|
+
Marshal.dump(emps)
|
20
|
+
end
|
21
|
+
|
22
|
+
x.report('OptAR req') do
|
23
|
+
emps = Employee.male_employees.optars(
|
24
|
+
req_attribute: %i[emp_id first_name last_name]
|
25
|
+
)
|
26
|
+
Marshal.dump(emps)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
ActiveRecord::Base.logger = old_logger
|
30
|
+
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
|
34
|
+
def bm_mem
|
35
|
+
old_logger = ActiveRecord::Base.logger
|
36
|
+
ActiveRecord::Base.logger = nil
|
37
|
+
|
38
|
+
Benchmark.memory do |x|
|
39
|
+
x.compare!
|
40
|
+
x.report('AR') do
|
41
|
+
emps = Employee.male_employees.to_a
|
42
|
+
Marshal.dump(emps)
|
43
|
+
end
|
44
|
+
|
45
|
+
x.report('AR select') do
|
46
|
+
emps = Employee.male_employees.select(%i[emp_id first_name last_name]).to_a
|
47
|
+
Marshal.dump(emps)
|
48
|
+
end
|
49
|
+
|
50
|
+
x.report('OptAR') do
|
51
|
+
emps = Employee.male_employees.optars
|
52
|
+
Marshal.dump(emps)
|
53
|
+
end
|
54
|
+
|
55
|
+
x.report('OptAR req') do
|
56
|
+
emps = Employee.male_employees.optars(
|
57
|
+
req_attribute: %i[emp_id first_name last_name]
|
58
|
+
)
|
59
|
+
Marshal.dump(emps)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
ActiveRecord::Base.logger = old_logger
|
63
|
+
|
64
|
+
nil
|
65
|
+
end
|
66
|
+
|
67
|
+
# AR 20.771M memsize ( 1.208k retained)
|
68
|
+
# 232.563k objects ( 1.000 retained)
|
69
|
+
# 50.000 strings ( 0.000 retained)
|
70
|
+
# AR select 8.314M memsize ( 1.208k retained)
|
71
|
+
# 105.451k objects ( 1.000 retained)
|
72
|
+
# 50.000 strings ( 0.000 retained)
|
73
|
+
# OptAR 6.227M memsize ( 1.208k retained)
|
74
|
+
# 88.802k objects ( 1.000 retained)
|
75
|
+
# 25.000 strings ( 0.000 retained)
|
76
|
+
# OptAR req 6.227M memsize ( 1.208k retained)
|
77
|
+
# 88.803k objects ( 1.000 retained)
|
78
|
+
# 25.000 strings ( 0.000 retained)
|
79
|
+
|
80
|
+
# Comparison:
|
81
|
+
# OptAR: 6226520 allocated
|
82
|
+
# OptAR req: 6226752 allocated - 1.00x more
|
83
|
+
# AR select: 8313821 allocated - 1.34x more
|
84
|
+
# AR: 20771357 allocated - 3.34x more
|
85
|
+
|
86
|
+
# user system total real
|
87
|
+
# AR 0.350000 0.010000 0.360000 ( 0.377849)
|
88
|
+
# AR select 0.170000 0.010000 0.180000 ( 0.185033)
|
89
|
+
# OptAR 0.060000 0.000000 0.060000 ( 0.075719)
|
90
|
+
# OptAR req 0.080000 0.010000 0.090000 ( 0.087333)
|
91
|
+
|
92
|
+
# user system total real
|
93
|
+
# AR 0.320000 0.000000 0.320000 ( 0.343211)
|
94
|
+
# AR select 0.200000 0.010000 0.210000 ( 0.209019)
|
95
|
+
# OptAR 0.060000 0.000000 0.060000 ( 0.075407)
|
96
|
+
# OptAR req 0.070000 0.000000 0.070000 ( 0.076678)
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'benchmark/memory'
|
2
|
+
|
3
|
+
def bm_time
|
4
|
+
old_logger = ActiveRecord::Base.logger
|
5
|
+
ActiveRecord::Base.logger = n
|
6
|
+
Benchmark.bm(12) do |x|
|
7
|
+
x.report('AR') do
|
8
|
+
emps = Employee.all.to_a
|
9
|
+
Marshal.dump(emps)
|
10
|
+
end
|
11
|
+
|
12
|
+
x.report('AR select') do
|
13
|
+
emps = Employee.all.select([:emp_id, :first_name, :last_name, :created_at]).to_a
|
14
|
+
Marshal.dump(emps)
|
15
|
+
end
|
16
|
+
|
17
|
+
x.report('OptAR') do
|
18
|
+
emps = Employee.all.optars
|
19
|
+
Marshal.dump(emps)
|
20
|
+
end
|
21
|
+
|
22
|
+
x.report('OptAR req') do
|
23
|
+
emps = Employee.all.optars(
|
24
|
+
req_attribute: [:emp_id, :first_name, :last_name, :created_at]
|
25
|
+
)
|
26
|
+
Marshal.dump(emps)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
ActiveRecord::Base.logger = old_logger
|
30
|
+
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
|
34
|
+
def bm_mem
|
35
|
+
old_logger = ActiveRecord::Base.logger
|
36
|
+
ActiveRecord::Base.logger = nil
|
37
|
+
|
38
|
+
Benchmark.memory do |x|
|
39
|
+
x.compare!
|
40
|
+
x.report('AR') do
|
41
|
+
emps = Employee.all.to_a
|
42
|
+
Marshal.dump(emps)
|
43
|
+
end
|
44
|
+
|
45
|
+
x.report('AR select') do
|
46
|
+
emps = Employee.all.select([:emp_id, :first_name, :last_name, :created_at]).to_a
|
47
|
+
Marshal.dump(emps)
|
48
|
+
end
|
49
|
+
|
50
|
+
x.report('OptAR') do
|
51
|
+
emps = Employee.all.optars
|
52
|
+
Marshal.dump(emps)
|
53
|
+
end
|
54
|
+
|
55
|
+
x.report('OptAR req') do
|
56
|
+
emps = Employee.all.optars(
|
57
|
+
req_attribute: [:emp_id, :first_name, :last_name, :created_at]
|
58
|
+
)
|
59
|
+
Marshal.dump(emps)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
ActiveRecord::Base.logger = old_logger
|
63
|
+
|
64
|
+
nil
|
65
|
+
end
|
66
|
+
|
67
|
+
bm_time
|
68
|
+
|
69
|
+
bm_mem
|
70
|
+
|
71
|
+
|
72
|
+
# user system total real
|
73
|
+
# AR 0.660000 0.000000 0.660000 ( 0.692568)
|
74
|
+
# AR select 0.480000 0.010000 0.490000 ( 0.492313)
|
75
|
+
# OptAR 0.140000 0.010000 0.150000 ( 0.152383)
|
76
|
+
# OptAR req 0.150000 0.000000 0.150000 ( 0.168766)
|
77
|
+
|
78
|
+
# user system total real
|
79
|
+
# AR 0.690000 0.010000 0.700000 ( 0.713360)
|
80
|
+
# AR select 0.490000 0.010000 0.500000 ( 0.505020)
|
81
|
+
# OptAR 0.150000 0.010000 0.160000 ( 0.163765)
|
82
|
+
# OptAR req 0.160000 0.000000 0.160000 ( 0.169661)
|
83
|
+
|
84
|
+
# AR 41.403M memsize ( 1.208k retained)
|
85
|
+
# 464.023k objects ( 1.000 retained)
|
86
|
+
# 50.000 strings ( 0.000 retained)
|
87
|
+
# AR select 23.144M memsize ( 1.208k retained)
|
88
|
+
# 298.469k objects ( 1.000 retained)
|
89
|
+
# 50.000 strings ( 0.000 retained)
|
90
|
+
# OptAR 12.379M memsize ( 1.208k retained)
|
91
|
+
# 176.924k objects ( 1.000 retained)
|
92
|
+
# 20.000 strings ( 0.000 retained)
|
93
|
+
# OptAR req 12.379M memsize ( 1.208k retained)
|
94
|
+
# 176.925k objects ( 1.000 retained)
|
95
|
+
# 20.000 strings ( 0.000 retained)
|
96
|
+
|
97
|
+
# Comparison:
|
98
|
+
# OptAR: 12379226 allocated
|
99
|
+
# OptAR req: 12379490 allocated - 1.00x more
|
100
|
+
# AR select: 23144122 allocated - 1.87x more
|
101
|
+
# AR: 41403038 allocated - 3.34x more
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'benchmark/memory'
|
2
|
+
|
3
|
+
emps = Employee.all.to_a
|
4
|
+
m1 = Marshal.dump(emps)
|
5
|
+
|
6
|
+
emps = Employee.all.select([:emp_id, :first_name, :last_name, :created_at]).to_a
|
7
|
+
m2 = Marshal.dump(emps)
|
8
|
+
|
9
|
+
emps = Employee.all.optars
|
10
|
+
m3 = Marshal.dump(emps)
|
11
|
+
|
12
|
+
emps = Employee.all.optars(req_attribute: [:emp_id, :first_name, :last_name, :created_at])
|
13
|
+
m4 = Marshal.dump(emps)
|
14
|
+
|
15
|
+
# 2.2.3 :198 > ObjectSpace.memsize_of m1
|
16
|
+
# => 8388649
|
17
|
+
# 2.2.3 :199 > ObjectSpace.memsize_of m2
|
18
|
+
# => 4194345
|
19
|
+
# 2.2.3 :200 > ObjectSpace.memsize_of m3
|
20
|
+
# => 524329
|
21
|
+
# 2.2.3 :201 > ObjectSpace.memsize_of m4
|
22
|
+
# => 524329
|
23
|
+
|
24
|
+
def bm_time
|
25
|
+
old_logger = ActiveRecord::Base.logger
|
26
|
+
ActiveRecord::Base.logger = nil
|
27
|
+
Benchmark.bm(12) do |x|
|
28
|
+
x.report('AR') do
|
29
|
+
Marshal.load(@m1)
|
30
|
+
end
|
31
|
+
|
32
|
+
x.report('AR select') do
|
33
|
+
Marshal.load(@m2)
|
34
|
+
end
|
35
|
+
|
36
|
+
x.report('OptAR') do
|
37
|
+
Marshal.load(@m3)
|
38
|
+
end
|
39
|
+
|
40
|
+
x.report('OptAR req') do
|
41
|
+
Marshal.load(@m4)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
ActiveRecord::Base.logger = old_logger
|
45
|
+
|
46
|
+
nil
|
47
|
+
end
|
48
|
+
|
49
|
+
def bm_mem
|
50
|
+
old_logger = ActiveRecord::Base.logger
|
51
|
+
ActiveRecord::Base.logger = nil
|
52
|
+
|
53
|
+
Benchmark.memory do |x|
|
54
|
+
x.compare!
|
55
|
+
x.report('AR') do
|
56
|
+
Marshal.load(@m1)
|
57
|
+
end
|
58
|
+
|
59
|
+
x.report('AR select') do
|
60
|
+
Marshal.load(@m2)
|
61
|
+
end
|
62
|
+
|
63
|
+
x.report('OptAR') do
|
64
|
+
Marshal.load(@m3)
|
65
|
+
end
|
66
|
+
|
67
|
+
x.report('OptAR req') do
|
68
|
+
Marshal.load(@m4)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
ActiveRecord::Base.logger = old_logger
|
72
|
+
|
73
|
+
nil
|
74
|
+
end
|
75
|
+
bm_time
|
76
|
+
|
77
|
+
bm_mem
|
78
|
+
|
79
|
+
# user system total real
|
80
|
+
# AR 0.800000 0.120000 0.920000 ( 0.926619)
|
81
|
+
# AR select 0.560000 0.000000 0.560000 ( 0.565152)
|
82
|
+
# OptAR 0.050000 0.000000 0.050000 ( 0.047571)
|
83
|
+
# OptAR req 0.050000 0.000000 0.050000 ( 0.053500)
|
84
|
+
|
85
|
+
# user system total real
|
86
|
+
# AR 0.850000 0.010000 0.860000 ( 0.858270)
|
87
|
+
# AR select 0.560000 0.000000 0.560000 ( 0.576175)
|
88
|
+
# OptAR 0.060000 0.000000 0.060000 ( 0.059859)
|
89
|
+
# OptAR req 0.080000 0.000000 0.080000 ( 0.074988)
|
90
|
+
|
91
|
+
# AR 53.890M memsize ( 0.000 retained)
|
92
|
+
# 607.395k objects ( 0.000 retained)
|
93
|
+
# 50.000 strings ( 0.000 retained)
|
94
|
+
# AR select 33.837M memsize ( 0.000 retained)
|
95
|
+
# 430.717k objects ( 0.000 retained)
|
96
|
+
# 50.000 strings ( 0.000 retained)
|
97
|
+
# OptAR 3.975M memsize ( 0.000 retained)
|
98
|
+
# 44.173k objects ( 0.000 retained)
|
99
|
+
# 4.000 strings ( 0.000 retained)
|
100
|
+
# OptAR req 3.975M memsize ( 0.000 retained)
|
101
|
+
# 44.173k objects ( 0.000 retained)
|
102
|
+
# 4.000 strings ( 0.000 retained)
|
103
|
+
|
104
|
+
# Comparison:
|
105
|
+
# OptAR req: 3975400 allocated
|
106
|
+
# OptAR: 3975400 allocated - same
|
107
|
+
# AR select: 33837085 allocated - 8.51x more
|
108
|
+
# AR: 53889558 allocated - 13.56x more
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'benchmark/ips'
|
2
|
+
|
3
|
+
old_logger = ActiveRecord::Base.logger
|
4
|
+
ActiveRecord::Base.logger = nil
|
5
|
+
|
6
|
+
emps = Employee.all.to_a
|
7
|
+
@m1 = Marshal.dump(emps)
|
8
|
+
|
9
|
+
emps = Employee.all.select(%i[emp_id first_name last_name created_at]).to_a
|
10
|
+
@m2 = Marshal.dump(emps)
|
11
|
+
|
12
|
+
emps = Employee.all.optars
|
13
|
+
@m3 = Marshal.dump(emps)
|
14
|
+
|
15
|
+
emps = Employee.all.optars(
|
16
|
+
req_attribute %i[emp_id first_name last_name created_at]
|
17
|
+
)
|
18
|
+
@m4 = Marshal.dump(emps)
|
19
|
+
|
20
|
+
Benchmark.ips do |x|
|
21
|
+
x.time = 5
|
22
|
+
x.warmup = 2
|
23
|
+
|
24
|
+
x.report('AR') do
|
25
|
+
Marshal.load(@m1)
|
26
|
+
end
|
27
|
+
|
28
|
+
x.report('AR select') do
|
29
|
+
Marshal.load(@m2)
|
30
|
+
end
|
31
|
+
|
32
|
+
x.report('OptAR') do
|
33
|
+
Marshal.load(@m3)
|
34
|
+
end
|
35
|
+
|
36
|
+
x.report('OptAR req') do
|
37
|
+
Marshal.load(@m4)
|
38
|
+
end
|
39
|
+
|
40
|
+
x.compare!
|
41
|
+
end
|
42
|
+
|
43
|
+
ActiveRecord::Base.logger = old_logger
|
44
|
+
|
45
|
+
# Warming up --------------------------------------
|
46
|
+
# AR 1.000 i/100ms
|
47
|
+
# AR select 1.000 i/100ms
|
48
|
+
# OptAR 2.000 i/100ms
|
49
|
+
# OptAR req 2.000 i/100ms
|
50
|
+
# Calculating -------------------------------------
|
51
|
+
# AR 1.130 (± 0.0%) i/s - 6.000 in 5.394933s
|
52
|
+
# AR select 1.820 (± 0.0%) i/s - 10.000 in 5.620499s
|
53
|
+
# OptAR 21.157 (±18.9%) i/s - 104.000 in 5.088126s
|
54
|
+
# OptAR req 20.879 (±19.2%) i/s - 102.000 in 5.099266s
|
55
|
+
|
56
|
+
# Comparison:
|
57
|
+
# OptAR: 21.2 i/s
|
58
|
+
# OptAR req: 20.9 i/s - same-ish: difference falls within error
|
59
|
+
# AR select: 1.8 i/s - 11.62x slower
|
60
|
+
# AR: 1.1 i/s - 18.72x slower
|