hypfvieh / dbus-java

Improved version of java DBus library provided by freedesktop.org (https://dbus.freedesktop.org/doc/dbus-java/)
https://hypfvieh.github.io/dbus-java/
MIT License
185 stars 73 forks source link

InputStreamMessageReader performance and logic issues #183

Closed ghost closed 2 years ago

ghost commented 2 years ago

While looking to obtain a bit more performance, I noticed a few things about InputStreamMessageReader that could be addressed. See patch file in comment below. These include some minor issues, in no particular order:

As well as some bugs both actual and theoretical:

Here's a screenshot of "D-Bus 4.1.1" (from main branch, no changes to the reader):

dbus-4 1 1

Here's a screenshot of "D-Bus 4.1.2" (patch file applied to main branch):

dbus-4 1 2

If I'm reading the the profiling information correctly, the changes reduce the total CPU usage by a piddly 800 ms every 200,000 ms for a very modest 0.4% overall improvement.

Attached are the .nps files that can be loaded into VisualVM 2.1.4:

ghost commented 2 years ago

Patch file for the changes:

hypfvieh commented 2 years ago

I've used some of your improvements and added some more.

Regarding the return of channel.read(), the javadoc says:

Returns: The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream

So checking for rv == -1 should work. Any other error on read() is signaled by an exception (IOException in most cases). However, using rv < 0 will also work, so I kept that change.

ghost commented 2 years ago

So checking for rv == -1 should work.

Here's what I found for JDK 8:

Presumably these C-level values are mapped to the following codes in IOStatus,java:

    @Native public static final int UNAVAILABLE = -2;      // Nothing available (non-blocking)
    @Native public static final int THROWN = -5;           // Exception thrown in JNI code

It looks like UNAVAILABLE is normalized to 0 and THROWN results in an exception, as you said.

Thanks for applying the patch!