⬆️ ⬇️

Reducing the processor load in Adobe AIR



Reducing the processor load in Adobe AIR



Let's be honest. AIR is often scolded for exorbitant consumption of RAM and processor resources when used in real time. In spite of the fact that many programs written on AIR sin with this, these problems can be avoided. There are several techniques that allow you to write more "light" programs that are superior to others written by standard means in terms of performance.



One of the easiest ways to dramatically reduce the CPU load is based on changing the frequency of window updates per second (Framerate Throttling). In this article, I will explain the framerate method and show how best to use this method in your programs.

')

Note: To use this method, you must have basic concepts about ActionScript and AIR programming.



What is Framerate Throttling?



Slowdown framerate'ov is a technique that allows you to control the framerate'y program, which, in turn, allows to increase its performance when using (Active) and reduce the amount of consumed resources when it is not used (Idle). As in ActionScript 3, developers have an unusually useful line of code at their disposal - Stage.frameRate . It allows you to change the number of updates to the program window per second on the fly. In previous versions of ActionScript, we had significant problems with the implementation of this feature in the integrated development environment (IDE). Fortunately, times have changed and now complex programs do not "hang", lingering in the computer's memory in the background.



How to use Framerate Throttling?



Since this technique is entirely based on changing the parameters of Stage.frameRate in the direction of a greater or lesser magnitude, the setting and improvement of performance in general depend solely on the developer. However, it also depends on the program itself - some of them allow using this technique more effectively than others.



Note: Performance is measured on a Macbook Pro 2.8 GHz Intel Core 2 Duo using the following code samples and expressed as a percentage. Naturally, the results will vary depending on the configuration of the machine.





"Newbie"



The easiest way to use the framerate easing technique is NativeApplication Event.ACTIVATE and Event.DEACTIVATE ; these events increase the number of framerates when the program is used (Active) and decreases when it is inactive (Idle). With a single empty window, the results showed a processor load of 1.8% in usage mode and 0.4% in idle mode. In general, you can set the frequency of updating the program window to 0.01 or turn it off, then the program takes only 0.2% of the total CPU load, while I found out that the chrome window does not lose focus.



package {

import flash.desktop.NativeApplication;

import flash.display.Sprite;

import flash.events.Event;



public class Application extends Sprite {

public function Application () {

__init ();

}



private function __init ():void {

NativeApplication.nativeApplication.addEventListener

(Event.ACTIVATE, __activate__);

NativeApplication.nativeApplication.addEventListener

(Event.DEACTIVATE, __deactivate__);

}



private function __activate__

($event:Event):void {

stage.frameRate = 50;

}

private function __deactivate__ ($event:Event):void {

stage.frameRate = 1;

}

}

}




"Advanced"



Certain programs make it possible to more effectively use the technique of slowing down framerates. For example, a program that needs some level of interaction, even in the background. A program that has scrollable content with links, so AIR allows the mouse to scroll through the contents of the program when it is active, which in turn means that you currently need a higher framerate value.



In this example, if the program is in the background, but the mouse scrolls the contents of the program, MouseEvent.MOUSE_WHEEL increases the framerate and sets the Event.ENTER_FRAME event, which reduces the framerate by half a second after scrolling. In cases similar to this, the use of a buffer will be the best choice, which will make it possible not to change the framerate every time with each scrolling, and also because then there will be no event for the moment when the mouse wheel is in idle mode.





package {

import flash.desktop.NativeApplication;

import flash.display.Sprite;

import flash.events.Event;

import flash.events.MouseEvent;

import flash.utils.getTimer;



public class Application extends Sprite {

public static const ACTIVE:int = 50;

public static const INACTIVE:int = 1;



public var active:Boolean;

public var scrolling:Boolean;

public var buffer:int;



public function Application () {

__init ();

}



private function __init ():void {

NativeApplication.nativeApplication.addEventListener

(Event.ACTIVATE, __activate__);

NativeApplication.nativeApplication.addEventListener

(Event.DEACTIVATE, __deactivate__);

stage.addEventListener

(MouseEvent.MOUSE_WHEEL, __mouseWheel__);

}



private function __activate__ ($event:Event):void {

active = true;

stage.frameRate = ACTIVE;

}

private function __deactivate__ ($event:Event):void {

active = false;

stage.frameRate = INACTIVE;

}

private function __mouseWheel__ ($event:MouseEvent):void {

if (!active) {

if (!scrolling) {

stage.addEventListener

(Event.ENTER_FRAME, __enterframe__);

}

stage.frameRate = ACTIVE;

scrolling = true;

buffer = getTimer () + 500;

}

}

private function __enterframe__

($event:Event):void {

if (buffer < getTimer ()) {

stage.frameRate = INACTIVE;

scrolling = false;

stage.removeEventListener

(Event.ENTER_FRAME, __enterframe__);

}

}

}

}




"Expert"



If optimization is the meaning of your life, then you can surprise your friends with complex manipulations with slowing down the number of framerates. (Note: this will not impress your girlfriend).



In my programs, I like to make the transitions from one window state to another smooth and more aesthetic. Therefore, I use a high number of framerates - 50. Unfortunately, such a large number leads to a heavy load on the CPU. Therefore, I set framerate 50 only when moving elements \ windows. After the end of the movement, I reduce the number of framerates to 24. There are cases when the loader plays the animation, while the program itself is in the background. The loader doesn't need 50 fps, so I set the framerate number to 5 when the program is visible in the background and I set 1 when not visible.



Note: for this example, I use animate() to call at the beginning of each movement. You may want to integrate your own fps retarder into your tweet engine, so you will no longer need to manually call animate() .



package {

import flash.desktop.NativeApplication;

import flash.display.Sprite;

import flash.events.Event;

import flash.utils.getTimer;



public class Application extends Sprite {

public static const ANIMATING:int = 50;

public static const ACTIVE:int = 24;

public static const INACTIVE_VISIBLE:int = 5;

public static const INACTIVE_INVISIBLE:int = 1;



public var active:Boolean;

public var animating:Boolean;

public var buffer:int;



public function Application () {

__init ();

}



private function __init ():void {

NativeApplication.nativeApplication.addEventListener

(Event.ACTIVATE, __activate__);

NativeApplication.nativeApplication.addEventListener

(Event.DEACTIVATE, __deactivate__);

}



public function activate ():void {

if (!animating) {

stage.frameRate = ACTIVE;

}

}

public function deactivate ():void {

if (!animating) {

stage.frameRate = (stage.nativeWindow.visible) ?

INACTIVE_VISIBLE : INACTIVE_INVISIBLE;

}

}

public function animate ($duration:int = 1000):void {

stage.frameRate = 50;

buffer = getTimer () + $duration;

animating = true;



if (!animating) {

stage.addEventListener (Event.ENTER_FRAME, __checkBuffer__);

}

}



private function __activate__ ($event:Event):void {

active = true;

activate ();

}

private function __deactivate__ ($event:Event):void {

active = false;

deactivate ();

}

private function __checkBuffer__ ($event:Event):void {

if (buffer < getTimer ()) {

stage.removeEventListener

(Event.ENTER_FRAME, __checkBuffer__);

animating = false;

if (active) {

activate ();

} else {

deactivate ();

}

}

}

}

}




Slowing framerate'ov only a small chapter in the optimization of your program, written in AIR. But these are the basics of optimization, and the use of this technique will lead you to the optimal use of computer resources when writing programs. Agree, no one likes cumbersome and inconvenient programs.



about the author



Johnny is an experienced Adobe XDCE team designer. Before joining Adobe, he developed AIR programs such as DestroyFlickr and DestroyTwitter in his free time for entertainment. He also has his own blog, “Destroy Today,” which has become an opportunity for him to share his findings in programming with others. Oddly enough, after all, Johnny received a Bachelor in the College of Art at the Institute of Maryland.



The original text in English is here:

www.adobe.com/devnet/air/flex/articles/framerate_throttling.html

Source: https://habr.com/ru/post/91961/



All Articles