10
10
) ]
11
11
extern "system" { }
12
12
13
+ use std:: time:: Duration ;
13
14
use criterion:: { criterion_group, criterion_main, BatchSize , Criterion } ;
15
+ use tokio:: runtime:: Runtime ;
16
+
14
17
use mlua:: prelude:: * ;
15
18
19
+ fn collect_gc_twice ( lua : & Lua ) {
20
+ lua. gc_collect ( ) . unwrap ( ) ;
21
+ lua. gc_collect ( ) . unwrap ( ) ;
22
+ }
23
+
16
24
fn create_table ( c : & mut Criterion ) {
17
- c. bench_function ( "create table" , |b| {
18
- b. iter_batched_ref (
19
- || Lua :: new ( ) ,
20
- |lua| {
25
+ let lua = Lua :: new ( ) ;
26
+
27
+ c. bench_function ( "create [table empty]" , |b| {
28
+ b. iter_batched (
29
+ || collect_gc_twice ( & lua) ,
30
+ |_| {
21
31
lua. create_table ( ) . unwrap ( ) ;
22
32
} ,
23
33
BatchSize :: SmallInput ,
@@ -26,12 +36,14 @@ fn create_table(c: &mut Criterion) {
26
36
}
27
37
28
38
fn create_array ( c : & mut Criterion ) {
29
- c. bench_function ( "create array 10" , |b| {
30
- b. iter_batched_ref (
31
- || Lua :: new ( ) ,
32
- |lua| {
39
+ let lua = Lua :: new ( ) ;
40
+
41
+ c. bench_function ( "create [array] 10" , |b| {
42
+ b. iter_batched (
43
+ || collect_gc_twice ( & lua) ,
44
+ |_| {
33
45
let table = lua. create_table ( ) . unwrap ( ) ;
34
- for i in 1 ..11 {
46
+ for i in 1 ..= 10 {
35
47
table. set ( i, i) . unwrap ( ) ;
36
48
}
37
49
} ,
@@ -41,10 +53,12 @@ fn create_array(c: &mut Criterion) {
41
53
}
42
54
43
55
fn create_string_table ( c : & mut Criterion ) {
44
- c. bench_function ( "create string table 10" , |b| {
45
- b. iter_batched_ref (
46
- || Lua :: new ( ) ,
47
- |lua| {
56
+ let lua = Lua :: new ( ) ;
57
+
58
+ c. bench_function ( "create [table string] 10" , |b| {
59
+ b. iter_batched (
60
+ || collect_gc_twice ( & lua) ,
61
+ |_| {
48
62
let table = lua. create_table ( ) . unwrap ( ) ;
49
63
for & s in & [ "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" ] {
50
64
let s = lua. create_string ( s) . unwrap ( ) ;
@@ -56,114 +70,106 @@ fn create_string_table(c: &mut Criterion) {
56
70
} ) ;
57
71
}
58
72
59
- fn call_add_function ( c : & mut Criterion ) {
60
- c. bench_function ( "call add function 3 10" , |b| {
73
+ fn call_lua_function ( c : & mut Criterion ) {
74
+ let lua = Lua :: new ( ) ;
75
+
76
+ c. bench_function ( "call Lua function [sum] 3 10" , |b| {
61
77
b. iter_batched_ref (
62
78
|| {
63
- let lua = Lua :: new ( ) ;
64
- let f = {
65
- let f: LuaFunction = lua
66
- . load (
67
- r#"
68
- function(a, b, c)
69
- return a + b + c
70
- end
71
- "# ,
72
- )
73
- . eval ( )
74
- . unwrap ( ) ;
75
- lua. create_registry_value ( f) . unwrap ( )
76
- } ;
77
- ( lua, f)
78
- } ,
79
- |( lua, f) | {
80
- let add_function: LuaFunction = lua. registry_value ( f) . unwrap ( ) ;
79
+ collect_gc_twice ( & lua) ;
80
+ lua. load ( "function(a, b, c) return a + b + c end" )
81
+ . eval :: < LuaFunction > ( )
82
+ . unwrap ( )
83
+ } ,
84
+ |function| {
81
85
for i in 0 ..10 {
82
- let _result: i64 = add_function . call ( ( i, i + 1 , i + 2 ) ) . unwrap ( ) ;
86
+ let _result: i64 = function . call ( ( i, i + 1 , i + 2 ) ) . unwrap ( ) ;
83
87
}
84
88
} ,
85
89
BatchSize :: SmallInput ,
86
90
) ;
87
91
} ) ;
88
92
}
89
93
90
- fn call_add_callback ( c : & mut Criterion ) {
91
- c. bench_function ( "call callback add 2 10" , |b| {
94
+ fn call_sum_callback ( c : & mut Criterion ) {
95
+ let lua = Lua :: new ( ) ;
96
+ let callback = lua
97
+ . create_function ( |_, ( a, b, c) : ( i64 , i64 , i64 ) | Ok ( a + b + c) )
98
+ . unwrap ( ) ;
99
+ lua. globals ( ) . set ( "callback" , callback) . unwrap ( ) ;
100
+
101
+ c. bench_function ( "call Rust callback [sum] 3 10" , |b| {
92
102
b. iter_batched_ref (
93
103
|| {
94
- let lua = Lua :: new ( ) ;
95
- let f = {
96
- let c: LuaFunction = lua
97
- . create_function ( |_, ( a, b, c) : ( i64 , i64 , i64 ) | Ok ( a + b + c) )
98
- . unwrap ( ) ;
99
- lua. globals ( ) . set ( "callback" , c) . unwrap ( ) ;
100
- let f: LuaFunction = lua
101
- . load (
102
- r#"
103
- function()
104
- for i = 1,10 do
105
- callback(i, i, i)
106
- end
107
- end
108
- "# ,
109
- )
110
- . eval ( )
111
- . unwrap ( ) ;
112
- lua. create_registry_value ( f) . unwrap ( )
113
- } ;
114
- ( lua, f)
115
- } ,
116
- |( lua, f) | {
117
- let entry_function: LuaFunction = lua. registry_value ( f) . unwrap ( ) ;
118
- entry_function. call :: < _ , ( ) > ( ( ) ) . unwrap ( ) ;
104
+ collect_gc_twice ( & lua) ;
105
+ lua. load ( "function() for i = 1,10 do callback(i, i+1, i+2) end end" )
106
+ . eval :: < LuaFunction > ( )
107
+ . unwrap ( )
108
+ } ,
109
+ |function| {
110
+ function. call :: < _ , ( ) > ( ( ) ) . unwrap ( ) ;
119
111
} ,
120
112
BatchSize :: SmallInput ,
121
113
) ;
122
114
} ) ;
123
115
}
124
116
125
- fn call_append_callback ( c : & mut Criterion ) {
126
- c. bench_function ( "call callback append 10" , |b| {
117
+ fn call_async_sum_callback ( c : & mut Criterion ) {
118
+ let lua = Lua :: new ( ) ;
119
+ let callback = lua
120
+ . create_async_function ( |_, ( a, b, c) : ( i64 , i64 , i64 ) | async move { Ok ( a + b + c) } )
121
+ . unwrap ( ) ;
122
+ lua. globals ( ) . set ( "callback" , callback) . unwrap ( ) ;
123
+
124
+ c. bench_function ( "call async Rust callback [sum] 3 10" , |b| {
125
+ let rt = Runtime :: new ( ) . unwrap ( ) ;
126
+ b. to_async ( rt) . iter_batched (
127
+ || {
128
+ collect_gc_twice ( & lua) ;
129
+ lua. load ( "function() for i = 1,10 do callback(i, i+1, i+2) end end" )
130
+ . eval :: < LuaFunction > ( )
131
+ . unwrap ( )
132
+ } ,
133
+ |function| async move {
134
+ function. call_async :: < _ , ( ) > ( ( ) ) . await . unwrap ( ) ;
135
+ } ,
136
+ BatchSize :: SmallInput ,
137
+ ) ;
138
+ } ) ;
139
+ }
140
+
141
+ fn call_concat_callback ( c : & mut Criterion ) {
142
+ let lua = Lua :: new ( ) ;
143
+ let callback = lua
144
+ . create_function ( |_, ( a, b) : ( LuaString , LuaString ) | {
145
+ Ok ( format ! ( "{}{}" , a. to_str( ) ?, b. to_str( ) ?) )
146
+ } )
147
+ . unwrap ( ) ;
148
+ lua. globals ( ) . set ( "callback" , callback) . unwrap ( ) ;
149
+
150
+ c. bench_function ( "call Rust callback [concat string] 10" , |b| {
127
151
b. iter_batched_ref (
128
152
|| {
129
- let lua = Lua :: new ( ) ;
130
- let f = {
131
- let c: LuaFunction = lua
132
- . create_function ( |_, ( a, b) : ( LuaString , LuaString ) | {
133
- Ok ( format ! ( "{}{}" , a. to_str( ) ?, b. to_str( ) ?) )
134
- } )
135
- . unwrap ( ) ;
136
- lua. globals ( ) . set ( "callback" , c) . unwrap ( ) ;
137
- let f: LuaFunction = lua
138
- . load (
139
- r#"
140
- function()
141
- for _ = 1,10 do
142
- callback("a", "b")
143
- end
144
- end
145
- "# ,
146
- )
147
- . eval ( )
148
- . unwrap ( ) ;
149
- lua. create_registry_value ( f) . unwrap ( )
150
- } ;
151
- ( lua, f)
152
- } ,
153
- |( lua, f) | {
154
- let entry_function: LuaFunction = lua. registry_value ( f) . unwrap ( ) ;
155
- entry_function. call :: < _ , ( ) > ( ( ) ) . unwrap ( ) ;
153
+ collect_gc_twice ( & lua) ;
154
+ lua. load ( "function() for i = 1,10 do callback('a', tostring(i)) end end" )
155
+ . eval :: < LuaFunction > ( )
156
+ . unwrap ( )
157
+ } ,
158
+ |function| {
159
+ function. call :: < _ , ( ) > ( ( ) ) . unwrap ( ) ;
156
160
} ,
157
161
BatchSize :: SmallInput ,
158
162
) ;
159
163
} ) ;
160
164
}
161
165
162
166
fn create_registry_values ( c : & mut Criterion ) {
163
- c. bench_function ( "create registry 10" , |b| {
164
- b. iter_batched_ref (
165
- || Lua :: new ( ) ,
166
- |lua| {
167
+ let lua = Lua :: new ( ) ;
168
+
169
+ c. bench_function ( "create [registry value] 10" , |b| {
170
+ b. iter_batched (
171
+ || collect_gc_twice ( & lua) ,
172
+ |_| {
167
173
for _ in 0 ..10 {
168
174
lua. create_registry_value ( lua. pack ( true ) . unwrap ( ) ) . unwrap ( ) ;
169
175
}
@@ -178,10 +184,12 @@ fn create_userdata(c: &mut Criterion) {
178
184
struct UserData ( i64 ) ;
179
185
impl LuaUserData for UserData { }
180
186
181
- c. bench_function ( "create userdata 10" , |b| {
182
- b. iter_batched_ref (
183
- || Lua :: new ( ) ,
184
- |lua| {
187
+ let lua = Lua :: new ( ) ;
188
+
189
+ c. bench_function ( "create [table userdata] 10" , |b| {
190
+ b. iter_batched (
191
+ || collect_gc_twice ( & lua) ,
192
+ |_| {
185
193
let table: LuaTable = lua. create_table ( ) . unwrap ( ) ;
186
194
for i in 1 ..11 {
187
195
table. set ( i, UserData ( i) ) . unwrap ( ) ;
@@ -192,20 +200,80 @@ fn create_userdata(c: &mut Criterion) {
192
200
} ) ;
193
201
}
194
202
203
+ fn call_userdata_method ( c : & mut Criterion ) {
204
+ struct UserData ( i64 ) ;
205
+ impl LuaUserData for UserData {
206
+ fn add_methods < ' lua , M : LuaUserDataMethods < ' lua , Self > > ( methods : & mut M ) {
207
+ methods. add_method ( "method" , |_, this, ( ) | Ok ( this. 0 ) ) ;
208
+ }
209
+ }
210
+
211
+ let lua = Lua :: new ( ) ;
212
+ lua. globals ( ) . set ( "userdata" , UserData ( 10 ) ) . unwrap ( ) ;
213
+
214
+ c. bench_function ( "call [userdata method] 10" , |b| {
215
+ b. iter_batched_ref (
216
+ || {
217
+ collect_gc_twice ( & lua) ;
218
+ lua. load ( "function() for i = 1,10 do userdata:method() end end" )
219
+ . eval :: < LuaFunction > ( )
220
+ . unwrap ( )
221
+ } ,
222
+ |function| {
223
+ function. call :: < _ , ( ) > ( ( ) ) . unwrap ( ) ;
224
+ } ,
225
+ BatchSize :: SmallInput ,
226
+ ) ;
227
+ } ) ;
228
+ }
229
+
230
+ fn call_async_userdata_method ( c : & mut Criterion ) {
231
+ #[ derive( Clone , Copy ) ]
232
+ struct UserData ( i64 ) ;
233
+ impl LuaUserData for UserData {
234
+ fn add_methods < ' lua , M : LuaUserDataMethods < ' lua , Self > > ( methods : & mut M ) {
235
+ methods. add_async_method ( "method" , |_, this, ( ) | async move { Ok ( this. 0 ) } ) ;
236
+ }
237
+ }
238
+
239
+ let lua = Lua :: new ( ) ;
240
+ lua. globals ( ) . set ( "userdata" , UserData ( 10 ) ) . unwrap ( ) ;
241
+
242
+ c. bench_function ( "call async [userdata method] 10" , |b| {
243
+ let rt = Runtime :: new ( ) . unwrap ( ) ;
244
+ b. to_async ( rt) . iter_batched (
245
+ || {
246
+ collect_gc_twice ( & lua) ;
247
+ lua. load ( "function() for i = 1,10 do userdata:method() end end" )
248
+ . eval :: < LuaFunction > ( )
249
+ . unwrap ( )
250
+ } ,
251
+ |function| async move {
252
+ function. call_async :: < _ , ( ) > ( ( ) ) . await . unwrap ( ) ;
253
+ } ,
254
+ BatchSize :: SmallInput ,
255
+ ) ;
256
+ } ) ;
257
+ }
258
+
195
259
criterion_group ! {
196
260
name = benches;
197
261
config = Criterion :: default ( )
198
- . sample_size( 200 )
262
+ . sample_size( 300 )
263
+ . measurement_time( Duration :: from_secs( 10 ) )
199
264
. noise_threshold( 0.02 ) ;
200
265
targets =
201
266
create_table,
202
267
create_array,
203
268
create_string_table,
204
- call_add_function,
205
- call_add_callback,
206
- call_append_callback,
269
+ call_lua_function,
270
+ call_sum_callback,
271
+ call_async_sum_callback,
272
+ call_concat_callback,
207
273
create_registry_values,
208
274
create_userdata,
275
+ call_userdata_method,
276
+ call_async_userdata_method,
209
277
}
210
278
211
279
criterion_main ! ( benches) ;
0 commit comments