Best practices for Online Game Performance Optimization: latency strategy

Source: Internet
Author: User

Web games, especially the instant combat webgame with multiple online users at the same time, performance optimization is an inevitable problem in the development process and in the later stages of development. After the hardships, we have summarized a Best Practice: latency strategy. To put it bluntly, just one sentence: Do not do too many things in one frame. Then, let's make a summary.

Understanding asynchronous single thread of Flash Player

First, we should understand the asynchronous single-thread mechanism of Flash Player. Of course, except for the multi-threaded APIs of the new FP version. The single thread of FP is shown in that we can control only one thread, which can be expressed in frame loop. The runway model tells us that some code is run at each frame and then rendered. So which code is executed? This is supported by the event mechanism. Events are distributed asynchronously to complete animation rendering. For example, we create a loader object at a certain time and download an image. At this time, the main thread continues to execute. FP will create a new thread to complete the download, dispatch an event. Complete Event, and notify the main thread to handle it.

This is a bit like the Javascript operating mechanism. Javascript is also used to run code in a single thread. It splits the code into small code fragments and executes the code. Ajax asynchronous requests are like the loader in flash, except that AJAX XMLHttpRequest is an interface opened directly by the browser, and Flash loader is an interface opened by the browser plug-in.

Animation Implementation of Web games

In web games, enter_frame events are usually added. Each frame is used to detect data changes and complete various functions and animations. This is like a story where there are two "timelines", one is data, and the other is execution. We can change the data at any time and wait for the next enter_frame to execute the animation. For example, if we set the role's current action to "Walking", then when the next frame enters, use some code to change the role bitmap to "Walking" and assign the bitmapdata value directly.

Causes and optimization of performance bottlenecks

If the processing of an enter_frame takes too long and exceeds the time of one frame, the performance bottleneck arises. The following code describes the problem:

private function onEnterFrame(e:Event):void{    frameCount++;    trace(frameCount, flash.utils.getTimer() + "ms");    // so heavy    for (var i:int = 0; i < 100000; i ++) {        var s:Sprite = new Sprite();        addChild(s);    }}

Output:

1 324ms2 1833ms3 3019ms4 4569ms5 7344ms

The interval between each frame exceeds 1 second ...... Method execution is too long, while FP is a single thread, which causes enter_frame events to fail to be distributed according to the original time. Frame time is extended, resulting in lower frame rate. The minimum time segment that the human eye can tell is about 100 ms. In this case, the frame rate is reduced to 10 Based on the coordination between the best "data" and "execution.

Obviously, there is a solution. Don't do too much work in one frame! In general scenarios, such as the appearance of other players and the walking of monsters, the real-time requirement is not high. A common example is that there are 50 monsters in a new scene. The server will send the information of 50 monsters to the front end at one time. If we initialize and render 50 monsters within one frame, I am afraid this frame will be extended. We can cache these monsters and listen to another enter_frame event to process five monsters at each frame.

Share the work of one frame in the subsequent Frame

Insert another sentence, which is a bit like JavaScript. After setinterval in JS, if the execution time of a code segment is too long, the interval cannot be set. The same.

Conclusion

In the case of frame rate 25, the time of a frame is only 40 ms. This requires that we do not abuse each millisecond. The performance of the as3 language we usually talk about, for example, using shift to get an integer and replacing array with vector, is often not the bottleneck of performance. Splitting a large number of operations into several parts for latency processing is the best practice for optimizing the performance of web games.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.