Skip to content

avm2: Speed up some native calls #20328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

Lord-McSweeney
Copy link
Collaborator

@Lord-McSweeney Lord-McSweeney commented May 2, 2025

This allows us to optimize some calls to native functions when

  1. the function has a [Ruffle(FastCall)] annotation,
  2. the arguments are of the correct count and type, and
  3. the class of the value the function is being called on is final.

For example, Math.abs runs more than twice as fast with this PR as it does on current master. From my testing, in most SWFs, 5-20% of calls that would be CallMethod are now CallNative.

Box2d stats:

ConstructSuper -> Pop: 70%

FindPropStrict -> GetScriptGlobals: 82.67%
FindPropStrict -> GetOuterScope: 8.96%
FindPropStrict -> GetScopeObject: 8.36%

FindProperty -> GetOuterScope: 5.66%
FindProperty -> GetScopeObject: 94.33%

InitProperty -> SetSlot: 6.28%
InitProperty -> SetSlotNoCoerce: 90.98%
InitProperty -> CallMethod: 2.73%

SetProperty -> SetSlot: 8.07%
SetProperty -> SetSlotNoCoerce: 73.88%
SetProperty -> CallMethod: 2.4%

GetProperty -> GetSlot: 88.92%
GetProperty -> CallMethod: 0.47%

CallProperty -> CallMethod: 74.1% (-9.16%)
CallProperty -> CallNative: 9.16% (+9.16%)
CallProperty -> CoerceUSwapPop: 5.17%
CallProperty -> CoerceISwapPop: 9.56%

CallPropVoid -> CallMethod: 91.72% (-2.25%)
CallPropVoid -> CallNative: 2.25% (+2.25%)

ConstructProp -> ConstructSlot: 100%

Coerce -> Nop: 73.18%

CoerceD -> Nop: 98.75%

CoerceB -> Nop: 97.29%

CoerceU -> Nop: 64.91%

CoerceI -> Nop: 52%

ReturnValue -> ReturnValueNoCoerce: 96.39%

TODO:

  • Record box2d optimization stats
  • Add documentation on FastCall to the globals README
  • Use a HashSet for the FastCall methods...?

@Lord-McSweeney Lord-McSweeney added A-avm2 Area: AVM2 (ActionScript 3) T-perf Type: Performance Improvements labels May 2, 2025
@moulins
Copy link
Contributor

moulins commented May 2, 2025

Use a HashSet for the FastCall methods...?

An alternative (maybe simpler to implement?) could be to sort the static array, and use binary_search for O(log n) lookups.

// "[Ruffle(FastCall)]". Unlike the rest, it is not indexed by method id-
// instead, every item in the list is a method id.
//
// FIXME: should this be some sort of hashset?
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you sorted the vec before putting it in, we could use binary_search instead of iter().any() to speed up* lookups without affecting memory use at all.
Alternatively, there's always https://github.com/rust-phf/rust-phf to construct a static hashset at compile time (and thus not waste any allocations), but might be an overkill here.

(*I imagine the difference might not matter as long as we have <100 fast methods. )

Lord-McSweeney added 6 commits May 22, 2025 20:36
GetProperty and CallProperty optimization logic have been moved to their own methods
Added to methods on Class, Function, Namespace, QName, String, Vector, XML
and XMLList, Graphics, SoundTransform, as well as some toplevel functions and
`getTimer`.
… `NativeCallable` metadata to the playerglobals README
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-avm2 Area: AVM2 (ActionScript 3) newsworthy T-perf Type: Performance Improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy