Data Locality pattern #559 (#889)

* Data Locality pattern #559

* Fixed review comments

* updated pom

* Fixed failed build
This commit is contained in:
Ranjeet
2019-09-30 20:49:08 +05:30
committed by Ilkka Seppälä
parent 5f39f7fbf7
commit 6b297b701f
13 changed files with 633 additions and 0 deletions

View File

@ -0,0 +1,53 @@
/**
* 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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Use the Data Locality pattern is when you have a performance problem.
* Take advantage of that to improve performance by increasing data localitykeeping 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 renderingusing the Component pattern.
*
*/
public class Application {
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
private static final int NUM_ENTITIES = 5;
/**
* Start game loop with each component have NUM_ENTITIES instance
*/
public static void main(String[] args) {
LOGGER.info("Start Game Application using Data-Locality pattern");
GameEntity gameEntity = new GameEntity(NUM_ENTITIES);
gameEntity.start();
gameEntity.update();
}
}

View File

@ -0,0 +1,83 @@
/**
* 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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 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 components
*/
public GameEntity(int numEntities) {
LOGGER.info("Init Game with #Entity : {}", numEntities);
aiComponentManager = new AiComponentManager(numEntities);
physicsComponentManager = new PhysicsComponentManager(numEntities);
renderComponentManager = new RenderComponentManager(numEntities);
}
/**
* start all component
*/
public void start() {
LOGGER.info("Start Game");
aiComponentManager.start();
physicsComponentManager.start();
renderComponentManager.start();
}
/**
* update all component
*/
public void update() {
LOGGER.info("Update Game Component");
// Process AI.
aiComponentManager.update();
// update physics.
physicsComponentManager.update();
// Draw to screen.
renderComponentManager.render();
}
}

View File

@ -0,0 +1,47 @@
/**
* 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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 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() {
LOGGER.info("update AI component");
}
@Override
public void render() {
}
}

View File

@ -0,0 +1,33 @@
/**
* 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;
/**
* Implement different Game component update and render process
*/
public interface Component {
void update();
void render();
}

View File

@ -0,0 +1,46 @@
/**
* 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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Implementation of Physics Component of Game
*/
public class PhysicsComponent implements Component {
private static final Logger LOGGER = LoggerFactory.getLogger(PhysicsComponent.class);
/**
* update physics component of game
*/
@Override
public void update() {
LOGGER.info("Update physics component of game");
}
@Override
public void render() {
// do nothing
}
}

View File

@ -0,0 +1,47 @@
/**
* 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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 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() {
LOGGER.info("Render Component");
}
}

View File

@ -0,0 +1,68 @@
/**
* 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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* AI component manager for Game
*/
public class AiComponentManager {
private static final Logger LOGGER = LoggerFactory.getLogger(AiComponentManager.class);
private static final int MAX_ENTITIES = 10000;
private final int numEntities;
private static final Component[] AI_COMPONENTS = new AiComponent[MAX_ENTITIES];
public AiComponentManager(int numEntities) {
this.numEntities = numEntities;
}
/**
* 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 of Game
*/
public void update() {
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();
}
}
}
}

View File

@ -0,0 +1,70 @@
/**
* 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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 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;
private static final Component[] PHYSICS_COMPONENTS = new PhysicsComponent[MAX_ENTITIES];
public PhysicsComponentManager(int numEntities) {
this.numEntities = numEntities;
}
/**
* 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();
}
}
/**
* 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) {
PHYSICS_COMPONENTS[i].update();
}
}
}
}

View File

@ -0,0 +1,70 @@
/**
* 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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 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;
private static final Component[] RENDER_COMPONENTS = new RenderComponent[MAX_ENTITIES];
public RenderComponentManager(int numEntities) {
this.numEntities = 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();
}
}
/**
* 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) {
RENDER_COMPONENTS[i].render();
}
}
}
}

View File

@ -0,0 +1,40 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
* <p>
* 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:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* 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[] {});
}
}