Git Product home page Git Product logo

Comments (3)

endison1986 avatar endison1986 commented on June 3, 2024

About this issue, I analysis source code, and found that the problem might be here.
dev.dominion.ecs.engine.system.ClassIndex.java

public IndexKey getIndexKey(Object[] objects) {
    int length = objects.length;
    boolean[] checkArray = new boolean[index + length + 1];
    int min = Integer.MAX_VALUE, max = 0;
    for (int i = 0; i < length; i++) {
        int value = getIndex(objects[i].getClass());
        value = value == 0 ? getIndexOrAddClass(objects[i].getClass()) : value;
        if (checkArray[value]) {
            throw new IllegalArgumentException("Duplicate object types are not allowed");
        }
        checkArray[value] = true;
        min = Math.min(value, min);
        max = Math.max(value, max);
    }
    return new IndexKey(checkArray, min, max, length);
}

under lines has concurrency problem.

int value = getIndex(objects[i].getClass());
value = value == 0 ? getIndexOrAddClass(objects[i].getClass()) : value;

If I createEntity in multiple threads, getIndex method will be called by multiple thread with result in zero. Also the getIndexOrAddClass method will be called multiple times. So I add a synchronized block to fix this issue temporary, Hope you have better way.

private final Object lock = new Object();
@SuppressWarnings("ForLoopReplaceableByForEach")
public IndexKey getIndexKey(Object[] objects) {
    int length = objects.length;
    boolean[] checkArray = new boolean[index + length + 1];
    int min = Integer.MAX_VALUE, max = 0;
    for (int i = 0; i < length; i++) {
        var componentClass = objects[i].getClass();
        var value = getIndex(componentClass);
        if (value == 0) {
            synchronized (lock) {
                value = getIndex(componentClass);
                if (value == 0) {
                    value = getIndexOrAddClass(componentClass);
                }
            }
        }
        if (checkArray[value]) {
            throw new IllegalArgumentException("Duplicate object types are not allowed");
        }
        checkArray[value] = true;
        min = Math.min(value, min);
        max = Math.max(value, max);
    }
    return new IndexKey(checkArray, min, max, length);
}

from dominion-ecs-java.

enricostara avatar enricostara commented on June 3, 2024

Thanks for reporting, I'll look into this issue after I fix issue #117 (still in progress)

from dominion-ecs-java.

enricostara avatar enricostara commented on June 3, 2024

Hi @endison1986, issue fixed by #122

from dominion-ecs-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.