-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add as_slice builtin function, add execution test (#4523)
# Description ## Problem\* Expected to resolve slowdown in #4504 ## Summary\* Adds builtin function for `as_slice`, allowing it to be much more efficient (`O(1)` instead of `O(n)`) ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[Exceptional Case]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --------- Co-authored-by: vezenovm <[email protected]>
- Loading branch information
1 parent
414194c
commit 6a9ea35
Showing
11 changed files
with
181 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "array_to_slice" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.24.0" | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
x = "0" | ||
y = "1" |
33 changes: 33 additions & 0 deletions
33
test_programs/execution_success/array_to_slice/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Converts an array into a slice. | ||
fn as_slice_push<T, N>(xs: [T; N]) -> [T] { | ||
let mut slice = []; | ||
for elem in xs { | ||
slice = slice.push_back(elem); | ||
} | ||
slice | ||
} | ||
|
||
fn main(x: Field, y: pub Field) { | ||
let xs: [Field; 0] = []; | ||
let ys: [Field; 1] = [1]; | ||
let zs: [Field; 2] = [1, 2]; | ||
let ws: [Field; 3] = [1; 3]; | ||
let qs: [Field; 4] = [3, 2, 1, 0]; | ||
|
||
let mut dynamic: [Field; 4] = [3, 2, 1, 0]; | ||
let dynamic_expected: [Field; 4] = [1000, 2, 1, 0]; | ||
dynamic[x] = 1000; | ||
|
||
assert(x != y); | ||
assert(xs.as_slice() == as_slice_push(xs)); | ||
assert(ys.as_slice() == as_slice_push(ys)); | ||
assert(zs.as_slice() == as_slice_push(zs)); | ||
assert(ws.as_slice() == as_slice_push(ws)); | ||
assert(qs.as_slice() == as_slice_push(qs)); | ||
|
||
assert(dynamic.as_slice()[0] == dynamic_expected[0]); | ||
assert(dynamic.as_slice()[1] == dynamic_expected[1]); | ||
assert(dynamic.as_slice()[2] == dynamic_expected[2]); | ||
assert(dynamic.as_slice()[3] == dynamic_expected[3]); | ||
assert(dynamic.as_slice().len() == 4); | ||
} |
7 changes: 7 additions & 0 deletions
7
test_programs/execution_success/brillig_array_to_slice/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "brillig_array_to_slice" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.25.0" | ||
|
||
[dependencies] |
1 change: 1 addition & 0 deletions
1
test_programs/execution_success/brillig_array_to_slice/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
x = "0" |
18 changes: 18 additions & 0 deletions
18
test_programs/execution_success/brillig_array_to_slice/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
unconstrained fn brillig_as_slice(x: Field) -> (u64, Field, Field) { | ||
let mut dynamic: [Field; 1] = [1]; | ||
dynamic[x] = 2; | ||
assert(dynamic[0] == 2); | ||
|
||
let brillig_slice = dynamic.as_slice(); | ||
assert(brillig_slice.len() == 1); | ||
|
||
(brillig_slice.len(), dynamic[0], brillig_slice[0]) | ||
} | ||
|
||
fn main(x: Field) { | ||
let (slice_len, dynamic_0, slice_0) = brillig_as_slice(x); | ||
assert(slice_len == 1); | ||
assert(dynamic_0 == 2); | ||
assert(slice_0 == 2); | ||
} | ||
|