Skip to content

Commit

Permalink
Merge branch 'main' into ayaz/optimize-list-literal-alloc
Browse files Browse the repository at this point in the history
  • Loading branch information
ayazhafiz authored Sep 21, 2024
2 parents 49dedf0 + 6c846a5 commit 9c656dd
Show file tree
Hide file tree
Showing 15 changed files with 571 additions and 156 deletions.
23 changes: 20 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ members = [
"crates/wasm_module",
"crates/wasm_interp",
"crates/language_server",
"crates/roc_std_heap",
]

exclude = [
Expand Down
19 changes: 19 additions & 0 deletions crates/cli/tests/cli_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,25 @@ mod cli_run {
);
}

#[test]
#[cfg_attr(windows, ignore)]
fn module_params_multiline_pattern() {
test_roc_app(
"crates/cli/tests/module_params",
"multiline_params.roc",
&[],
&[],
&[],
indoc!(
r#"
hi
"#
),
UseValgrind::No,
TestCliCommands::Dev,
);
}

#[test]
#[cfg_attr(windows, ignore)]
fn transitive_expects() {
Expand Down
8 changes: 8 additions & 0 deletions crates/cli/tests/module_params/MultilineParams.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module {
sendHttpReq,
getEnvVar
} -> [hi]

hi : Str
hi =
"hi"
11 changes: 11 additions & 0 deletions crates/cli/tests/module_params/multiline_params.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
app [main] {
pf: platform "../fixtures/multi-dep-str/platform/main.roc",
}

import MultilineParams {
sendHttpReq: \_ -> crash "todo",
getEnvVar: \_ -> crash "todo",
}

main =
MultilineParams.hi
35 changes: 18 additions & 17 deletions crates/compiler/builtins/bitcode/src/list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,16 @@ pub fn listDropAt(
) callconv(.C) RocList {
const size = list.len();
const size_u64 = @as(u64, @intCast(size));

// NOTE
// we need to return an empty list explicitly,
// because we rely on the pointer field being null if the list is empty
// which also requires duplicating the utils.decref call to spend the RC token
if (size <= 1) {
list.decref(alignment, element_width, elements_refcounted, dec);
return RocList.empty();
}

// If droping the first or last element, return a seamless slice.
// For simplicity, do this by calling listSublist.
// In the future, we can test if it is faster to manually inline the important parts here.
Expand All @@ -638,25 +648,16 @@ pub fn listDropAt(
// were >= than `size`, and we know `size` fits in usize.
const drop_index: usize = @intCast(drop_index_u64);

if (elements_refcounted) {
const element = source_ptr + drop_index * element_width;
dec(element);
}

// NOTE
// we need to return an empty list explicitly,
// because we rely on the pointer field being null if the list is empty
// which also requires duplicating the utils.decref call to spend the RC token
if (size < 2) {
list.decref(alignment, element_width, elements_refcounted, dec);
return RocList.empty();
}

if (list.isUnique()) {
var i = drop_index;
const copy_target = source_ptr;
if (elements_refcounted) {
const element = source_ptr + drop_index * element_width;
dec(element);
}

const copy_target = source_ptr + (drop_index * element_width);
const copy_source = copy_target + element_width;
std.mem.copyForwards(u8, copy_target[i..size], copy_source[i..size]);
const copy_size = (size - drop_index - 1) * element_width;
std.mem.copyForwards(u8, copy_target[0..copy_size], copy_source[0..copy_size]);

var new_list = list;

Expand Down
29 changes: 18 additions & 11 deletions crates/compiler/can/src/desugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,17 +1226,7 @@ fn desugar_pattern<'a>(env: &mut Env<'a>, scope: &mut Scope, pattern: Pattern<'a
Apply(tag, desugared_arg_patterns.into_bump_slice())
}
RecordDestructure(field_patterns) => {
let mut allocated = Vec::with_capacity_in(field_patterns.len(), env.arena);
for field_pattern in field_patterns.iter() {
let value = desugar_pattern(env, scope, field_pattern.value);
allocated.push(Loc {
value,
region: field_pattern.region,
});
}
let field_patterns = field_patterns.replace_items(allocated.into_bump_slice());

RecordDestructure(field_patterns)
RecordDestructure(desugar_record_destructures(env, scope, field_patterns))
}
RequiredField(name, field_pattern) => {
RequiredField(name, desugar_loc_pattern(env, scope, field_pattern))
Expand Down Expand Up @@ -1274,6 +1264,23 @@ fn desugar_pattern<'a>(env: &mut Env<'a>, scope: &mut Scope, pattern: Pattern<'a
}
}

pub fn desugar_record_destructures<'a>(
env: &mut Env<'a>,
scope: &mut Scope,
field_patterns: Collection<'a, Loc<Pattern<'a>>>,
) -> Collection<'a, Loc<Pattern<'a>>> {
let mut allocated = Vec::with_capacity_in(field_patterns.len(), env.arena);
for field_pattern in field_patterns.iter() {
let value = desugar_pattern(env, scope, field_pattern.value);
allocated.push(Loc {
value,
region: field_pattern.region,
});
}

field_patterns.replace_items(allocated.into_bump_slice())
}

/// Desugars a `dbg expr` expression into a statement block that prints and returns the
/// value produced by `expr`. Essentially:
/// (
Expand Down
6 changes: 5 additions & 1 deletion crates/compiler/can/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::Path;
use crate::abilities::{AbilitiesStore, ImplKey, PendingAbilitiesStore, ResolvedImpl};
use crate::annotation::{canonicalize_annotation, AnnotationFor};
use crate::def::{canonicalize_defs, report_unused_imports, Def};
use crate::desugar::desugar_record_destructures;
use crate::env::Env;
use crate::expr::{
ClosureData, DbgLookup, Declarations, ExpectLookup, Expr, Output, PendingDerives,
Expand Down Expand Up @@ -326,13 +327,16 @@ pub fn canonicalize_module_defs<'a>(
before_arrow: _,
after_arrow: _,
}| {
let desugared_patterns =
desugar_record_destructures(&mut env, &mut scope, pattern.value);

let (destructs, _) = canonicalize_record_destructs(
&mut env,
var_store,
&mut scope,
&mut output,
PatternType::ModuleParams,
&pattern.value,
&desugared_patterns,
pattern.region,
PermitShadows(false),
);
Expand Down
5 changes: 4 additions & 1 deletion crates/compiler/can/src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,10 @@ pub fn canonicalize_record_destructs<'a>(
}
};
}
_ => unreachable!("Any other pattern should have given a parse error"),
_ => unreachable!(
"Any other pattern should have given a parse error: {:?}",
loc_pattern.value
),
}
}

Expand Down
Loading

0 comments on commit 9c656dd

Please sign in to comment.