Skip to content

Commit b1b7fa3

Browse files
bors[bot]Marwes
andauthored
Merge #861
861: Reduce the function call overhead somewhat r=Marwes a=Marwes Took care of some low hanging stuff and then decided to compare against https://github.com/khvzak/mlua . There may be some additional overhead compared to calling lua raw I guess, but we are still in the same ballpark so doing any improvements here seems fairly futile (general vm performance improvements like inlining still needs work though). cc #858 cc @Anvoker ``` identity time: [186.46 ns 187.29 ns 188.17 ns] change: [-3.3246% -2.5457% -1.7372%] (p = 0.00 < 0.05) Performance has improved. Found 2 outliers among 100 measurements (2.00%) 1 (1.00%) high mild 1 (1.00%) high severe identity lua time: [179.16 ns 180.01 ns 180.88 ns] Found 3 outliers among 100 measurements (3.00%) 3 (3.00%) high mild ``` Co-authored-by: Markus Westerlind <[email protected]>
2 parents 775ebaf + 28050dd commit b1b7fa3

33 files changed

+586
-579
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ branches:
1818

1919
matrix:
2020
include:
21-
- rust: nightly-2020-03-12
21+
- rust: nightly-2020-07-26
2222
# - rust: beta
2323
- rust: stable
2424
env: ARCH=i686

Cargo.lock

Lines changed: 9 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ futures = { version = "0.3.1", default-features = false }
4343
codespan = "0.9"
4444
codespan-reporting = "0.9"
4545
pin-project-lite = { version = "0.1", optional = true }
46-
salsa = { version = "0.14.2", package = "gluon-salsa" }
46+
salsa = { version = "0.15.2", package = "gluon-salsa" }
4747

4848
serde = { version = "1.0.0", optional = true }
4949
serde_state = { version = "0.4", optional = true }
@@ -145,8 +145,8 @@ path = "examples/lisp/main.rs"
145145
[package.metadata.docs.rs]
146146
features = ["docs_rs"]
147147

148-
# [profile.bench]
149-
# debug = 2
150-
#
151-
# [profile.release]
152-
# debug = 2
148+
[profile.bench]
149+
debug = 2
150+
151+
[profile.release]
152+
debug = 2

base/src/resolve.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ where
6767
{
6868
pub fn canonical_alias<'t, F>(
6969
&mut self,
70-
env: &dyn TypeEnv<Type = T>,
70+
env: &(dyn TypeEnv<Type = T> + '_),
7171
interner: &mut impl TypeContext<Symbol, T>,
7272
typ: &'t T,
7373
mut canonical: F,
@@ -105,7 +105,7 @@ where
105105

106106
pub fn remove_aliases_to_concrete<'a>(
107107
&mut self,
108-
env: &dyn TypeEnv<Type = T>,
108+
env: &(dyn TypeEnv<Type = T> + '_),
109109
interner: &mut impl TypeContext<Symbol, T>,
110110
mut typ: T,
111111
) -> Result<T, Error> {
@@ -139,7 +139,7 @@ where
139139

140140
pub fn remove_aliases(
141141
&mut self,
142-
env: &dyn TypeEnv<Type = T>,
142+
env: &(dyn TypeEnv<Type = T> + '_),
143143
interner: &mut impl TypeContext<Symbol, T>,
144144
typ: T,
145145
) -> Result<T, Error> {
@@ -148,7 +148,7 @@ where
148148

149149
pub fn remove_aliases_predicate(
150150
&mut self,
151-
env: &dyn TypeEnv<Type = T>,
151+
env: &(dyn TypeEnv<Type = T> + '_),
152152
interner: &mut impl TypeContext<Symbol, T>,
153153
mut typ: T,
154154
mut predicate: impl FnMut(&AliasData<Symbol, T>) -> bool,
@@ -163,7 +163,7 @@ where
163163

164164
pub fn remove_alias(
165165
&mut self,
166-
env: &dyn TypeEnv<Type = T>,
166+
env: &(dyn TypeEnv<Type = T> + '_),
167167
interner: &mut impl TypeContext<Symbol, T>,
168168
typ: &T,
169169
predicate: impl FnOnce(&AliasData<Symbol, T>) -> bool,
@@ -181,7 +181,7 @@ where
181181

182182
pub fn remove_alias_to_concrete<'a>(
183183
&mut self,
184-
env: &'a dyn TypeEnv<Type = T>,
184+
env: &'a (dyn TypeEnv<Type = T> + '_),
185185
interner: &mut impl TypeContext<Symbol, T>,
186186
typ: &'a T,
187187
predicate: impl FnOnce(&AliasData<Symbol, T>) -> bool,
@@ -238,7 +238,7 @@ where
238238

239239
/// Removes type aliases from `typ` until it is an actual type
240240
pub fn remove_aliases<T>(
241-
env: &dyn TypeEnv<Type = T>,
241+
env: &(dyn TypeEnv<Type = T> + '_),
242242
interner: &mut impl TypeContext<Symbol, T>,
243243
mut typ: T,
244244
) -> T
@@ -255,8 +255,7 @@ where
255255
}
256256

257257
pub fn remove_aliases_cow<'t, T>(
258-
env: &dyn TypeEnv<Type = T>,
259-
258+
env: &(dyn TypeEnv<Type = T> + '_),
260259
interner: &mut impl TypeContext<Symbol, T>,
261260
typ: &'t T,
262261
) -> Cow<'t, T>
@@ -275,7 +274,7 @@ where
275274
/// Resolves aliases until `canonical` returns `true` for an alias in which case it returns the
276275
/// type that directly contains that alias
277276
pub fn canonical_alias<'t, F, T>(
278-
env: &dyn TypeEnv<Type = T>,
277+
env: &(dyn TypeEnv<Type = T> + '_),
279278
interner: &mut impl TypeContext<Symbol, T>,
280279
typ: &'t T,
281280
mut canonical: F,
@@ -313,7 +312,7 @@ where
313312
/// Expand `typ` if it is an alias that can be expanded and return the expanded type.
314313
/// Returns `None` if the type is not an alias or the alias could not be expanded.
315314
pub fn remove_alias<T>(
316-
env: &dyn TypeEnv<Type = T>,
315+
env: &(dyn TypeEnv<Type = T> + '_),
317316
interner: &mut impl TypeContext<Symbol, T>,
318317
typ: &T,
319318
) -> Result<Option<T>, Error>
@@ -338,7 +337,7 @@ where
338337
}
339338

340339
pub fn peek_alias<'t, T>(
341-
env: &'t dyn TypeEnv<Type = T>,
340+
env: &(dyn TypeEnv<Type = T> + '_),
342341
typ: &'t T,
343342
) -> Result<Option<AliasRef<Symbol, T>>, Error>
344343
where

benches/function_call.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ use gluon::{
1010
};
1111

1212
// Benchmarks function calls
13+
14+
fn identity(b: &mut Bencher) {
15+
let vm = new_vm();
16+
let text = r#"
17+
let id x = x
18+
id
19+
"#;
20+
vm.load_script("id", text).unwrap();
21+
let mut id: FunctionRef<fn(i32) -> i32> = vm.get_global("id").unwrap();
22+
b.iter(|| {
23+
let result = id.call(20).unwrap();
24+
black_box(result)
25+
})
26+
}
27+
1328
fn factorial(b: &mut Bencher) {
1429
let vm = new_vm();
1530
let text = r#"
@@ -81,6 +96,7 @@ fn gluon_rust_boundary_overhead(b: &mut Bencher) {
8196
}
8297

8398
fn function_call_benchmark(c: &mut Criterion) {
99+
c.bench_function("identity", identity);
84100
c.bench_function("factorial", factorial);
85101
c.bench_function("factorial tail call", factorial_tail_call);
86102
c.bench_function("gluon rust boundary overhead", gluon_rust_boundary_overhead);

c-api/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ pub unsafe extern "C" fn glu_push_string(vm: &Thread, s: &u8, len: usize) -> Err
155155
Ok(s) => s,
156156
Err(_) => return Error::Unknown,
157157
};
158-
match s.push(&mut vm.current_context()) {
158+
match s.vm_push(&mut vm.current_context()) {
159159
Ok(()) => Error::Ok,
160160
Err(_) => Error::Unknown,
161161
}
@@ -166,7 +166,7 @@ pub unsafe extern "C" fn glu_push_string(vm: &Thread, s: &u8, len: usize) -> Err
166166
#[no_mangle]
167167
pub unsafe extern "C" fn glu_push_string_unchecked(vm: &Thread, s: &u8, len: usize) -> Error {
168168
let s = str::from_utf8_unchecked(slice::from_raw_parts(s, len));
169-
match s.push(&mut vm.current_context()) {
169+
match s.vm_push(&mut vm.current_context()) {
170170
Ok(()) => Error::Ok,
171171
Err(_) => Error::Unknown,
172172
}

codegen/src/pushable.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn derive_struct(
4747
Fields::Unnamed(_) if field_idents.len() == 1 => {
4848
let ty = &field_types[0];
4949
let push_impl = quote! {
50-
<#ty as _gluon_api::Pushable<'__vm>>::push(self.0, ctx)?;
50+
<#ty as _gluon_api::Pushable<'__vm>>::vm_push(self.0, ctx)?;
5151
};
5252
return gen_impl(&container, &ident, generics, push_impl);
5353
}
@@ -163,7 +163,7 @@ fn gen_impl(
163163
impl #impl_generics _gluon_api::Pushable<'__vm> for #ident #ty_generics
164164
#where_clause #(#pushable_bounds),*
165165
{
166-
fn push(self, ctx: &mut _gluon_thread::ActiveThread<'__vm>) -> _GluonResult<()> {
166+
fn vm_push(self, ctx: &mut _gluon_thread::ActiveThread<'__vm>) -> _GluonResult<()> {
167167
#push_impl
168168
Ok(())
169169
}
@@ -182,7 +182,7 @@ fn gen_push_impl(
182182
// push each field onto the stack
183183
let stack_pushes = field_idents.iter().zip(field_types).map(|(ident, ty)| {
184184
quote! {
185-
<#ty as _gluon_api::Pushable<'__vm>>::push(#ident, ctx)?;
185+
<#ty as _gluon_api::Pushable<'__vm>>::vm_push(#ident, ctx)?;
186186
}
187187
});
188188

doc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ serde = "1.0.0"
3232
serde_derive = "1.0.0"
3333
serde_json = "1.0.0"
3434

35-
gluon = { version = "0.16.1", path = ".." } # GLUON
35+
gluon = { version = "0.16.1", default-features = false, path = ".." } # GLUON
3636
completion = { package = "gluon_completion", version = "0.16.1", path = "../completion" } # GLUON
3737

3838

examples/marshalling.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ impl api::VmType for Enum {
4343
}
4444

4545
impl<'vm, 'value> api::Pushable<'vm> for Enum {
46-
fn push(self, context: &mut ActiveThread<'vm>) -> vm::Result<()> {
47-
api::ser::Ser(self).push(context)
46+
fn vm_push(self, context: &mut ActiveThread<'vm>) -> vm::Result<()> {
47+
api::ser::Ser(self).vm_push(context)
4848
}
4949
}
5050

@@ -240,7 +240,7 @@ where
240240

241241
// apply all generic parameters to the type
242242
let mut vec = AppVec::new();
243-
AppVec::push(&mut vec, T::make_type(thread));
243+
vec.push(T::make_type(thread));
244244
Type::app(ty, vec)
245245
}
246246
}
@@ -249,13 +249,13 @@ impl<'vm, T> Pushable<'vm> for GluonUser<T>
249249
where
250250
T: Pushable<'vm>,
251251
{
252-
fn push(self, ctx: &mut ActiveThread<'vm>) -> vm::Result<()> {
252+
fn vm_push(self, ctx: &mut ActiveThread<'vm>) -> vm::Result<()> {
253253
(record! {
254254
name => self.inner.name,
255255
age => self.inner.age,
256256
data => self.inner.data,
257257
})
258-
.push(ctx)
258+
.vm_push(ctx)
259259
}
260260
}
261261

0 commit comments

Comments
 (0)