Optimize type casting by using __transmute instead of asm blocks#7675
Conversation
PR SummaryMedium Risk Overview Copy-type conversions that still need E2E snapshots reflect smaller bytecode and lower gas on affected programs (e.g. Reviewed by Cursor Bugbot for commit 73c6b17. Bugbot is set up for automated code reviews on this repo. Configure here. |
Performance improvementsE2E Gas Usage
E2E Bytecode Size
In-language Gas Usage
In-language Bytecode Size
|
|
👍 |
|
I am seeing some transmute-by-copy, like below. And some transmute of self. let input = (0u64, 0u64, 0u64, self.as_u64());
__transmute::<(u64, u64, u64, u64), u256>(input)I would expect we would be doing something like: let input = (0u64, 0u64, 0u64, self.as_u64());
*__transmute::<&(u64, u64, u64, u64), &u256>(&input) |
Currently all the transmute are "by-value" or better to say, they look like they convert values, but in the background it's just This is inconsistent similar to inconsistencies listed in #6351 and #6993. Should we specify |
Hum... I think that it makes sense that
I think we can gather the benefits for now, specially on
I think that I was thinking more for cases like |
Description
This PR improves gas costs and bytecode sizes by consistently replacing
asm-based conversions of reference types instdwith__transmute.Note that copy-types are still converted using
asmblocks, because currently we didn't extend__transmuteto handle copy-types without demoting transmuted values to stack and actually casting pointers. This gives a performance penalty when transmuting copy-types which we don't want to have.As expected, removing
asmblocks unlocked existing IR performance optimizations and resulted in performance improvements given in the comment below.Note that we might think of temporarily forbidding using
__transmutewith copy-types until it is properly supported. But we have code inops.swwhere__transmutemust be used to convert between copy-types, because the operator trait methods must be const-evaluable and if they useasmfor conversion, they cannot be. Historically, making them const-evaluable was the reason for adding__transmutein the first place.Note that in the future, we plan to improve IR optimizations by letting them be aware of "safe
asmblocks". Pure conversion blocks will be considered safe. That improvement would likely bring the same performance benefits as rolling out__transmute, but:__transmuteis the expected way to convert types and we should use it consistentlty across the codebase.EDIT: The issue of silent
sway-irsnapshots is addressed in #7676.Note that the PR changes snapshot files in thesway-irtests. These changes are not related to this PR, but likely to the snapshots not being updated because of theinstachecks being silent. For more info see #7666 (review).Checklist
Breaking*orNew Featurelabels where relevant.