SpaiR / imgui-java

JNI based binding for Dear ImGui
MIT License
587 stars 90 forks source link

Is it possible to change String argument inputs to Charsequence ones? #157

Open enesaltinkaya opened 1 year ago

enesaltinkaya commented 1 year ago

First off, thanks so much for your hard work.

I'm using ImGui.text() a lot. Library is expecting a String. If I have a text value that is changing I have to generate a new String since it is immutable. If I update imgui per frame, java allocates 4mb per second for strings.

https://user-images.githubusercontent.com/8594559/221267457-52ed2e52-ca97-4924-b039-98189f5d8764.mp4

I checked how Lwjgl does it. For setting Glfw window title for example.

public static void glfwSetWindowTitle(@NativeType("GLFWwindow *") long window, @NativeType("char const *") CharSequence title) {
    MemoryStack stack = stackGet(); int stackPointer = stack.getPointer();
    try {
        stack.nUTF8(title, true);
        long titleEncoded = stack.getPointerAddress();
        nglfwSetWindowTitle(window, titleEncoded);
    } finally {
        stack.setPointer(stackPointer);
    }
}

public int nUTF8(CharSequence text, boolean nullTerminated) {
    long target = nmalloc(POINTER_SIZE, memLengthUTF8(text, nullTerminated));
    return encodeUTF8Unsafe(text, nullTerminated, target);
}

// encodeUTF8Unsafe loops characters at CharSequence and writes them 1 byte at a time to allocated target.

So if we could somehow make the library accept CharSequences instead of Strings and copy those characters to native side without allocating anything that would be superb.

SpaiR commented 1 year ago

Good call! For that we need to implement API generator. It's currently in a WIP state, but I think it might work out in the future.