This article is first sent from the public account “Chengxiang Ink Shadow (ID: cxmyDev)”, welcome to follow.

In The wechat open course Pro 2019 in January this year, the wechat performance optimization framework “Hardcoder” was finally open source.

Wechat open source stuff, as Android development, is of course double-click on 666 again.

But as an Android developer, I’m more concerned about what Handcoder is. What’s the impact on us Android developers?

What is a Hardcoder?

Hardcoder was first released on wechat in 2018. To put it simply, Hardcoder is a performance optimization framework developed by wechat.

If you read such a sentence, it is easy to understand that as long as we access Hardcoder in App after wechat open source, we can be as smooth as wechat.

But the ideal is always full…

Hardcoder itself is also a product of business development. As wechat becomes more and more complex, conventional performance optimization has brought less and less improvement. However, from the manufacturer’s point of view, wechat, the national App, should be open in seconds without the delay, to achieve the ultimate optimization.

For example, if the manufacturer’s phone is found to be broken, the user must think there is something wrong with the phone, not wechat.

You know what they say, “When you’re strong, the whole world smiles on you.”

Manufacturers are also aware of this, so part of the early manufacturers will be for wechat, do some small optimization, which is more typical of the “violence frequency”. When the system recognizes that wechat is currently running, it will increase the CPU frequency roughly, thus improving the running performance of App.

You know, all “one size fits all” things are exposing some problems.

The same goes for “violent frequency enhancement.” Increasing the CPU frequency too much is bound to have an impact on your phone’s power consumption, most immediately when you find that your phone drains faster.

Manufacturers’ limitations on hardware resources are partly considered from the perspective of power consumption. With more and more functions of mobile phones, battery technology can’t keep up. A mobile phone with powerful functions but inseparable from power supply will certainly not be the ideal choice for users.

So the phone manufacturer will make some restrictions on the hardware, when they don’t need so many resources, they will give less resources. However, the reduction of resources will inevitably cause some experience problems, such as lag, and this kind of user experience problems, manufacturers are not willing to.

After all, who card, who not card, user psychology naturally have a steelyard.

However, there is a difficulty in this. The fundamental reason why manufacturers choose such a violent frequency improvement scheme is that, as a mobile phone, there is no way to know exactly what the App is doing and whether it needs resources. After all, now App has too many functions, and it cannot be optimized precisely by mobile phone itself.

Since the manufacturer wanted to optimize the experience, and wechat had the need to optimize the performance of the App, it really hit it off, and Hardcoder was born. Hardcoder builds a reliable communication framework between App and system (ROM) to let the system know the requirements of App.

Before the manufacturer did not know when to give resources, when to save resources, is really afraid of him not to come and afraid of his disorderly.

Now, with Hardcoder, since the system does not know it, the App will actively tell it, which is equivalent to wechat actively telling the mobile phone system what scene I am in or what I am going to do next. This scene needs some system resources to support, so as to ensure that the speed is fast and the speed will not be stuck.

So this should make sense, Hardcoder is more like a communication framework that tells the operating system, I’m going to do something complicated, please give me resources.

Hardcoder itself, and the vendors, have preset some scenario values that can be used directly.

int APP_SCENE_UNDEFINE = 0;                 // Undefined scenario

int APP_SCENE_STARTUP = 1;                  // Process starts, APP starts

int APP_SCENE_WINDOW_SWITCH = 2;            //UI page switch (same process), activity, fragment switch

int APP_SCENE_WINDOW_SCROLL = 3;            //UI page slide

int APP_SCENE_DATA_LOADING_AND_PROCESS = 4// Data loading and processing tasks refer to the local front and background tasks of APP

int APP_SCENE_MULTI_MEDIA_PROCESS = 5;      // Multimedia related services

int APP_SCENE_COMMUNICATE = 6;              //APP/ server communication

int APP_SCENE_SYSTEM_DEVICE = 7;            // Invoke system services

Copy the code

Of course you can define it yourself.

The principle of Hardcoder

Now that you know what Hardcoder does, take a quick look at its design.

Hardcoder builds a reliable communication framework between App and system, breaking out of the problem that App can only call the system standard API, but cannot directly call the underlying hardware resources of the system, so that real-time communication can be realized between Android App and system.

With Hardcoder, App can apply for more hardware resources, such as CPU frequency, large and small core, GPU frequency, etc. from the system when necessary to improve the performance of App.

Hardcoder framework, divided into Client end and Server end, this open source is the Client end, the Server end by the manufacturer system side to achieve their own.

The communication between the Hardcoder Client and Server uses LocalSocket, which means that if the App needs resources, it sends a request to the Server implemented by the system through the Hardcoder Client. After the system receives the request, Adjust different system resources as needed, such as giving larger CPU frequencies, tying the system to a larger core, and so on.

Speaking of LocalSocket, I think Android developers should be familiar with it. Because at the Java level, Android itself provides an API for LocalSocket, but Hardcoder doesn’t use it. Because Hardcoder mainly adopts Native implementation, wechat uses Linux Socket interface in C layer and implements a set of LocalSocket by itself.

This is probably the big man’s world, can’t use their own writing.

So how much of a performance improvement is there using Hardcoder?

According to the official data, the average optimization effect is 10%~30%, and because wechat’s application for resources is also more precise and accurate, the power consumption only increases by 2%, which is equivalent to 20% performance improvement with 2% power consumption. This data should be acceptable in the view of the manufacturer.

Hardcoder framework also covers a large number of devices. Currently, Hardcoder framework has been connected to OPPO, Vivo, Huawei, Xiaomi, Samsung, Meizu and other mainstream mobile phone manufacturers, covering more than 460 million devices.

This should make sense by now. To put it plainly, Hardcoder is a communication framework between App and system. The performance optimization framework mentioned before, but in fact, the real logic is in the Server side. If you want to apply for resources, you can use Hardcoder to apply for your resources.

The average developer looks at Hardcoder

Size dictates complexity, and there’s a huge difference between a simple technology for 10 million users and a billion users.

Some things, wechat can do, even if you got the source of wechat, you can’t do it. Mainly because of wechat such user volume, let manufacturers all the way to the green light.

According to the document, Hardcoder has been supported by mainstream manufacturers in China, and some of them have opened the permission application mode.

It should be difficult to apply for commercial channels like VIVO, but OPPO and Huawei, which do not need to register, can use Hardcoder.

However, Hardcoder is essentially a framework for communicating with the system. As for whether the system responds to your request to apply for resources, it completely rejects the logic of the system side. It is not up to us to decide whether the other party can accept the message you send to apply for resources.

As mentioned in the document, Hardcoder Server also has certain restrictions on application request resources, such as the current App in the foreground or background, will have different processing logic.

From the point of view of wechat, Hardcoder is a performance optimization framework. From the point of view of ordinary developers, Hardcoder may just be a communication framework, and it is also a one-way communication framework. Whether it works depends on the manufacturer, this part can be called metaphysics.

Maybe you don’t work on A phone and it works on B phone, or maybe you work on A test phone and it doesn’t work on A private phone.

However, improving the user experience has always been a vendor’s goal, which is consistent with the goal of most App developers, so Hardcoder has value in the long run. However, in the domestic environment of the whole family of APPS, it naturally makes manufacturers distrust APPS, pushing manufacturers and APPS into opposition.

But if the manufacturer has a standard verification process, it can test whether the App developer is really “honest”, controlling precisely when needed, applying only when needed and not when not. Can let App and manufacturer happy play.

Before Hardcoder was released, it was not only used by wechat, but also used by many Tencent products, such as QQ, enterprise wechat, Tiantian Express and so on. Equal to wechat this time open source Hardcoder, if manufacturers want to open up for the majority of developers to use, in fact, the cost is very low.

At the end of the day, it’s up to the vendors to decide whether Hardcoder works, so we’re looking forward to hearing from subsequent vendors.

Finally, Hardcoder’s documentation, which is open source on Github, is worth reading and learning for Android developers.

What do you think of Hardcoder? Leave a comment.


references:

https://github.com/Tencent/Hardcoder

Did this article help you? Message, forward, favorites is the biggest support, thank you!


“Growth”, will get the learning materials I prepared.