Wait-Free Hash Maps in the Entity-Component-System Pattern for Realtime Interactive Systems
Total Page:16
File Type:pdf, Size:1020Kb
Wait-Free Hash Maps in the Entity-Component-System Pattern for Realtime Interactive Systems Patrick Lange∗ Rene Weller† Gabriel Zachmann‡ University of Bremen ABSTRACT objects: Entities, Components and Systems. These are used to de- scribe objects of a RIS via composition instead of object-oriented In the past, the Entity-Component-System (ECS) pattern has be- inheritance. come a major design pattern used in modern architectures for Re- Many advantages arise when using ECS. For instance, sthe be- altime Interactive Systems (RIS). In this paper we introduce high havior of Entities can be changed at runtime by adding or removing performance wait-free hash maps for the System access of Com- Components. Moreover, Systems are likewise interchangeable as ponents within the ECS pattern. This allows non-locking read and they are not part of the Entity nor Component implementation. This write operations, leading to a highly responsive low-latency data allows even the quick swap of e.g. a physics engine. This leads to access while maintaining a consistent data state. Furthermore, we the elimination of ambiguity problems of wide and deep inheritance present centralized as well as decentralized approaches for reduc- hierarchies, often encountered in traditional RIS development. As a ing the memory demand of these memory-intensive wait-free hash result of this, the ECS pattern and variations of it have been applied maps for diverse RIS applications. Our approaches gain their effi- to many RIS, such as [16, 18, 12]. ciency by Component-wise queues which use atomic markup op- However, the ECS pattern does not aim at satisfying the perfor- erations for fast memory deletion. We have implemented our new mance requirement of RIS architectures as it does not specify any method in a current RIS and the results show that our approach is low-level implementation. For instance, the System access imple- able to efficiently reduce the memory usage of wait-free hash maps mentation to the Components is not defined. Usually, every System very effectively by more than a factor of ten while still maintaining iterates over a container (e.g. a array) of all Entities and applies their high performance. Furthermore, we derive best practices from the Systems behavior on the corresponding Components. In fact, our numerical results for different use cases of wait-free hash map modern RIS can consist of hundreds or thousands of Components memory management in diverse RIS applications. and Systems. Therefore, an access parallelization (e.g. in the form Index Terms: H.5.1 [Information Interfaces and Presentation]: of threads or OpenMP support) is necessary in order to maintain Multimedia Information System—Artificial, augmented and vir- realtime performance of the whole application. Consequently, this tual realities D.4.2 [Operating Systems]: Storage Management— container of Components (and therefore every Component) has to Garbage collection be implemented as a shared data structure. Such shared data struc- tures can quickly become bottlenecks of modern RIS applications 1 INTRODUCTION as they typically use crucial software synchronisation patterns such as mutexes, semaphores and consequently synchronization which A central part of Realtime Interactive Systems (RIS), Virtual Re- can lead to thread starvation or system deadlock [17]. The ECS ality (VR) systems, game engines and realtime simulations is the pattern does not cover any guidelines or specifications for effec- generation, management and distribution of the global simulation tively solving this problem but several applicable concurrency con- or world state. Usually many independent software components trol management approaches exist in the literature. need to communicate and exchange data in these modern graph- One promising approach are wait-free concurrency control man- ics interactive systems in order to generate this global state [17]. agements. They guarantee access to a shared data structure in a These components and their corresponding performance within an finite number of steps for each System (eg. as a traditional thread RIS is often governed by the functional as well as non-functional or OpenMP implementation), regardless of other Systems accessing requirements of typical RIS development such as (realtime) perfor- the shared data structure. This means that these approaches do not mance, responsiveness, scalability, consistency and (re-)usability need any traditional locking mechanism in order to preserve a con- [19]. Consequently, RIS development strives for reusable patterns sistent data state. Experiments have shown a superior performance and software architectures in order to increase the satisfaction of of wait-free approaches compared to traditional locking approaches the above mentioned requirements, especially for massive amounts (See Figure 1). Consequently, wait-free approaches deliver high of RIS software components [12, 14]. Hence, diverse approaches performance access even for massive numbers of concurrent read have been presented to tackle this kind of challenge. In the past, the and write operations. However, as a drawback they often have a Entity-Component-System (ECS) pattern has become a major de- large memory footprint [17, 19]: Usually, they rely on a double- sign pattern used in modern architectures for RIS [7]. This pattern buffering approach with atomic operations in order to achieve wait- strives for high reusability and architectural scalability. The main free behavior. This double-buffering creates for every write access idea of ECS is to decouple high-level modules such as physics, ren- to the shared data structure a clone of the manipulated data. When dering or sound from the low-level objects with their correspond- all read operations on the cloned data are finished, the cloned data ing data. Therefore, ECS introduces three software architectural is released. ∗e-mail:[email protected] †e-mail:[email protected] ‡e-mail:[email protected] Hence, the amount of cloned data directly responds to the amount of write operations [17, 19]. In this paper, we present a novel solutions to this challenge; our new approaches allow con- current read- and write access even for highly data driven RIS ap- plications (resp. RIS applications which inherit many Components and/or Systems). Moreover, they can even handle multimodal RIS applications in which different Systems interact with each other in different frequencies. In detail, our contribution of this paper is • an extension of the ECS pattern for high performance double- buffered wait-free hash maps with • centralized as well as decentralized approaches for efficient memory management of these data structures which greatly reduces their memory consumption. Our contribution allows non-locking read and write operations of Systems, leading to a highly responsive low-latency data access while maintaining a consistent state even for structured Compo- Figure 1: Performance comparison of wait-free and lock-based con- nents. Simultaneously, our contribution greatly reduces the mem- currency control management implementations, which are tradition- ory footprint of the introduced wait-free hash amps. Our novel ally used for VR and RIS frameworks. Adopted from [19]. memory management is easy to implement and it fits perfectly into the implementation of wait-free hash maps without altering the ECS pattern itself. threads accessing the shared data structure by introducing a few Our approach therefore greatly benefits the overall RIS perfor- atomic operations [13]. This means that these approaches do not mance. need any traditional locking mechanism in order to preserve a consistent data state. Experiments have shown a superior perfor- 2 RELATED WORK mance of wait-free approaches with respect to traditional locking approaches as Figure 1 illustrates. These wait-free approaches not Research in increasing scalability and performance as well as man- only support structured Components such as arrays or lists but also aging concurrency within RIS frameworks has attracted increasing use fast hash key operations in order to find and retrieve the stored interest in the last decade. This research can be broadly classified Component inside the used hash table. Due to their excellent scal- into two classes: high-level and low-level concepts. High-level ability, they are perfectly suited for RIS frameworks which need to concepts describe the overall RIS software architecture in terms support massive amounts of Systems, such as multi-agent system of software classes which use standard libraries for solving par- based VR and RIS applications [25, 18]. Consequently, using wait- allelization and concurrency of the low-level implementation. Ex- free data structures as a data access backbone can highly improve amples for such high-level concepts are Simulator X [12] with its the performance and scalability of RIS frameworks. actor model [22] or other high-level approaches, e.g. based on However, these wait-free hash maps come at a cost: In order dataflows [23]. Further, [2] gave an overview of high-level archi- to achieve wait-free behavior of read and write operations, they tectures aiming at solving consistency and concurrency for Collab- use double-buffering for write operations. This means that every orative Virtual Environments (CVE). However, this overview ne- write access on the shared data structure is preceded by a double- glected wait-free synchronization approaches. In contrast to these buffering which clones the data [17, 19]. All ongoing read opera- high-level concepts, low-level concepts investigate programming tions can still access the old data state while new read queries are language specific implementations of synchronization approaches directly routed to the new (manipulated) data state. Therefore, after for RIS challenges. In the past, low-level concepts mainly in- a given timespan, the old data will not be used any more. When troduced lock-based concurrency control management (CCM) ap- all read operations on the old data state are finished, the data is re- proaches for RIS, VR and CVE frameworks in order to efficiently leased. Hence, the amount of cloned data directly responds to the implement the high-level concepts.