Skip to content

Conversation

HerrCai0907
Copy link
Member

@HerrCai0907 HerrCai0907 commented Sep 5, 2025

String literals are widely used static data, and our GC algorithm does not recycle them (see

if (changetype<usize>(obj) < __heap_base) {
). Recording the addresses of all string literals and preventing the emission of tostack code can effectively improve performance

String literals are widely used static data, and our GC algorithm does not recycle them (see https://github.com/AssemblyScript/assemblyscript/blob/7e2a62d9615d2ae02b593af87ee4920a3c57b0bd/std/assembly/rt/itcms.ts#L244). Recording the addresses of all string literals and preventing the emission of tostack code can effectively improve performance
@HerrCai0907
Copy link
Member Author

In bootstrap parse, the execution time reduced from 210ms to 200ms.

Copy link
Member

@CountBleck CountBleck left a comment

Choose a reason for hiding this comment

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

This optimization looks very promising (so cool that you found this!). I can definitely see the difficulty of making something equivalent to Set<i64> that works in the JS build.

My understanding of this PR is that you're trying to add the if (address < __heap_base) shortcut to the shadow stack generation code, and staticGcObjectOffsets is how you're trying to determine (for needToStack) whether some data is static data. Do you think it's possible to instead directly compare the address/offset to the value of __heap_base?

If this isn't easy or possible within the compiler, what would generating something like this (well, the isize equivalent) look like (assuming this idea is easy to do):

(if (i32.lt_u (local.get $address) (global.get $~lib/memory/__heap_base))
    (; insert tostack code here ;)
)

Would Binaryen optimize that out, even in debug builds? Just throwing some backup ideas out, because I'm not a huge fan of staticGcObjectOffsets (but if we can't do it any other way, I guess we have no choice).

@HerrCai0907
Copy link
Member Author

HerrCai0907 commented Sep 5, 2025

My understanding of this PR is that you're trying to add the if (address < __heap_base) shortcut to the shadow stack generation code, and staticGcObjectOffsets is how you're trying to determine (for needToStack) whether some data is static data. Do you think it's possible to instead directly compare the address/offset to the value of __heap_base?

That is wrong. e.g. StaticArray also in data segments but we should add it to stack otherwise all of the A in this array will be free.

The precise description should be the so-called pointee free type located in the static data area. But string literal is the most common used type. So I also add string to the map. But we can extend it later.

@CountBleck
Copy link
Member

Ah, understood.

@HerrCai0907 HerrCai0907 merged commit 9664c5b into main Sep 6, 2025
14 checks passed
@HerrCai0907 HerrCai0907 deleted the opt-static-string branch September 6, 2025 00:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants