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

Merged
merged 8 commits into from
Jun 5, 2025

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 Lord-McSweeney force-pushed the avm2-faster-natives branch 2 times, most recently from b742a7b to 5289ba4 Compare May 23, 2025 03:54
@Lord-McSweeney Lord-McSweeney marked this pull request as ready for review June 2, 2025 01:33
@Lord-McSweeney Lord-McSweeney force-pushed the avm2-faster-natives branch 2 times, most recently from 3dc35f1 to 02d7105 Compare June 2, 2025 04:12
@torokati44 torokati44 force-pushed the avm2-faster-natives branch 2 times, most recently from 9a8ef04 to 06bb4ca Compare June 4, 2025 21:25
Lord-McSweeney added 8 commits June 5, 2025 12:38
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
@torokati44 torokati44 force-pushed the avm2-faster-natives branch from 06bb4ca to b0b7fc3 Compare June 5, 2025 10:38
@Lord-McSweeney Lord-McSweeney merged commit 7920c83 into ruffle-rs:master Jun 5, 2025
23 checks passed
Hancock33 added a commit to Hancock33/batocera.piboy that referenced this pull request Jun 8, 2025
--------------------------------------------------------------------------------------------------------
batocera-emulationstation.mk ed45ae08169b953c178f592b6cd34b4934777fc0 # Version: Commits on Jun 06, 2025
--------------------------------------------------------------------------------------------------------
update po

Signed-off-by: Nicolas Adenis-Lamarre <[email protected]>,

------------------------------------------------------------------------------------------------
batocera-es-piboy.mk ed45ae08169b953c178f592b6cd34b4934777fc0 # Version: Commits on Jun 06, 2025
------------------------------------------------------------------------------------------------
update po

Signed-off-by: Nicolas Adenis-Lamarre <[email protected]>,

------------------------------------------------------------------------------------------
dolphin-emu.mk a0611b512bdf3fe01432c5a136e02b64e82b35db # Version: Commits on Jun 05, 2025
------------------------------------------------------------------------------------------
Merge pull request #13713 from Dentomologist/filesystemwidget_center_header_text

FilesystemWidget: Center header text,

--------------------------------------------------------------------------------------
flycast.mk b097aa3331274b10e97719901d8cf88b2abe6cc2 # Version: Commits on Jun 06, 2025
--------------------------------------------------------------------------------------
dropping iOS support

well done Gabe!,

-------------------------------------------------------------------------------------
hatari.mk fe2f3ffeb66326ed230ceff88d524b2c3193af77 # Version: Commits on Jun 06, 2025
-------------------------------------------------------------------------------------
Minor updates to compatiblity docs,

--------------------------------------------------------------------------------------
openmsx.mk e6a903b70c09a8af562882fbb71eb31071c90517 # Version: Commits on Jun 06, 2025
--------------------------------------------------------------------------------------
EmuDuration: Use C++20 abbreviated function template syntax.,

-----------------------------------------------------------------------------------
play.mk 5917411fbf30c56c4d0e9ade635c4acf8102dd8c # Version: Commits on Jun 06, 2025
-----------------------------------------------------------------------------------
Android: Opt-out of edge to edge enforcement.,

-------------------------------------------------------------------------------------
ppsspp.mk e35de5479b93f71ca617010ce9aea4c2d80ebcd5 # Version: Commits on Jun 06, 2025
-------------------------------------------------------------------------------------
Merge pull request #20457 from hrydgard/homebrew-memsize-fix

Fix homebrew apps that request large memory,

------------------------------------------------------------------------------------
rpcs3.mk 46cce45b1382ee6ae7bae4179236b7d1e0b47a93 # Version: Commits on Jun 01, 2025
------------------------------------------------------------------------------------
Fix missing inserted = true in check_for_duplicates_fast,

---------------------------------------------------------------
ruffle.mk nightly-2025-06-06 # Version: Commits on Jun 06, 2025
---------------------------------------------------------------
## What's Changed

* chore: Update translations by @RuffleBuild in ruffle-rs/ruffle#20556

* avm2: Speed up some native calls by @Lord-McSweeney in ruffle-rs/ruffle#20328

* core: Refresh frame script on registration by @nivkner in ruffle-rs/ruffle#20293

**Full Changelog**: ruffle-rs/ruffle@nightly-2025-06-04...nightly-2025-06-06,

----------------------------------------------------
ryujinx.mk 1.3.75 # Version: Commits on Jun 06, 2025
----------------------------------------------------
Canary-1.3.75

--------------------------------------------------------------------------------------
shadps4.mk 91d29459fb55cb0d28006639e7a38134c5a368ec # Version: Commits on Jun 06, 2025
--------------------------------------------------------------------------------------
Implement PM4CondExec (#3046),

--------------------------------------------------------------------------------------
tsugaru.mk b965c1978aba407144772d50b9229df46a530591 # Version: Commits on Jun 06, 2025
--------------------------------------------------------------------------------------
Optimizing PageLevelException,

-------------------------------------------------
vice.mk r45694 # Version: Commits on Jun 05, 2025
-------------------------------------------------
PET 8296 $Axxxx video ram treatment.

On the PET 8296, when writing to $Axxxx which writes through to RAM, treat this

as video ram, so we can do beam racing with it like the $8xxx memory.

git-svn-id: https://svn.code.sf.net/p/vice-emu/code/trunk@45694 379a1393-f5fb-40a0-bcee-ef074d9b53f7,

-------------------------------------------------------------------------------------------
xenia-native.mk 422517c673bba086c2b857946ae5a37ee35b8e50 # Version: Commits on Jun 02, 2025
-------------------------------------------------------------------------------------------
[XAM] Only show license_mask instructions for XBLA game unlock offer ids,

----------------------------------------------------------------------------------------
pironman5.mk fdec70cd3e51ad3173bc5faf3029898597b3133e # Version: Commits on Jun 06, 2025
----------------------------------------------------------------------------------------
chore: update version to 1.2.12 and pm_dashboard dependency

Update package version and pm_dashboard dependency to their latest versions to ensure compatibility and include recent fixes.,

-----------------------------------------------------
ecm.mk v6.15.0-rc1 # Version: Commits on Jun 02, 2025
-----------------------------------------------------
Fix LOCALSTATEDIR when installing to /usr

In that case we want /var, not /usr/var.

Apply the same logic as for /etc

BUG: 505090,

--------------------------------------------------------------------------------------
pm_auto.mk dee42b6e7133491f00d72d1b6b418ac87ad87bb7 # Version: Commits on Jun 06, 2025
--------------------------------------------------------------------------------------
Remove unwanted print,

------------------------------------------------------------------------------------
box64.mk 03d220b1d297a9e5be81760833b014edf9dfe7ab # Version: Commits on Jun 06, 2025
------------------------------------------------------------------------------------
Bumped version to v0.3.6,

------------------------------------------------------------------------------------------
devilutionx.mk c3d8595b722afcf6c4a134c26d7da70652fd4baf # Version: Commits on Jun 06, 2025
------------------------------------------------------------------------------------------
palette_update_quest_palette: tone map 1 color (#8027)

Co-authored-by: Stephen C. Wills <[email protected]>,

----------------------------------------------------------------------------------------
openmohaa.mk 3d1741ec2090ee5af3c89d9045c0207195f88e6e # Version: Commits on Jun 05, 2025
----------------------------------------------------------------------------------------
Fix bulge vertexes causing some surfaces to disconnect,

----------------------------------------------------------------------------------------
supertux2.mk 0a84620bd9b1485c2b0115ccafa89370b4ccd4a9 # Version: Commits on Jun 05, 2025
----------------------------------------------------------------------------------------
Rework shard behavior (#3170)

* Rework shard behavior

* Decrease time the shards are active

* Fadeout shards when becoming inactive

Fixes #3134,

------------------------------------------------------------
syncthing.mk v2.0.0-rc.20 # Version: Commits on Jun 06, 2025
------------------------------------------------------------
## Major changes in 2.0

- Database backend switched from LevelDB to SQLite. There is a migration on

  first launch which can be lengthy for larger setups. The new database is

  easier to understand and maintain and, hopefully, less buggy.

- Deleted items are no longer kept forever in the database, instead they are

  forgotten after six months. If your use case require deletes to take

  effect after more than a six month delay, set the

  `--db-delete-retention-interval` command line option or corresponding

  environment variable to zero, or a longer time interval of your choosing.

- Modernised command line options parsing. Old single-dash long options are

  no longer supported, e.g. `-home` must be given as `--home`. Some options

  have been renamed, others have become subcommands. All serve options are

  now also accepted as environment variables. See  `syncthing --help` and

  `syncthing serve --help` for details.

- Rolling hash detection of shifted data is no longer supported as this

  effectively never helped. Instead, scanning and syncing is faster and more

  efficient without it.

- A \default folder\ is no longer created on first startup.

- Multiple connections are now used by default between v2 devices. The new

  default value is to use three connections: one for index metadata and two

  for data exchange.

This release is also available as:

* APT repository: https://apt.syncthing.net/

* Docker image: `docker.io/syncthing/syncthing:2.0.0-rc.20` or `ghcr.io/syncthing/syncthing:2.0.0-rc.20`

  (`{docker,ghcr}.io/syncthing/syncthing:2` to follow just the major version)

## What's Changed

### Fixes

* fix(db): handle large numbers of blocks in update by @calmh in syncthing/syncthing#10025

* fix(syncthing): make directory flags global for all commands by @calmh in syncthing/syncthing#10028

* fix(sqlite): apply options by @pixelspark in syncthing/syncthing#10049

* fix(db): version vector serialisation :( by @calmh in syncthing/syncthing#10050

* fix(model): loop-break regression while block copying in puller by @imsodin in syncthing/syncthing#10069

* fix(model): close fd immediately in copier by @imsodin in syncthing/syncthing#10079

* fix(model): use same folder first in copier by @imsodin in syncthing/syncthing#10093

* fix(model): correct bufferpool handling; simplify by @calmh in syncthing/syncthing#10113

* fix(protocol): avoid deadlock with concurrent connection start and close by @calmh in syncthing/syncthing#10140

* fix(syncthing): avoid writing panic log to nil fd by @ardevd in syncthing/syncthing#10154

### Features

* feat: add `syncthing debug database-statistics` command by @calmh in syncthing/syncthing#10117

* feat(config): enable multiple connections by default by @calmh in syncthing/syncthing#10151

* feat(config): expose folder and device info as metrics (fixes #9519) by @calmh in syncthing/syncthing#10148

### Other

* chore: remove abandoned next-gen-gui experiment by @calmh in syncthing/syncthing#10004

* chore: remove weak hashing which does not pull its weight by @calmh in syncthing/syncthing#10005

* chore: switch database engine to sqlite (fixes #9954) by @calmh in syncthing/syncthing#9965

* chore: harmonise command line flags by @calmh in syncthing/syncthing#10007

* chore(db): increase journal limit to 64MiB by @bt90 in syncthing/syncthing#10022

* chore: forget deleted files older than six months (fixes #6284) by @calmh in syncthing/syncthing#10023

* chore(db): use shorter read transactions and periodic checkpoint for smaller WAL by @calmh in syncthing/syncthing#10027

* chore: configurable delete retention interval by @calmh in syncthing/syncthing#10030

* chore(db): fix debug logging by @bt90 in syncthing/syncthing#10033

* chore(db): buffer pulled files for smaller WAL by @calmh in syncthing/syncthing#10036

* chore(db): use one SQLite database per folder by @calmh in syncthing/syncthing#10042

* chore(model): delay starting a pull while there are incoming index updates by @calmh in syncthing/syncthing#10041

* chore(syncthing): remove \default\ folder concept by @calmh in syncthing/syncthing#10068

* chore(syncthing): ensure migrated database is closed before exiting by @xjtdy888 in syncthing/syncthing#10076

* chore(db, model): simplify per hash DB lookup in copier by @imsodin in syncthing/syncthing#10080

* chore(model): refactor copier for more flatness by @imsodin in syncthing/syncthing#10094

* build: upgrade setup-zig action by @calmh in syncthing/syncthing#10134

* build: properly propagate build tags to Debian build by @calmh in syncthing/syncthing#10144

* chore(protocol): don't start connection routines a second time by @imsodin in syncthing/syncthing#10146

* chore(protocol): only allow enc. password changes on cluster config by @imsodin in syncthing/syncthing#10145

## New Contributors

* @ardevd made their first contribution in syncthing/syncthing#10154

**Full Changelog**: syncthing/syncthing@v1.29.7...v2.0.0-rc.20,

----------------------------------------------------------------------------------------------------
sdl2-gamecontrollerdb.mk 0ca7d0466a01230b0685be7b9ee9ae7ed0ce0775 # Version: Commits on Jun 06, 2025
----------------------------------------------------------------------------------------------------
Add upstream mappings,

----------------------------------------------------------------------------------------
retroarch.mk ca405929ad72e6f47401de7c3142436d5eb44351 # Version: Commits on Jun 06, 2025
----------------------------------------------------------------------------------------
Merge pull request #17981 from warmenhoven/warmenhoven/pr/MicCoreAudio

Add Microphone CoreAudio driver for iOS,

-----------------------------------------------------------------------------------
tr1x.mk 4492c9b337a27a30e891d53bcd5dd6b5c4b80752 # Version: Commits on Jun 06, 2025
-----------------------------------------------------------------------------------
ui/settings: fix overflows in handling float values,

-----------------------------------------------------------------------------------
tr2x.mk 4492c9b337a27a30e891d53bcd5dd6b5c4b80752 # Version: Commits on Jun 06, 2025
-----------------------------------------------------------------------------------
ui/settings: fix overflows in handling float values,

--------------------------------------------------------------------------------------------------
libretro-beetle-psx.mk 271a967e78dfe2e5936523e3ba52ce6f3c2fa870 # Version: Commits on Jun 06, 2025
--------------------------------------------------------------------------------------------------
Fetch translations & Recreate libretro_core_options_intl.h,

--------------------------------------------------------------------------------------------
libretro-dice.mk 47bc3f3af2c6f0b5cd556f87d60fddf60bb9d326 # Version: Commits on Jun 06, 2025
--------------------------------------------------------------------------------------------
Merge pull request #46 from mittonk/whitespace

style: whitespace cleanup,

---------------------------------------------------------------------------------------------
libretro-fbneo.mk 7ffe043d410f6021f7cfca77589b42bf2e094c3c # Version: Commits on Jun 06, 2025
---------------------------------------------------------------------------------------------
(libretro) update files,

-----------------------------------------------------------------------------------------------
libretro-flycast.mk b097aa3331274b10e97719901d8cf88b2abe6cc2 # Version: Commits on Jun 06, 2025
-----------------------------------------------------------------------------------------------
dropping iOS support

well done Gabe!,

-------------------------------------------------------------------------------------------------
libretro-geargrafx.mk 185527973f51319a4f9a094fa9787ed077772834 # Version: Commits on Jun 06, 2025
-------------------------------------------------------------------------------------------------
Fix 'quit' with Q instead of CTRL+Q,

--------------------------------------------------------------------------------------------
libretro-play.mk 5917411fbf30c56c4d0e9ade635c4acf8102dd8c # Version: Commits on Jun 06, 2025
--------------------------------------------------------------------------------------------
Android: Opt-out of edge to edge enforcement.,

----------------------------------------------------------------------------------------------
libretro-ppsspp.mk e35de5479b93f71ca617010ce9aea4c2d80ebcd5 # Version: Commits on Jun 06, 2025
----------------------------------------------------------------------------------------------
Merge pull request #20457 from hrydgard/homebrew-memsize-fix

Fix homebrew apps that request large memory,
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