Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
enesaltinkaya opened this issue Feb 24, 2023 · 1 comment
Labels
feat New feature or request

Comments

@enesaltinkaya
Copy link

enesaltinkaya commented Feb 24, 2023

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.

2023-02-24.21-50-51.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
Copy link
Owner

SpaiR commented Feb 27, 2023

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.

@SpaiR SpaiR added feat New feature or request api labels Feb 27, 2023
@SpaiR SpaiR removed the api label Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants