Skip to content

Rust store optimisation#206

Merged
N1ark merged 5 commits intomainfrom
rust-store-optim
Dec 26, 2025
Merged

Rust store optimisation#206
N1ark merged 5 commits intomainfrom
rust-store-optim

Conversation

@N1ark
Copy link
Copy Markdown
Contributor

@N1ark N1ark commented Dec 19, 2025

Based on #205
Fixes #162

Add the store optimisation to Rust!
Running our Kani + Miri benchmark, we get an average time that goes from 0.232s to 0.211s, so a ~10% improvement!

Also added a couple reductions, and optimised bv_to_z which interprets a bitvector into a signed value; this function went from taking 320 cycles to 31 cycles/op (on one test I tried that was quite slow, where it was used ~4M times)

@N1ark N1ark requested a review from giltho as a code owner December 19, 2025 02:58
@N1ark N1ark added soteria-rust Issues related to soteria-rust performance Issues relating to performance improvements labels Dec 19, 2025
@N1ark N1ark changed the base branch from main to good-layouts December 19, 2025 10:21
Base automatically changed from good-layouts to main December 25, 2025 14:33
@giltho
Copy link
Copy Markdown
Contributor

giltho commented Dec 25, 2025

This somehow also needs promoting files

@giltho
Copy link
Copy Markdown
Contributor

giltho commented Dec 25, 2025

I need to write a GitHub action that can be triggered by a comment (e.g. !promote that builds, run dune test --auto-promote, commits and pushes

@giltho
Copy link
Copy Markdown
Contributor

giltho commented Dec 25, 2025

Heh fun idea
@copilot can you promote the files? Check the failed action

Copy link
Copy Markdown

Copilot AI commented Dec 25, 2025

@giltho I've opened a new pull request, #215, to work on those changes. Once the pull request is ready, I'll request review from you.

@giltho
Copy link
Copy Markdown
Contributor

giltho commented Dec 25, 2025

What a waste of precious earth resources that was

Copy link
Copy Markdown
Contributor

@giltho giltho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only nitpicks, feel free to ignore all of them

The one question I really have (for a future PR) is: how much work would it be do so that the following program doesn't allocate on the heap? I.e. you put the structure value on the stack and it already has all the necessary structure for reasoning here:

struct A { b : B }
struct B { x : u32 }

fn main() {
  let a = A { b : B { x : 12 } };
  let x = a.b.x;
}

Comment thread soteria-rust/lib/store.ml
- Uninit: the symbol is bound to an immediate, uninitialized value.
- Dead: the symbol is dead; it doesn't exist (e.g. after a [StorageDead]).
*)
type 'a binding_kind =
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is sufficiently identical to soteria-C that we should probably factor it out?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(In a future PR of course)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm yes we could!

Comment thread soteria-rust/lib/store.ml Outdated
Comment on lines -1797 to +1798
let p = precision_of_f v.node.ty in
match (p, v.node.kind, size) with
| F32, Float f, 32 ->
mk 32 @@ Z.of_int32 (Int32.bits_of_float (Stdlib.Float.of_string f))
| F64, Float f, 64 ->
mk 64 @@ Z.of_int64 (Int64.bits_of_float (Stdlib.Float.of_string f))
| _, _, _ -> Unop (BvOfFloat (rounding, signed, size), v) <| t_bv size
Unop (BvOfFloat (rounding, signed, size), v) <| t_bv size
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious why this is more efficient?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not more efficient it's just wrong hahahha
BV.of_float converts a float into its integer representation, e.g. 5.0 becomes 0x5, while IntN.bits_of_float is the bit content of the float (wrong)

Comment thread soteria/lib/soteria_std/list.ml Outdated
type 'err fun_exec = rust_val list -> (rust_val, unit) Rust_state_m.t

let get_variable var_id =
type lazy_ptr = Store of Expressions.local_id | Heap of full_ptr
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is lazy_ptr not just "place"?
Or possibly "resolved_place" or something

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well because you don't know if lazy_ptr is a lazy pointer; really it's an (lazy_ptr, full_ptr) Either.t where lazy_ptr = local ⊂ place

@N1ark
Copy link
Copy Markdown
Contributor Author

N1ark commented Dec 26, 2025

Only nitpicks, feel free to ignore all of them

The one question I really have (for a future PR) is: how much work would it be do so that the following program doesn't allocate on the heap? I.e. you put the structure value on the stack and it already has all the necessary structure for reasoning here:

struct A { b : B }
struct B { x : u32 }

fn main() {
  let a = A { b : B { x : 12 } };
  let x = a.b.x;
}

I'm not sure; in theory it's "easy" because to each projection you just attach an index. I'm still worried we might miss some unsoundnesses, because load-store does all of the value-validation we want, but supposedly you cant do anything unsafe without going through a pointer (which would lower to the heap), so.... maybe? worth a shot for sure, uncertain what the gains are

edit: Created #162

@N1ark N1ark added this pull request to the merge queue Dec 26, 2025
Merged via the queue into main with commit bb305bc Dec 26, 2025
4 checks passed
@N1ark N1ark deleted the rust-store-optim branch December 26, 2025 10:37
pcarrott pushed a commit that referenced this pull request Dec 30, 2025
* Fix `BV.of_float` + add 2 reductions

* Add store optimisation to Rust!

* Optim `bv_to_z`

* PR comments

* Promote tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Issues relating to performance improvements soteria-rust Issues related to soteria-rust

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Store optimisation in Rust

3 participants