Profiling a Prototype Game Engine Based on an Entity Component
Total Page:16
File Type:pdf, Size:1020Kb
Profiling a Prototype Game Engine Based on an Entity Component System in C++ Dennis Nilsson & Anton Björkman Malmö University 2019-06-23 Faculty of Science Department of Computer Science Degree Project in Game Development, 15 credits Supervisor: Carl Johan Gribel Examiner: Olle Lindberg 2019-06-23 We would like to thank our supervisor Carl Johan Gribel and examinator Olle Lindeberg for supporting us while writing this thesis at Malmö University. 1 Abstract—This paper introduces a performance comparison be done by conducting benchmarks on two artifacts, a data- between an object-oriented artifact and a data-oriented artifact oriented and an object-oriented. The benchmarks will record in the context of using an entity component system. the time it takes to render frames in milliseconds. By analyzing A game engine must handle a variety of game objects which may share common attributes such as a transformation, collision the results of the benchmarks, this thesis aims to contribute by and input management. Instead of solving this via multiple presenting a performance comparison between a data-oriented inheritance, entity component system uses composition to de- or an object-oriented artifact. The relevance of this thesis is couple traits into data (components) and systems operating on providing important insight for developers when performance them. Game objects (entities) then serve as containers for all is a prioritized quality attribute. Future work extending our components necessary for a particular purpose, such as a player character of a piece of platform. Composition is not necessarily thesis could be examining the number of CPU cache misses data oriented however, as evident by recent development in e.g. per benchmark. Furthermore benchmarks in 3D environments the Unity engine [13]. This thesis will implement two artifacts. would be of great importance for contemporary developers One data-oriented artifact using an entity component system. One within the game industry. object-oriented artifact using an entity component system. These The structure of this paper is as follows: This section, two artifacts will be stress tested to explore the performance. Keywords: Data-oriented design, Object-oriented design, En- the introduction. Section II takes up background and related tity Component System work. Section III explains the method used conducting the experiment. Section IV presents results and data. Section V & VI includes discussion and conclusion. Abbreviations will be I. INTRODUCTION avoided to minimize confusion. Games are becoming larger for each generation inevitably resulting in more computations for the hardware to perform. II. BACKGROUND AND RELATED WORK One reason for the continuing enlargement of games is the as- piration of achieving higher realism than previous generations The complexity of games striving for a realistic experience and or adding more content. Unity is currently component- graphics-wise are increasing for each generation of consoles. based but is introducing a new component which is an entity This derives partly from the increasing amount of polygons component system. This is because component-based systems in striving for realistic graphics [12]. It also derives from did not age well because it did not align data in memory the fact that the more data a game scene contains, the more possibly resulting in low cache coherence [13]. Programming computation power is required to maintain for example 60 using an object-oriented design without considering the data frames per second, as opposed to a game scene containing less allocation can result in inefficient allocation of data in memory data. Stable frames per second relies on computations being due to factors such as encapsulation of data, inheritance, finished within the time limit of the game loop [15]. As an polymorphism. Encapsulation of data refers to bundling of data example, to be able to maintain ∼60 fps, ∼16 milliseconds and methods operating on that data in the same class. Logic (ms) is the game loops time limit. Not being able to achieve and data bundled together in a class could lead to low cache stable frames per second is considered as game-breaking in coherence. A low cache-coherence can possibly require more the game industry. fetches from memory from the central processing unit (CPU) Even though computation power has been increasing since affecting performance negatively. A data-driven design such as the arrival of computers, there are complications such as an entity component system has the property of aligning data the difference in clock speed between the CPU and memory locally which could lead to improved cache-coherence [10], [15]. Factors such as increased power consumption, heat and [6]. thermal losses is contributing in the challenge of developing Today’s games require efficient computation of data to faster CPU’s [9]. While CPU’s has mostly increased in clock maintain stable frames per second (fps). Unity currently uses speed, DRAM’s been mostly increasing in size. This is a a component-based entity system. This simplifies game de- bottleneck in performance because the clock speeds in CPUs velopment since its intuitive to add components to objects. A exceed the clock speeds in DRAM memory [2]. Possibly with consequence of this is, in order to be able to create or destroy the outcome that the CPU waits a couple of hundred cycles objects in Unity, a global list must be modified containing the until the DRAM is able to provide the necessary data for names of each object. This will require a mutex lock. Another processing [15]. Since cache storage exist in the CPU’s for consequence is an extension of the previous consequence. Ev- speeding up memory access, developers should take advantage ery game object has a C# wrapper pointing to the C++ object. of that. By utilizing as much of the hardware’s capacity as There is no control over where in memory it is allocated, possible. As Bob Nystrom mentions, “Modern CPUs have meaning it could be anywhere [13]. This could potentially caches to speed up memory access. These can access memory increase the amount of cache misses. A cache miss happens adjacent to recently accessed memory much quicker. Take when data required for the CPU’s next operation does not advantage of that to improve performance by increasing data exist in the cache. The CPU then has to fetch new data from locality - keeping data in contiguous memory in the order that memory in order to continue its work. Data-oriented design you process it” [15]. such as an entity component system focuses on aligning data Our definition of data-oriented design is the alignment of locally. This can possibly reduce the amount of cache misses. data in memory with the goal of increasing data locality. The purpose of this study is to compare the performance of a Furthermore it is the alignment of data in the order that it data-oriented artifact and an object-oriented artifact. This will will be processed in run-time. A data-oriented design tends 2 to promote data locality. It is important to align data so that memory so when code is running the things to process is next it can be processed after each other in the cache line. By to each other in memory. Like reading a book with coherent following a data-oriented design developers could increase the text. Figure 2 shows an illustration of how aligned data may cache coherence. An object-oriented design unfortunately tend look like in memory. to spread data randomly across the memory due to storing objects on the heap, unless a memory pool is implemented Figure 2: Visualization of aligned data in memory. and used. A positive effect of an object-oriented approach is the readability. For instance a human would be described in one class, as one object with all the properties a human holds. C. Entity Component System This thesis will address the performance of a data-oriented Entity Component System is an architectural pattern that in compared to an object-oriented artifact in a game-like envi- a sense is an inverted object-oriented design. Instead of the ronment when using an entity component system. The main entities using its own implementations of logic, the entities contribution of the benchmarks is displaying the performance are acted upon by components which in turn are controlled of, in render time in milliseconds per frame, a data-oriented through systems [5]. Entity component systems are novel and artifact and an object-oriented artifact using an entity compo- there is not much research within the area. Unity is integrating nent system. entity component system as a component in their game engine. RQ: What is the performance of a data-oriented artifact This is due to the ability of retaining the user-friendliness of compared to an object-oriented artifact simulating a game-like the component-based system currently used meanwhile also environment using an entity component system? gaining performance and parallelism [13]. An illustration of Hypothesis: The data-oriented artifact renders frames faster an entity component system is shown in Figure 3. than the object-oriented artifact depending on better cache performance. Entities Entity 1 A. Object oriented Object-oriented design bring several attributes such as read- n ability, inheritance, polymorphism, encapsulation of data and Body Collideable Renderable reusability. The readability attribute can simplify the com- Components munication within a project. Object-oriented design strives n after describing things as humans perceive it. It makes it easier for team members to familiarize with a project. A m possible negative consequence of object-oriented design is the Systems MovementSystem CollisionSystem RenderSystem potential dynamic allocation of objects in run time. This can be counteracted by allocating memory for objects before run time. Figure 3: Visualization of an Entity Component System.