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

perf: improve DSPLaunchDelegate debug adapter monitor performance #1080

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Supplier;

import org.eclipse.core.runtime.CoreException;
Expand Down Expand Up @@ -304,13 +304,14 @@ public void launch(DSPLaunchDelegateLaunchBuilder builderSrc) throws CoreExcepti
"Debug Adapter");
builder.launch.setAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT, initialCaptureOutput);

final var bytes = Collections.synchronizedList(new LinkedList<Byte>());
final var bytes = new ConcurrentLinkedQueue<Byte>();
inputStream = new InputStream() {
@Override
public int read() throws IOException {
while (debugAdapterProcess.isAlive()) {
if (!bytes.isEmpty()) {
return bytes.remove(0);
final Byte b = bytes.poll();
if (b != null) {
return b;
} else {
try {
Thread.sleep(50);
Expand All @@ -323,16 +324,14 @@ public int read() throws IOException {
return -1;
}
};
DSPLaunchDelegateLaunchBuilder finalBuilder = builder;
final var consoleEncoding = builder.launch.getAttribute(DebugPlugin.ATTR_CONSOLE_ENCODING);
final var consoleCharset = consoleEncoding == null //
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add to the PR description why are you adding this two calls to Charset instead of just passing consoleEncoding?

Copy link
Member Author

@sebthom sebthom Aug 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consoleEncoding might return null or what do you mean? I just moved the instruction out of the listener where it is unnecessarily reevaluated on each callback

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously the value returned by builder.launch.getAttribute(DebugPlugin.ATTR_CONSOLE_ENCODING) was passed to text.getBytes as is (just casting to NonNull).
Now if the value is null we map it to a default charset. Is that an intentional change? If so I think it would be worth putting in the commit and PR messages.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because getBytes(String) would first lookup the CharSet object. If we obtain the Charset before and directly pass it to getBytes() we save that lookup on each listener event.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation. To be equivalent as before, we should throw a NPE if builder.launch.getAttribute(DebugPlugin.ATTR_CONSOLE_ENCODING), otherwise we are changing the behavior, are we not?

I am fine changing the behaviour, but I think it should be added to the commit an PR messages.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think falling back to the default charset is more sensible than throwing an exception if the encoding is not overwritten in the launch config.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, me too.

Could you amend the commit message and add "If the console encoding was not specified in the launch config it now falls back to the JVM's default encoding instead of throwing an exception." there as well?

Then I am good

? Charset.defaultCharset()
: Charset.forName(consoleEncoding);
castNonNull(castNonNull(debugAdapterIProcess.getStreamsProxy()).getOutputStreamMonitor())
.addListener((text, monitor) -> {
try {
for (byte b : text.getBytes(castNonNull(
finalBuilder.launch.getAttribute(DebugPlugin.ATTR_CONSOLE_ENCODING)))) {
bytes.add(b);
}
} catch (IOException e) {
DSPPlugin.logError(e);
for (byte b : text.getBytes(consoleCharset)) {
bytes.add(b);
}
});
outputStream = new OutputStream() {
Expand Down
Loading