About function call inlining


#1

About function call inlining


~~~~~~~~~~~~~~~~~~ Dears, method inlining anyone expects.

I’m again doing a poor, but motivation post for future method inlining support. I said bad things for the Flash platform in this community, all due to performance and old-lity. I know Flash Professional since I was 12 (2014), and I’m experienced enough with ActionScript.

For release cases, I can confirm MXMLC-generated ABCs don’t have inlineable function-calls inlined. I won’t tell how I did prove it; that’s obvious.

But what if we had a modern tool for inlining AS3 methods? Also using Flash libraries such as…:


The reality behind inlining.

  • Any plans to improve AS3 performance? (Feb 26, 2015 3:09 AM) One flashdictionary’s point: ActionScript Inline feature was never completed. As a result, the major ActionScript frameworks such as Starling, Away3D are not taking advantage of the Inline feature.

  • Miscellaneous optimizations The inline part ― Inline code, when possible, to reduce the number of function calls in your code. ― recommends you to manually inline code. More context: Calling functions can be expensive. Try to reduce the number of function calls by moving code inline. Moving code inline is a good way to optimize for pure performance. However, keep in mind that inline code can make your code harder to reuse and can increase the size of your SWF file. Some function calls, such the Math class methods, are easily to move inline. […] These tips might be old (I don’t know), but if Adobe posted it, it means AVM2 might do nothing about method inlining when executing ABC (that would make program initialiastion slower).


Apparat

I found Joa Ebbert’s Apparat tools in a famous SO question. Sadly, though, Apparat only inlines static methods and requires extending a Inline class or using a [Macro] meta-data (something like that).


IMO

The inlining method optimisation is hard for pure ECMAScript dev’s. Always possible, but yk: boring.

For AS3 dev’s it’s easier: fixed properties and mostly readonly function definitions. Namespace quals (with use namespaces) are easily detected.

I made a bit of a readme for a future solution. Share me your thoughts.


#2

Ohmm, not 2 forget: Adobe is lazy 8 optimising. They commit failures 8 this point :frowning: . Unfortunately I won’t be able to build my Bitzier drawing editor 4 creating my anime-like series, kkk.


#3

Except ASC2 support function inlining since 2012

see Adobe ActionScript Compiler 2.0 Release Notes (PDF)

and ActionScript Compiler 2.0 Backward Compatibility


use the metadata [Inline] and the compiler option -inline

for inlining to work you need to follow those rules

  • The function is final, static or the containing scope is file or package
    Compiler Error Message:

    The function myFunction could not be inlined, as only functions marked as final, static or global functions can be inlined.

  • The function does not contain any activations

  • The function does not contain any try or with statements

  • The function does not contain any function closures


package
{
    // global function
    [Inline]
    public function test1():String
    {
        return "hello world";
    }
}
package
{
    public class Test
    {
        private var _abc:uint = 0;
        private static var _def:uint = 1;

        // final method
        [Inline]
        public final function abc1():uint
        {
            return _abc;
        }

        // static method
        [Inline]
        public static function def2():uint
        {
            return _def;
        }    
    }
}

from an install of the AIR SDK
go to the bin/ folder

$ ./mxmlc -help list

Adobe ActionScript Compiler (mxmlc)
Version 2.0.0 build 354200
Copyright 2004-2012 Adobe Systems Incorporated. All rights reserved.

-advanced-telemetry
-compiler.accessible
-compiler.actionscript-file-encoding <string>
-compiler.compress
-compiler.context-root <context-path>
-compiler.debug
-compiler.enable-runtime-design-layers
-compiler.external-library-path [path-element] [...]
-compiler.fonts.advanced-anti-aliasing
-compiler.fonts.local-font-paths [path-element] [...]
-compiler.fonts.local-fonts-snapshot <string>
-compiler.fonts.max-cached-fonts <string>
-compiler.fonts.max-glyphs-per-face <string>
-compiler.include-libraries [library] [...]
-compiler.library-path [path-element] [...]
-compiler.locale [locale-element] [...]
-compiler.minimum-supported-version <string>
-compiler.mobile
-compiler.mxml.compatibility-version <version>
-compiler.mxml.minimum-supported-version <string>
-compiler.namespaces.namespace [uri] [manifest] [...]
-compiler.omit-trace-statements
-compiler.optimize
-compiler.preloader <string>
-compiler.report-invalid-styles-as-warnings
-compiler.services <filename>
-compiler.show-actionscript-warnings
-compiler.show-binding-warnings
-compiler.show-invalid-css-property-warnings
-compiler.show-multiple-definition-warnings
-compiler.show-shadowed-device-font-warnings
-compiler.show-unused-type-selector-warnings
-compiler.source-path [path-element] [...]
-compiler.strict
-compiler.theme [filename] [...]
-compiler.verbose-stacktraces
-error-problems [class] [...]
-help [keyword] [...]
-ignore-problems [class] [...]
-include-resource-bundles [bundle] [...]
-load-config <filename>
-metadata.contributor <name>
-metadata.creator <name>
-metadata.date <text>
-metadata.description <text>
-metadata.language <code>
-metadata.localized-description <text> <lang>
-metadata.localized-title <title> <lang>
-metadata.publisher <name>
-metadata.title <text>
-output <filename>
-runtime-shared-libraries [url] [...]
-runtime-shared-library-path [path-element] [rsl-url] [policy-file-url] [rsl-url] [policy-file-url]
-static-link-runtime-shared-libraries
-swf-version <int>
-target-player <version>
-tools-locale <string>
-use-direct-blit
-use-gpu
-use-network
-version
-warning-problems [class] [...]
-warnings

$ ./mxmlc -help advanced optimize

Adobe ActionScript Compiler (mxmlc)
Version 2.0.0 build 354200
Copyright 2004-2012 Adobe Systems Incorporated. All rights reserved.

-compiler.optimize
    alias -optimize
    Enable post-link SWF optimization

$ ./mxmlc -help advanced inline

Adobe ActionScript Compiler (mxmlc)
Version 2.0.0 build 354200
Copyright 2004-2012 Adobe Systems Incorporated. All rights reserved.

-compiler.inline
    alias -inline
    turn on the inlining of functions (advanced)



For optimizing ABC/SWF bytecode, again ASC2 since AIR 3.6 supports the avm2.intrinsics.memory package

see Faster ByteArray Operations with ASC2

The ActionScript bytecode generated by FlasCC delivers higher performance than the bytecode generated by the ActionScript compiler. Apart from the usage of better data types and instructions, FlasCC has the advantage of using domain memory which results in faster read/write access of the memory buffer. Flash and AIR Runtimes already support the special memory opcodes needed to use domain memory. The unavailability of a toolchain (Flash Builder, Flash Pro, AIR SDK) did not make it possible to use domain memory for creating .swf files. Beginning AIR 3.6, the ActionScript compiler (ASC2) also generates fast memory opcodes directly from the AS3 code (previously available only through FlasCC).

and
Making ByteArray faster
An ASC 2.0 Domain Memory Opcodes Primer
Domain Memory Opcode Performance: Reading and Writing

Most of those things already mentioned in Memory and Performance.



Now for commenting a bit

there are many things you’re pointing at that are particularly unfair or outdated

I get it that some dev can get confused with the ActionScript Compiler 2.0 Backward Compatibility

it can happen on some tiny use cases that some bugs exists

yeah sure some big popular library may not use this or that

but in the end those tools are there, they do works
ASC2 inlining and fast memory opcodes are perfectly valid and useable features

so please don’t use forum posts as facts because most of what you pointed at are plain wrong

if you want to inline functions in AS3 the right tool is ASC2 and the [Inline] metadata, not Apparat

eg. the right answer on StackOverflow should be the one from Sarah Northway

Adobe introduced native inline functions with the new ASC2 compiler in 2012. Use the -inline compiler argument to inline all getters and setters and any functions marked with the new [Inline] metadata. Inlined functions must meet these conditions:

Not that using Apparat was wrong at the time but there is an history in all that

  • Adobe release project Alchemy as beta/pre-release
  • people discover that Alchemy can write some specific undocumented SWF opcodes
  • Joa Ebbert release Apparat which allow to write those SWF opcodes
  • Adobe release FlasCC (official name of project Alchemy)
  • Adobe release ASC2 which allow to write those SWF opcodes

so yeah there was a relatively short period of time where Apparat was the only tool available
but it did not last forever, sadly the answer on StackOverflow does not reflect that
and so people will have a tendency to think they can only use that feature by using Apparat


#4

@zwetan I already found that ASC 2.0 meta-data trick, but I had compiled test code using MXMLC (what I believe extends from ASC) and nothing got changed in ABC. (Also, what about calling a method from a pushed-scope this?)

And more:

  • ASC 2.0 is painful.
  • No where I had heard about the MXMLC -compiler.inline option. Adobe said nothing about that option.

Anyways, after the next time my laptop gets free, I’ll do more tests.

(Unrelated, but a AS3 dev cannot even run MXMLC in Android… There’s Java N-IDE and Dory.JS, but no MXMLC. Don’t tell me there are VMs (whose performance is poor).)


closed #5

archived #6