From f94c658c73e0b886ce485135da552ee7eb0c8661 Mon Sep 17 00:00:00 2001 From: ranjeet-floyd Date: Sun, 16 Jun 2019 12:32:00 +0530 Subject: [PATCH 1/4] Data Locality pattern #559 --- data-locality/README.md | 30 ++++++++ data-locality/pom.xml | 63 ++++++++++++++++ .../iluwatar/data/locality/Application.java | 39 ++++++++++ .../data/locality/game/GameEntity.java | 71 +++++++++++++++++++ .../locality/game/component/AiComponent.java | 37 ++++++++++ .../locality/game/component/Component.java | 36 ++++++++++ .../game/component/PhysicsComponent.java | 36 ++++++++++ .../game/component/RenderComponent.java | 37 ++++++++++ .../component/manager/AiComponentManager.java | 63 ++++++++++++++++ .../manager/PhysicsComponentManager.java | 64 +++++++++++++++++ .../manager/RenderComponentManager.java | 64 +++++++++++++++++ .../data/locality/ApplicationTest.java | 40 +++++++++++ 12 files changed, 580 insertions(+) create mode 100644 data-locality/README.md create mode 100644 data-locality/pom.xml create mode 100644 data-locality/src/main/java/com/iluwatar/data/locality/Application.java create mode 100644 data-locality/src/main/java/com/iluwatar/data/locality/game/GameEntity.java create mode 100644 data-locality/src/main/java/com/iluwatar/data/locality/game/component/AiComponent.java create mode 100644 data-locality/src/main/java/com/iluwatar/data/locality/game/component/Component.java create mode 100644 data-locality/src/main/java/com/iluwatar/data/locality/game/component/PhysicsComponent.java create mode 100644 data-locality/src/main/java/com/iluwatar/data/locality/game/component/RenderComponent.java create mode 100644 data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/AiComponentManager.java create mode 100644 data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/PhysicsComponentManager.java create mode 100644 data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/RenderComponentManager.java create mode 100644 data-locality/src/test/java/com/iluwatar/data/locality/ApplicationTest.java diff --git a/data-locality/README.md b/data-locality/README.md new file mode 100644 index 000000000000..6a72a45194b3 --- /dev/null +++ b/data-locality/README.md @@ -0,0 +1,30 @@ +--- +layout: pattern +title: Data Locality +folder: data-locality +permalink: /patterns/data-locality/ +categories: Other +tags: + - Java + - Difficulty-Intermediate + - Performance +--- + +## Intent +Accelerate memory access by arranging data to take advantage of CPU caching. + +Modern CPUs have caches to speed up memory access. These can access memory adjacent to recently accessed memory much quicker. Take advantage of that to improve performance by increasing data locality keeping data in contiguous memory in the order that you process it. + + +## Applicability + +* Like most optimizations, the first guideline for using the Data Locality pattern is when you have a performance problem. +* With this pattern specifically, you’ll also want to be sure your performance problems are caused by cache misses. + +## Real world example + +* The [Artemis](http://gamadu.com/artemis/) game engine is one of the first and better-known frameworks that uses simple IDs for game entities. + +## Credits + +* [Game Programming Patterns Optimization Patterns: Data Locality](http://gameprogrammingpatterns.com/data-locality.html) \ No newline at end of file diff --git a/data-locality/pom.xml b/data-locality/pom.xml new file mode 100644 index 000000000000..f70e747e3ae6 --- /dev/null +++ b/data-locality/pom.xml @@ -0,0 +1,63 @@ + + + + 4.0.0 + + com.iluwatar + java-design-patterns + 1.21.0-SNAPSHOT + + data-locality + + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter-engine + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + \ No newline at end of file diff --git a/data-locality/src/main/java/com/iluwatar/data/locality/Application.java b/data-locality/src/main/java/com/iluwatar/data/locality/Application.java new file mode 100644 index 000000000000..9afac2c0dac8 --- /dev/null +++ b/data-locality/src/main/java/com/iluwatar/data/locality/Application.java @@ -0,0 +1,39 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.data.locality; + +import com.iluwatar.data.locality.game.GameEntity; + +/** + * Start Game Application + */ +public class Application { + /** + * Start + */ + public static void main(String[] args) { + GameEntity gameEntity = new GameEntity(10); + gameEntity.start(); + gameEntity.update(); + } +} diff --git a/data-locality/src/main/java/com/iluwatar/data/locality/game/GameEntity.java b/data-locality/src/main/java/com/iluwatar/data/locality/game/GameEntity.java new file mode 100644 index 000000000000..db8c724fe948 --- /dev/null +++ b/data-locality/src/main/java/com/iluwatar/data/locality/game/GameEntity.java @@ -0,0 +1,71 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.data.locality.game; + +import com.iluwatar.data.locality.game.component.manager.AiComponentManager; +import com.iluwatar.data.locality.game.component.manager.PhysicsComponentManager; +import com.iluwatar.data.locality.game.component.manager.RenderComponentManager; + +/** + * Manage Game Component + */ +public class GameEntity { + + private final AiComponentManager aiComponentManager; + private final PhysicsComponentManager physicsComponentManager; + private final RenderComponentManager renderComponentManager; + + /** + * Init + */ + public GameEntity(int numEntities) { + aiComponentManager = new AiComponentManager(numEntities); + physicsComponentManager = new PhysicsComponentManager(numEntities); + renderComponentManager = new RenderComponentManager(numEntities); + } + + /** + * start all component + */ + public void start() { + aiComponentManager.start(); + physicsComponentManager.start(); + renderComponentManager.start(); + } + + /** + * update all component + */ + public void update() { + // Process AI. + aiComponentManager.update(); + + // update physics. + physicsComponentManager.update(); + + // Draw to screen. + renderComponentManager.render(); + } + + +} diff --git a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/AiComponent.java b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/AiComponent.java new file mode 100644 index 000000000000..4c2dfadfd0de --- /dev/null +++ b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/AiComponent.java @@ -0,0 +1,37 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.data.locality.game.component; + +/** + * AI component + */ +public class AiComponent implements Component { + + /** + * Update ai component + */ + @Override + public void update() { + System.out.println("update :" + this.getClass().getSimpleName()); + } +} diff --git a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/Component.java b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/Component.java new file mode 100644 index 000000000000..56c3e82506a9 --- /dev/null +++ b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/Component.java @@ -0,0 +1,36 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.data.locality.game.component; + +/** + * Component + */ +public interface Component { + default void update() { + System.out.println("Default update"); + } + + default void render() { + System.out.println("Default render"); + } +} diff --git a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/PhysicsComponent.java b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/PhysicsComponent.java new file mode 100644 index 000000000000..372b3e48d2a0 --- /dev/null +++ b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/PhysicsComponent.java @@ -0,0 +1,36 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.data.locality.game.component; + +/** + * Physics Component implementation + */ +public class PhysicsComponent implements Component { + /** + * update + */ + @Override + public void update() { + System.out.println("update :" + this.getClass().getSimpleName()); + } +} diff --git a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/RenderComponent.java b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/RenderComponent.java new file mode 100644 index 000000000000..d10f72aedec3 --- /dev/null +++ b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/RenderComponent.java @@ -0,0 +1,37 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.data.locality.game.component; + +/** + * Render Component implementation + */ +public class RenderComponent implements Component { + + /** + * render + */ + @Override + public void render() { + System.out.println("render :" + this.getClass().getSimpleName()); + } +} diff --git a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/AiComponentManager.java b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/AiComponentManager.java new file mode 100644 index 000000000000..7fa6e40bda07 --- /dev/null +++ b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/AiComponentManager.java @@ -0,0 +1,63 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.data.locality.game.component.manager; + +import com.iluwatar.data.locality.game.component.AiComponent; +import com.iluwatar.data.locality.game.component.Component; + +/** + * Component Manager for AI + */ +public class AiComponentManager { + + private static final int MAX_ENTITES = 10000; + + private final int numEntities; + + public static final Component[] AI_COMPONENTS = new AiComponent[MAX_ENTITES]; + + public AiComponentManager(int numEntities) { + this.numEntities = numEntities; + } + + /** + * start AI component + */ + public void start() { + for (int i = 0; i < numEntities; i++) { + AI_COMPONENTS[i] = new AiComponent(); + } + } + + /** + * Update AI component + */ + public void update() { + // Process AI. + for (int i = 0; i < numEntities; i++) { + if (AI_COMPONENTS.length > i && AI_COMPONENTS[i] != null) { + AI_COMPONENTS[i].update(); + } + } + } +} diff --git a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/PhysicsComponentManager.java b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/PhysicsComponentManager.java new file mode 100644 index 000000000000..31cb0d2b37b5 --- /dev/null +++ b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/PhysicsComponentManager.java @@ -0,0 +1,64 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.data.locality.game.component.manager; + +import com.iluwatar.data.locality.game.component.Component; +import com.iluwatar.data.locality.game.component.PhysicsComponent; + +/** + * Component Manager for Physics component + */ +public class PhysicsComponentManager { + + private static final int MAX_ENTITIES = 10000; + + private final int numEntities; + + public static final Component[] PHYSICS_COMPONENTS = new PhysicsComponent[MAX_ENTITIES]; + + public PhysicsComponentManager(int numEntities) { + this.numEntities = numEntities; + } + + /** + * Start physics component + */ + public void start() { + for (int i = 0; i < numEntities; i++) { + PHYSICS_COMPONENTS[i] = new PhysicsComponent(); + } + } + + + /** + * Update physics component + */ + public void update() { + // Process physics. + for (int i = 0; i < numEntities; i++) { + if (PHYSICS_COMPONENTS.length > i && PHYSICS_COMPONENTS[i] != null) { + PHYSICS_COMPONENTS[i].update(); + } + } + } +} diff --git a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/RenderComponentManager.java b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/RenderComponentManager.java new file mode 100644 index 000000000000..dfbe6ea408a8 --- /dev/null +++ b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/RenderComponentManager.java @@ -0,0 +1,64 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.data.locality.game.component.manager; + +import com.iluwatar.data.locality.game.component.Component; +import com.iluwatar.data.locality.game.component.RenderComponent; + +/** + * Component Manager for Render component + */ +public class RenderComponentManager { + + private static final int MAX_ENTITIES = 10000; + + private final int numEntities; + + private static final Component[] RENDER_COMPONENTS = new RenderComponent[MAX_ENTITIES]; + + public RenderComponentManager(int numEntities) { + this.numEntities = numEntities; + } + + /** + * Start render component + */ + public void start() { + for (int i = 0; i < numEntities; i++) { + RENDER_COMPONENTS[i] = new RenderComponent(); + } + } + + + /** + * render component + */ + public void render() { + // Process Render. + for (int i = 0; i < numEntities; i++) { + if (RENDER_COMPONENTS.length > i && RENDER_COMPONENTS[i] != null) { + RENDER_COMPONENTS[i].render(); + } + } + } +} diff --git a/data-locality/src/test/java/com/iluwatar/data/locality/ApplicationTest.java b/data-locality/src/test/java/com/iluwatar/data/locality/ApplicationTest.java new file mode 100644 index 000000000000..69b1c7a07df5 --- /dev/null +++ b/data-locality/src/test/java/com/iluwatar/data/locality/ApplicationTest.java @@ -0,0 +1,40 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.data.locality; + + +import org.junit.jupiter.api.Test; + +/** + * Test Game Application + */ +class ApplicationTest { + + /** + * Test run + */ + @Test + void main() { + Application.main(new String[] {}); + } +} \ No newline at end of file From 10c97b781011ff3e96e2fb126e52e28e6a006813 Mon Sep 17 00:00:00 2001 From: ranjeet Date: Wed, 11 Sep 2019 21:29:44 +0530 Subject: [PATCH 2/4] Fixed review comments --- .../iluwatar/data/locality/Application.java | 20 ++++++++++++++++--- .../data/locality/game/GameEntity.java | 20 +++++++++++++++---- .../locality/game/component/AiComponent.java | 14 +++++++++++-- .../locality/game/component/Component.java | 11 ++++------ .../game/component/PhysicsComponent.java | 16 ++++++++++++--- .../game/component/RenderComponent.java | 14 +++++++++++-- .../component/manager/AiComponentManager.java | 17 ++++++++++------ .../manager/PhysicsComponentManager.java | 14 +++++++++---- .../manager/RenderComponentManager.java | 8 +++++++- 9 files changed, 102 insertions(+), 32 deletions(-) diff --git a/data-locality/src/main/java/com/iluwatar/data/locality/Application.java b/data-locality/src/main/java/com/iluwatar/data/locality/Application.java index 9afac2c0dac8..76f309bb0e97 100644 --- a/data-locality/src/main/java/com/iluwatar/data/locality/Application.java +++ b/data-locality/src/main/java/com/iluwatar/data/locality/Application.java @@ -23,16 +23,30 @@ package com.iluwatar.data.locality; import com.iluwatar.data.locality.game.GameEntity; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** - * Start Game Application + * Use the Data Locality pattern is when you have a performance problem. + * Take advantage of that to improve performance by increasing data locality — keeping data in + * contiguous memory in the order that you process it. + * + * Example: Game loop that processes a bunch of game entities. + * Those entities are decomposed into different domains  + * — AI, physics, and rendering — using the Component pattern. + * */ public class Application { + + private static final Logger LOGGER = LoggerFactory.getLogger(Application.class); + + private static final int NUM_ENTITIES = 5; /** - * Start + * Start game loop with each component have NUM_ENTITIES instance */ public static void main(String[] args) { - GameEntity gameEntity = new GameEntity(10); + LOGGER.info("Start Game Application using Data-Locality pattern"); + GameEntity gameEntity = new GameEntity(NUM_ENTITIES); gameEntity.start(); gameEntity.update(); } diff --git a/data-locality/src/main/java/com/iluwatar/data/locality/game/GameEntity.java b/data-locality/src/main/java/com/iluwatar/data/locality/game/GameEntity.java index db8c724fe948..219f7388cf7b 100644 --- a/data-locality/src/main/java/com/iluwatar/data/locality/game/GameEntity.java +++ b/data-locality/src/main/java/com/iluwatar/data/locality/game/GameEntity.java @@ -25,20 +25,31 @@ import com.iluwatar.data.locality.game.component.manager.AiComponentManager; import com.iluwatar.data.locality.game.component.manager.PhysicsComponentManager; import com.iluwatar.data.locality.game.component.manager.RenderComponentManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** - * Manage Game Component + * The game Entity maintains a big array of pointers . + * Each spin of the game loop, we need to run the following: + * + * Update the AI components . + * + * Update the physics components for them. + * + * Render them using their render components. */ public class GameEntity { + private static final Logger LOGGER = LoggerFactory.getLogger(GameEntity.class); private final AiComponentManager aiComponentManager; private final PhysicsComponentManager physicsComponentManager; private final RenderComponentManager renderComponentManager; /** - * Init + * Init components */ public GameEntity(int numEntities) { + LOGGER.info("Init Game with #Entity : {}", numEntities); aiComponentManager = new AiComponentManager(numEntities); physicsComponentManager = new PhysicsComponentManager(numEntities); renderComponentManager = new RenderComponentManager(numEntities); @@ -48,6 +59,7 @@ public GameEntity(int numEntities) { * start all component */ public void start() { + LOGGER.info("Start Game"); aiComponentManager.start(); physicsComponentManager.start(); renderComponentManager.start(); @@ -57,6 +69,7 @@ public void start() { * update all component */ public void update() { + LOGGER.info("Update Game Component"); // Process AI. aiComponentManager.update(); @@ -66,6 +79,5 @@ public void update() { // Draw to screen. renderComponentManager.render(); } - - + } diff --git a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/AiComponent.java b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/AiComponent.java index 4c2dfadfd0de..83a4a18c88d2 100644 --- a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/AiComponent.java +++ b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/AiComponent.java @@ -22,16 +22,26 @@ */ package com.iluwatar.data.locality.game.component; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** - * AI component + * Implementation of AI component for Game */ public class AiComponent implements Component { + + private static final Logger LOGGER = LoggerFactory.getLogger(AiComponent.class); /** * Update ai component */ @Override public void update() { - System.out.println("update :" + this.getClass().getSimpleName()); + LOGGER.info("update AI component"); + } + + @Override + public void render() { + } } diff --git a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/Component.java b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/Component.java index 56c3e82506a9..5a25aa46c5bf 100644 --- a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/Component.java +++ b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/Component.java @@ -23,14 +23,11 @@ package com.iluwatar.data.locality.game.component; /** - * Component + * Implement different Game component update and render process */ public interface Component { - default void update() { - System.out.println("Default update"); - } - default void render() { - System.out.println("Default render"); - } + void update(); + + void render(); } diff --git a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/PhysicsComponent.java b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/PhysicsComponent.java index 372b3e48d2a0..32eed1878dee 100644 --- a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/PhysicsComponent.java +++ b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/PhysicsComponent.java @@ -22,15 +22,25 @@ */ package com.iluwatar.data.locality.game.component; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** - * Physics Component implementation + * Implementation of Physics Component of Game */ public class PhysicsComponent implements Component { + + private static final Logger LOGGER = LoggerFactory.getLogger(PhysicsComponent.class); /** - * update + * update physics component of game */ @Override public void update() { - System.out.println("update :" + this.getClass().getSimpleName()); + LOGGER.info("Update physics component of game"); + } + + @Override + public void render() { + // do nothing } } diff --git a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/RenderComponent.java b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/RenderComponent.java index d10f72aedec3..910a57caeb6a 100644 --- a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/RenderComponent.java +++ b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/RenderComponent.java @@ -22,16 +22,26 @@ */ package com.iluwatar.data.locality.game.component; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** - * Render Component implementation + * Implementation of Render Component of Game */ public class RenderComponent implements Component { + private static final Logger LOGGER = LoggerFactory.getLogger(RenderComponent.class); + + @Override + public void update() { + // do nothing + } + /** * render */ @Override public void render() { - System.out.println("render :" + this.getClass().getSimpleName()); + LOGGER.info("Render Component"); } } diff --git a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/AiComponentManager.java b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/AiComponentManager.java index 7fa6e40bda07..7e98746b3179 100644 --- a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/AiComponentManager.java +++ b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/AiComponentManager.java @@ -24,36 +24,41 @@ import com.iluwatar.data.locality.game.component.AiComponent; import com.iluwatar.data.locality.game.component.Component; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** - * Component Manager for AI + * AI component manager for Game */ public class AiComponentManager { - private static final int MAX_ENTITES = 10000; + private static final Logger LOGGER = LoggerFactory.getLogger(AiComponentManager.class); + + private static final int MAX_ENTITIES = 10000; private final int numEntities; - public static final Component[] AI_COMPONENTS = new AiComponent[MAX_ENTITES]; + private static final Component[] AI_COMPONENTS = new AiComponent[MAX_ENTITIES]; public AiComponentManager(int numEntities) { this.numEntities = numEntities; } /** - * start AI component + * start AI component of Game */ public void start() { + LOGGER.info("Start AI Game Component"); for (int i = 0; i < numEntities; i++) { AI_COMPONENTS[i] = new AiComponent(); } } /** - * Update AI component + * Update AI component of Game */ public void update() { - // Process AI. + LOGGER.info("Update AI Game Component"); for (int i = 0; i < numEntities; i++) { if (AI_COMPONENTS.length > i && AI_COMPONENTS[i] != null) { AI_COMPONENTS[i].update(); diff --git a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/PhysicsComponentManager.java b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/PhysicsComponentManager.java index 31cb0d2b37b5..abb2c45816cc 100644 --- a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/PhysicsComponentManager.java +++ b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/PhysicsComponentManager.java @@ -24,26 +24,31 @@ import com.iluwatar.data.locality.game.component.Component; import com.iluwatar.data.locality.game.component.PhysicsComponent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** - * Component Manager for Physics component + * Physics component Manager for Game. */ public class PhysicsComponentManager { + private static final Logger LOGGER = LoggerFactory.getLogger(PhysicsComponentManager.class); + private static final int MAX_ENTITIES = 10000; private final int numEntities; - public static final Component[] PHYSICS_COMPONENTS = new PhysicsComponent[MAX_ENTITIES]; + private static final Component[] PHYSICS_COMPONENTS = new PhysicsComponent[MAX_ENTITIES]; public PhysicsComponentManager(int numEntities) { this.numEntities = numEntities; } /** - * Start physics component + * Start physics component of Game */ public void start() { + LOGGER.info("Start Physics Game Component "); for (int i = 0; i < numEntities; i++) { PHYSICS_COMPONENTS[i] = new PhysicsComponent(); } @@ -51,9 +56,10 @@ public void start() { /** - * Update physics component + * Update physics component of Game */ public void update() { + LOGGER.info("Update Physics Game Component "); // Process physics. for (int i = 0; i < numEntities; i++) { if (PHYSICS_COMPONENTS.length > i && PHYSICS_COMPONENTS[i] != null) { diff --git a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/RenderComponentManager.java b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/RenderComponentManager.java index dfbe6ea408a8..66f18bd32557 100644 --- a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/RenderComponentManager.java +++ b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/RenderComponentManager.java @@ -24,12 +24,16 @@ import com.iluwatar.data.locality.game.component.Component; import com.iluwatar.data.locality.game.component.RenderComponent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** - * Component Manager for Render component + * Render component manager for Game */ public class RenderComponentManager { + private static final Logger LOGGER = LoggerFactory.getLogger(RenderComponentManager.class); + private static final int MAX_ENTITIES = 10000; private final int numEntities; @@ -44,6 +48,7 @@ public RenderComponentManager(int numEntities) { * Start render component */ public void start() { + LOGGER.info("Start Render Game Component "); for (int i = 0; i < numEntities; i++) { RENDER_COMPONENTS[i] = new RenderComponent(); } @@ -54,6 +59,7 @@ public void start() { * render component */ public void render() { + LOGGER.info("Update Render Game Component "); // Process Render. for (int i = 0; i < numEntities; i++) { if (RENDER_COMPONENTS.length > i && RENDER_COMPONENTS[i] != null) { From 5075e6aea972be678bc2dd24ee2e55b3fcb7b33a Mon Sep 17 00:00:00 2001 From: ranjeet Date: Wed, 11 Sep 2019 21:30:13 +0530 Subject: [PATCH 3/4] updated pom --- data-locality/pom.xml | 19 ------------------- pom.xml | 1 + 2 files changed, 1 insertion(+), 19 deletions(-) diff --git a/data-locality/pom.xml b/data-locality/pom.xml index f70e747e3ae6..5d8ce21d8619 100644 --- a/data-locality/pom.xml +++ b/data-locality/pom.xml @@ -34,11 +34,6 @@ data-locality - - org.junit.jupiter - junit-jupiter-api - test - org.junit.jupiter junit-jupiter-engine @@ -46,18 +41,4 @@ - - - - org.apache.maven.plugins - maven-compiler-plugin - - 8 - 8 - - - - - - \ No newline at end of file diff --git a/pom.xml b/pom.xml index cb5dbbd1c343..ce773f61a243 100644 --- a/pom.xml +++ b/pom.xml @@ -172,6 +172,7 @@ commander typeobjectpattern bytecode + data-locality From 40f008c8523fea85676f9b0a7f7895cadc4017cf Mon Sep 17 00:00:00 2001 From: ranjeet Date: Wed, 11 Sep 2019 21:51:53 +0530 Subject: [PATCH 4/4] Fixed failed build --- data-locality/pom.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/data-locality/pom.xml b/data-locality/pom.xml index 5d8ce21d8619..bc6c8fc5160d 100644 --- a/data-locality/pom.xml +++ b/data-locality/pom.xml @@ -30,9 +30,10 @@ com.iluwatar java-design-patterns - 1.21.0-SNAPSHOT + 1.22.0-SNAPSHOT data-locality + org.junit.jupiter