Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module 0x99::FiledAccess {
struct Inner has copy, drop {
value: u64,
}

struct Outer has copy, drop {
inner: Inner,
}

fun get_value(outer: &Outer): u64 {
outer.inner.value
}

fun set_value(outer: &mut Outer, new_value: u64) {
outer.inner.value = new_value;
}

fun get_inner_value1(inner: &Inner): u64 {
inner.value
}

fun get_inner_value2(inner: &Inner): u64 {
inner.value
}

// `&arg1.inner` cannot be reused
// perf_gain: 1 borrow_loc + 1 borrow_field eliminated
// new_cost:
// - `u64` flushed and copied twice
// perf_gain < new_cost, so this optimization is not applied
fun test_field_access_with_function(arg1: Outer): u64 {
get_inner_value1(&arg1.inner) + get_inner_value2(&arg1.inner)
}

// `arg1.inner.value` cannot be reused due to the mutation between the two accesses
fun test_field_access(arg1: Outer, arg2: u64): u64 {
let x = arg1.inner.value;
arg1.inner.value += 1;
x + arg2 + arg1.inner.value
}

// `arg1.inner.value` cannot be reused due to the mutation between the two accesses
fun test_field_access_ref(arg1: Outer, arg2: u64): u64 {
let x = arg1.inner.value;
let ref = &mut arg1.inner.value;
*ref += 1;
x + arg2 + arg1.inner.value
}

// `set_value(&mut arg1, arg2)` may modify `arg1.inner.value`, so the two calls cannot be reused
fun test_field_access_mut_ref(arg1: Outer, arg2: u64): u64 {
set_value(&mut arg1, arg2);
set_value(&mut arg1, arg2);
arg1.inner.value
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@

============ disassembled file-format ==================
// Bytecode version v9
module 0x99::FiledAccess
struct Inner has copy + drop
value: u64

struct Outer has copy + drop
inner: Inner

// Function definition at index 0
fun get_inner_value1(l0: &Inner): u64
move_loc l0
borrow_field Inner, value
read_ref
ret

// Function definition at index 1
fun get_inner_value2(l0: &Inner): u64
move_loc l0
borrow_field Inner, value
read_ref
ret

// Function definition at index 2
fun get_value(l0: &Outer): u64
move_loc l0
borrow_field Outer, inner
borrow_field Inner, value
read_ref
ret

// Function definition at index 3
fun set_value(l0: &mut Outer, l1: u64)
local l2: &mut u64
move_loc l0
mut_borrow_field Outer, inner
mut_borrow_field Inner, value
st_loc l2
move_loc l1
// @5
move_loc l2
write_ref
ret

// Function definition at index 4
fun test_field_access(l0: Outer, l1: u64): u64
local l2: &mut u64
borrow_loc l0
borrow_field Outer, inner
borrow_field Inner, value
read_ref
mut_borrow_loc l0
// @5
mut_borrow_field Outer, inner
mut_borrow_field Inner, value
st_loc l2
copy_loc l2
read_ref
// @10
ld_u64 1
add
move_loc l2
write_ref
move_loc l1
// @15
add
borrow_loc l0
borrow_field Outer, inner
borrow_field Inner, value
read_ref
// @20
add
ret

// Function definition at index 5
fun test_field_access_mut_ref(l0: Outer, l1: u64): u64
mut_borrow_loc l0
copy_loc l1
call set_value
mut_borrow_loc l0
move_loc l1
// @5
call set_value
borrow_loc l0
borrow_field Outer, inner
borrow_field Inner, value
read_ref
// @10
ret

// Function definition at index 6
fun test_field_access_ref(l0: Outer, l1: u64): u64
local l2: &mut u64
borrow_loc l0
borrow_field Outer, inner
borrow_field Inner, value
read_ref
mut_borrow_loc l0
// @5
mut_borrow_field Outer, inner
mut_borrow_field Inner, value
st_loc l2
copy_loc l2
read_ref
// @10
ld_u64 1
add
move_loc l2
write_ref
move_loc l1
// @15
add
borrow_loc l0
borrow_field Outer, inner
borrow_field Inner, value
read_ref
// @20
add
ret

// Function definition at index 7
fun test_field_access_with_function(l0: Outer): u64
borrow_loc l0
borrow_field Outer, inner
call get_inner_value1
borrow_loc l0
borrow_field Outer, inner
// @5
call get_inner_value2
add
ret


============ bytecode verification succeeded ========
Loading
Loading