Mobile application high-level language development - concurrent exploration

This article is reproduced from OpenHarmony TSC official WeChat public account " Summit Review No. 9 | Mobile Application Advanced Language Development - Concurrency Exploration "

Speaker | Li Yongbiao

Review and arrangement | Liao Tao

Typesetting proofreading | Li Pingping

Guest profile

Li Yongbiao, a member of the programming language TSG of the Technical Steering Committee of the OpenHarmony project group, and an expert in the compiler of Huawei's OpenHarmony virtual machine. From 2021 to now, the architect of Huawei terminal OS language compilation and runtime team, responsible for the overall technical architecture of OpenHarmony high-level language compilation and runtime. Worked in Alibaba, participated in and led AliOS high-level language virtual machine compilation optimization, memory management optimization, multi-thread optimization and other projects. Currently focusing on the programming language and language virtual machine of mobile OS.

content source

The First Open Atom Open Source Foundation OpenHarmony Technology Summit - Programming Language and Application Framework Sub-Forum

video review

Video link:

Summit Review Issue 9 | Advanced Language Development for Mobile Applications - Concurrency Exploration (Li Yongbiao)_哔哩哔哩_bilibili

Contents

With the slowing of Moore's Law becoming more prominent and the emergence of multi-core computer system architectures, concurrent programming continues to attract a lot of attention. At present, what are the concurrent explorations in the field of mobile applications, and what are the achievements and future plans on OpenHarmony? Li Yongbiao, Huawei Terminal Compiler and Runtime Architect, shared a few points with you at the first OpenHarmony Technology Summit.

01 ►Concurrent background

Moore's Law was proposed by Gordon Moore, the co-founder and former CEO of Intel, that is, the number of transistors and resistors integrated on a semiconductor chip will double every two years, the performance of the microprocessor will double, or the price will drop by half. In the past few decades, Moore's Law has made a huge contribution to the development of computing power and even productivity, but by 2022, as the Denard scaling effect has encountered the bottleneck of component physics, the industry has gradually had strong differences on this point of view , Some people think that Moore's Law is gradually slowing down or even has a tendency to fail, while others think that Moore's Law is still valid, but the cost has increased. In this context, the power consumption of a single chip constrains the performance of a single core, and concurrent programming based on multi-core has become an important technical means to improve performance in the mobile field.

Moore's Law slowing down

So, what is concurrency? Concurrency refers to the ability to deal with multiple tasks at the same time. Several tasks are ready to run in a period of time. On a single-core device, only one task can run at any time, and its running order is not fixed; and In a multi-core scenario, multiple tasks can run in parallel at the same time.

Concurrency and Parallelism

02Common concurrency models

2.1 ►►Threads and locks

Threads and locks are common representations of concurrency models. The figure below shows a typical CPU multi-core architecture. It can be seen that L3 Cache is shared among multiple cores, so the consistency of L3 Cache is to ensure the normal operation of programs among multiple cores. Base. Under the premise of ensuring that the single-threaded execution results remain unchanged, any compiler optimization can be done, such as constant propagation, common subexpression elimination, inlining, scalar replacement, dead code deletion, instruction reordering, etc. Since different operating systems have different constraints on memory consistency, it is necessary to use synchronization semantics such as locks to ensure multi-thread memory consistency. At present, multi-threaded programs with excellent performance can be realized through means such as optimistic locks based on shared memory, spin locks, biased locks, and precise memory barriers, but there are also certain problems: the optimization of thread and lock schemes depends on software engineering. Concurrency practice norms and experienced concurrent program developers, and it is easy to cause deadlocks, and the cost of testing and maintenance is also high.

A CPU multi-core architecture

2.2 ►► Actor model

The concept of the actor model has been proposed for decades: an actor is a basic computing unit that communicates through messages; it maintains a variable state internally, and the two sides cannot directly modify each other. Its advantage is that each actor is easy to maintain and test. Business development only needs to care about business message processing, and testing only needs to cover the order of message flow. In addition, its fault tolerance is also good, suitable for distributed programming. Of course, the Actor model also has certain shortcomings: there is a problem of full mailboxes, no state sharing, and only interaction through message communication, which is not suitable for fine-grained parallelism.

Actor model

2.3 ►►Functional programming

Functional programming has the following characteristics: functions are first-class citizens; no side effects (no shared mutable state); pure functions build everything. Under functional programming, as long as the input is certain, the output must also meet expectations. The functional formula in the real world is more dependent on parameterization, and will throw the side effect of the function (Side Effect) up, try to separate from the business logic layer written by the developer, and process it inside the framework, and has structural dependencies. This mode has the advantages of determinism, robustness (easy maintenance and easy testing), and natural support for parallelization, but the development efficiency is low. It is difficult to directly convert the actual business logic into functional development, and the performance is poor in some scenarios. .

functional programming

2.4 ►► Concurrency Model Features

Common concurrency models are mainly divided into two categories, one is based only on shared memory, and the other is based on message communication. The concurrency model (threads and locks) based on shared memory has the advantages of wide application range, close to the nature of hardware working methods, and high efficiency when used correctly, but there are inevitably problems such as difficult testing and maintenance. At present, this model has gradually Abandoned by the field of application development; the concurrency model based on message communication (Actor, functional programming) has the advantages of good fault tolerance, good performance in specific scenarios, and easy maintenance and testing, but there are also limited application scenarios and are not suitable for fine-grained Short boards such as parallelism.

03 ►Mobile application framework concurrency

3.1 ►► Dart language

Dart is a new programming language. Like JAVA and PHP, it was invented by Google to solve some practical problems in writing applications. In its early days, it was mainly to replace JavaScript (hereinafter referred to as JS) in the Web field. Later, Google internally used Dart to write and incubate a mobile development framework Sky, which was later named Flutter, which shined in the field of mobile cross-platform development. Dart's concurrency goals are mainly to enable the framework to support task parallelization, and to solve developers' concurrent tasks and multi-threaded development needs. It only shares immutable objects, but not mutable objects, and provides concurrency APIs for single-threaded concurrency (asynchronous) and multi-threaded concurrency (Isolate Spawn/Compute).

Dart Concurrency Architecture

3.2►►Swift

Swift is a new development language released by Apple at the WWDC Apple Developer Conference in 2014. It can run together with Objective-C on macOS and iOS platforms, and is used to build applications based on Apple platforms. In the Swift 5.5 version in 2022, the description of the concurrency API was released. The concurrency goal is mainly to reduce the time that application developers must spend from idea to implementation, so that the experience is far superior to existing solutions (queue agnostic, maintainable poor performance and low security). Swift's concurrency philosophy is that shared mutable state is not good for developers, it is not good for hardware, and it cannot break through a single process. Therefore, Swift hopes to provide non-destructive and easy-to-use APIs, and to continuously improve in terms of design, maintainability, security, scalability, and performance. Currently, the APIs provided by Swift include async/await, Task & TaskGroup, Actor, etc.

3.3 ►►Concurrency Model Trends of Popular Mobile Operating Systems

In the field of mobile application development, iterations and updates are frequent, and development efficiency is a key factor. However, locks are too low-level for application developers, difficult to use and debug, and belong to advanced usage. In order to provide an easy-to-use, simple, and efficient concurrency model, the multi-threading model currently provided by the industry to application developers tends to avoid data competition and achieve lock-free. For example, on the Web, JS uses a message communication mechanism in multi-threading (memory isolation, adding support for transferable Builtin objects); on Flutter, Dart uses a message communication mechanism in multi-threading (memory isolation); on Android , Kotlin natively proposed Worker API, immutable sharing, object transfer freezing and other solutions to replace the shared multi-threading solution (users do not use locks); on IOS, Swift 5.5 implements structured programming and Actor, the evolution of Swift's overall concurrency is a programming model that is safe by default.

04Concurrent exploration of OpenHarmony high-level language

In the concurrency of the JS world, the Actor model, the JS concurrency architecture mentioned above, has the advantages of no lock, easy maintenance and testing, good fault tolerance, and distributed programming, but it starts slowly and the overhead of concurrent instances is high. For the JS concurrency API—Worker, because each of its concurrent instances is not shared, developers need to encapsulate and parse message commands, care about the life cycle of Worker instances, and accurately adjust the number of Workers under high load and low load.

Based on the above problems, a lightweight concurrent instance - Lite Actor is implemented on OpenHarmony. This function supports the sharing of immutable objects, lightweight the infrastructure, greatly improves the startup time, and optimizes the startup memory.

Concurrent instances of ArkCompiler run

OpenHarmony also provides TaskPool. TaskPool is an easier-to-use concurrent task API, which enables developers to easily develop concurrent tasks, reduce the amount of code writing, and let them not care about the life cycle of concurrent instances and the load of concurrent tasks in scenarios. In addition, TaskPool can also unify the resource management of task loads, reduce the resource consumption of the system, and improve the overall performance of the system. In the TaskPool architecture shown in the figure, functions such as priority scheduling, load balancing, and unified management of the system are realized through Task Dispatch Manager, and self-adaptability and scalability are realized through Task Worker Threads.

TaskPool unified task pool design architecture

In the development of high-level language concurrency, the industry is more inclined to provide developers with easier-to-use, better-use and more efficient concurrency APIs. The concurrency API provided by OpenHarmony is currently between Dart and Swift, drawing on the strengths of both, and continuously optimizing and improving its existing problems. Additionally, OpenHarmony is considering introducing quasi-static objects for true sharing.

05 ►Summary _

On the OpenHarmony concurrency model, the concurrent instance will be further lightweighted in the future, and the lock-free concurrency of shared objects will be explored. In addition, in terms of OpenHarmony concurrent scheduling, we will also design related solutions from the two dimensions of time and space to optimize and improve the problem of thread flooding in the existing system. Stateful and executable resources automatically schedule and execute tasks concurrently.

Welcome everyone to continue to pay attention to OpenHarmony's concurrency research work, and look forward to working with community developers to build OpenHarmony's concurrency capabilities.

Click to follow to learn more about OpenHarmony TSC technical content

Guess you like

Origin blog.csdn.net/OpenHarmony_dev/article/details/132538758