Skip to content

Commit

Permalink
perf: reuse empty arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Aug 24, 2024
1 parent 69b08ed commit c81f927
Show file tree
Hide file tree
Showing 21 changed files with 108 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import org.eclipse.lsp4j.debug.StackFrame;

public class DSPStackFrame extends DSPDebugElement implements IStackFrame {

private static final IRegisterGroup[] NO_REGISTER_GROUPS = new IRegisterGroup[0];

private final DSPThread thread;
private StackFrame stackFrame;
private final int depth;
Expand Down Expand Up @@ -157,7 +160,7 @@ public DSPThread getThread() {

@Override
public IRegisterGroup[] getRegisterGroups() throws DebugException {
return new IRegisterGroup[0];
return NO_REGISTER_GROUPS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
import org.eclipse.lsp4j.debug.Thread;

public class DSPThread extends DSPDebugElement implements IThread {

private static final IStackFrame[] NO_STACK_FRAMES = new IStackFrame[0];
private static final IBreakpoint[] NO_BREAKPOINTS = new IBreakpoint[0];

private final Integer id;
/**
* The name may not be known, if it is requested we will ask for it from the
Expand Down Expand Up @@ -232,7 +236,7 @@ public boolean hasStackFrames() throws DebugException {
@Override
public IStackFrame[] getStackFrames() throws DebugException {
if (!isSuspended()) {
return new IStackFrame[0];
return NO_STACK_FRAMES;
}
if (!refreshFrames.getAndSet(false)) {
synchronized (frames) {
Expand Down Expand Up @@ -263,12 +267,12 @@ public IStackFrame[] getStackFrames() throws DebugException {
return future.get();
} catch (RuntimeException | ExecutionException e) {
if (isTerminated()) {
return new DSPStackFrame[0];
return NO_STACK_FRAMES;
}
throw newTargetRequestFailedException(e.getMessage(), e);
} catch (InterruptedException e) {
java.lang.Thread.currentThread().interrupt();
return new DSPStackFrame[0];
return NO_STACK_FRAMES;
}
}

Expand All @@ -291,7 +295,7 @@ public String getName() {
@Override
public IBreakpoint[] getBreakpoints() {
// TODO update breakpoints from stopped messages from server
return new IBreakpoint[0];
return NO_BREAKPOINTS;
}

public Integer getId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

public final class DSPValue extends DSPDebugElement implements IValue {

private static final IVariable[] NO_VARIABLES = new IVariable[0];

private final @Nullable DSPVariable modelVariable;
private final Integer variablesReference;
private final String value;
Expand All @@ -41,7 +43,7 @@ public DSPValue(DSPDebugTarget debugger, Integer variablesReference, String valu
@Override
public IVariable @Nullable [] getVariables() throws DebugException {
if (!hasVariables()) {
return new IVariable[0];
return NO_VARIABLES;
}
if (cachedVariables == null) {
final var arguments = new VariablesArguments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*******************************************************************************/
package org.eclipse.lsp4e.debug.presentation;

import static org.eclipse.lsp4e.internal.ArrayUtil.NO_STRINGS;
import static org.eclipse.lsp4e.internal.NullSafetyHelper.castNonNull;

import org.eclipse.core.runtime.Adapters;
Expand All @@ -21,6 +22,7 @@
import org.eclipse.lsp4e.debug.debugmodel.DSPDebugTarget;
import org.eclipse.lsp4e.debug.debugmodel.DSPStackFrame;
import org.eclipse.lsp4e.debug.debugmodel.DSPValue;
import org.eclipse.lsp4e.internal.ArrayUtil;
import org.eclipse.lsp4j.debug.EvaluateArguments;
import org.eclipse.lsp4j.debug.EvaluateResponse;

Expand Down Expand Up @@ -63,7 +65,7 @@ public String getExpressionText() {

@Override
public String[] getErrorMessages() {
return new String[0];
return NO_STRINGS;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*******************************************************************************/
package org.eclipse.lsp4e.debug.sourcelookup;

import static org.eclipse.lsp4e.internal.ArrayUtil.NO_OBJECTS;

import java.nio.file.Paths;

import org.eclipse.core.runtime.CoreException;
Expand All @@ -23,7 +25,7 @@ public Object[] findSourceElements(String name) throws CoreException {
if (name != null && Paths.get(name).isAbsolute()) {
return new Object[] { name };
}
return new Object[0];
return NO_OBJECTS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
public class LspJavaQuickAssistProcessor extends LSPCodeActionQuickAssistProcessor implements IQuickAssistProcessor {

private static final int RELEVANCE = 100;
private static final IJavaCompletionProposal[] NO_JAVA_COMPLETION_PROPOSALS = new IJavaCompletionProposal[0];

private IQuickAssistInvocationContext getContext(IInvocationContext context) {
return new IQuickAssistInvocationContext() {
Expand Down Expand Up @@ -63,7 +64,7 @@ public boolean hasAssists(@NonNullByDefault({}) IInvocationContext context) thro

ICompletionProposal[] proposals = computeQuickAssistProposals(getContext(context));
if (proposals == null)
return new IJavaCompletionProposal[0];
return NO_JAVA_COMPLETION_PROPOSALS;

final var javaProposals = new IJavaCompletionProposal[proposals.length];
for (int i = 0; i < proposals.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*******************************************************************************/
package org.eclipse.lsp4e;

import static org.eclipse.lsp4e.internal.ArrayUtil.NO_BYTES;
import static org.eclipse.lsp4e.internal.NullSafetyHelper.castNonNull;

import java.io.IOException;
Expand Down Expand Up @@ -60,11 +61,10 @@ public class LaunchConfigurationStreamProvider implements StreamConnectionProvid
protected static class StreamProxyInputStream extends InputStream implements IStreamListener {

private static final int EOF = -1;
private static final byte[] NO_DATA = new byte[0];

private final ConcurrentLinkedQueue<byte[]> queue = new ConcurrentLinkedQueue<>();
private final IProcess process;
private byte[] currentBuffer = NO_DATA;
private byte[] currentBuffer = NO_BYTES;
private int currentBufferPos = 0;

public StreamProxyInputStream(IProcess process) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.lsp4e.LanguageServerWrapper;
import org.eclipse.lsp4e.LanguageServers;
import org.eclipse.lsp4e.LanguageServers.LanguageServerDocumentExecutor;
import org.eclipse.lsp4e.internal.ArrayUtil;
import org.eclipse.lsp4e.internal.Pair;
import org.eclipse.lsp4e.ui.Messages;
import org.eclipse.lsp4e.ui.views.HierarchyViewInput;
Expand Down Expand Up @@ -59,7 +60,7 @@ public Object[] getChildren(final Object parentElement) {
if (parentElement instanceof CallHierarchyViewTreeNode treeNode) {
return findCallers(treeNode);
} else {
return new Object[0];
return ArrayUtil.NO_OBJECTS;
}
}

Expand Down
30 changes: 30 additions & 0 deletions org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/ArrayUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*******************************************************************************
* Copyright (c) 2024 Sebastian Thomschke and others.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Sebastian Thomschke - initial implementation
*******************************************************************************/
package org.eclipse.lsp4e.internal;

public class ArrayUtil {

/** reusable empty byte array */
public static final byte[] NO_BYTES = new byte[0];

/** reusable empty char array */
public static final char[] NO_CHARS = new char[0];

/** reusable empty {@link Object} array */
public static final Object[] NO_OBJECTS = new Object[0];

/** reusable empty {@link String} array */
public static final String[] NO_STRINGS = new String[0];

private ArrayUtil() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

public class CommandMarkerResolution extends WorkbenchMarkerResolution implements IMarkerResolution {

private static final IMarker[] NO_MARKERS = new IMarker[0];

private final Command command;

public CommandMarkerResolution(Command command) {
Expand Down Expand Up @@ -80,7 +82,7 @@ public String getDescription() {

@Override
public IMarker[] findOtherMarkers(IMarker[] markers) {
return new IMarker[0];
return NO_MARKERS;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@

public class LSPCodeActionMarkerResolution implements IMarkerResolutionGenerator2 {

private static final IMarkerResolution[] NO_MARKER_RESOLUTIONS = new IMarkerResolution[0];

private static final String LSP_REMEDIATION = "lspCodeActions"; //$NON-NLS-1$

private static final IMarkerResolution2 COMPUTING = new IMarkerResolution2() {
Expand Down Expand Up @@ -106,15 +108,15 @@ public IMarkerResolution[] getResolutions(IMarker marker) {
} catch (InterruptedException e) {
LanguageServerPlugin.logError(e);
Thread.currentThread().interrupt();
return new IMarkerResolution[0];
return NO_MARKER_RESOLUTIONS;
} catch (Exception e) {
LanguageServerPlugin.logError(e);
return new IMarkerResolution[0];
return NO_MARKER_RESOLUTIONS;
}
if (att == COMPUTING) {
return new IMarkerResolution[] { COMPUTING };
} else if (att == null) {
return new IMarkerResolution[0];
return NO_MARKER_RESOLUTIONS;
}
return ((List<Either<Command, CodeAction>>) att).stream().filter(LSPCodeActionMarkerResolution::canPerform)
.map(command -> command.map(CommandMarkerResolution::new, CodeActionMarkerResolution::new))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*******************************************************************************/
package org.eclipse.lsp4e.operations.completion;

import static org.eclipse.lsp4e.internal.ArrayUtil.NO_CHARS;
import static org.eclipse.lsp4e.internal.NullSafetyHelper.castNonNull;

import java.net.URI;
Expand Down Expand Up @@ -68,17 +69,19 @@

public class LSContentAssistProcessor implements IContentAssistProcessor {

private static final ICompletionProposal[] NO_COMPLETION_PROPOSALS = new ICompletionProposal[0];
private static final long TRIGGERS_TIMEOUT = 50;
private static final long CONTEXT_INFORMATION_TIMEOUT = 1000;

private @Nullable IDocument currentDocument;
private @Nullable String errorMessage;
private final boolean errorAsCompletionItem;
private @Nullable CompletableFuture<List<Void>> completionLanguageServersFuture;
private final Object completionTriggerCharsSemaphore = new Object();
private char[] completionTriggerChars = new char[0];
private char[] completionTriggerChars = NO_CHARS;
private @Nullable CompletableFuture<List<Void>> contextInformationLanguageServersFuture;
private final Object contextTriggerCharsSemaphore = new Object();
private char[] contextTriggerChars = new char[0];
private char[] contextTriggerChars = NO_CHARS;
private final boolean incompleteAsCompletionItem;

/**
Expand Down Expand Up @@ -107,12 +110,12 @@ public LSContentAssistProcessor(boolean errorAsCompletionItem, boolean incomplet
public ICompletionProposal @Nullable [] computeCompletionProposals(ITextViewer viewer, int offset) {
IDocument document = viewer.getDocument();
if (document == null) {
return new LSCompletionProposal[0];
return NO_COMPLETION_PROPOSALS;
}

URI uri = LSPEclipseUtils.toUri(document);
if (uri == null) {
return new LSCompletionProposal[0];
return NO_COMPLETION_PROPOSALS;
}

initiateLanguageServers(document);
Expand Down Expand Up @@ -201,7 +204,7 @@ private ICompletionProposal[] createErrorProposal(int offset, Exception ex) {
return new ICompletionProposal[] {
new CompletionProposal("", offset, 0, 0, null, Messages.completionError, null, ex.getMessage()) }; //$NON-NLS-1$
} else {
return new ICompletionProposal[0];
return NO_COMPLETION_PROPOSALS;
}
}

Expand All @@ -214,7 +217,7 @@ private ICompletionProposal[] createIncompleProposal(int offset, boolean incompl
return new ICompletionProposal[] { new CompletionProposal("", offset, 0, 0, null, //$NON-NLS-1$
Messages.completionIncomplete, null, Messages.continueIncomplete) };
}
return new ICompletionProposal[0];
return NO_COMPLETION_PROPOSALS;
}

private void initiateLanguageServers(IDocument document) {
Expand All @@ -234,8 +237,8 @@ private void initiateLanguageServers(IDocument document) {
// nothing
}
}
this.completionTriggerChars = new char[0];
this.contextTriggerChars = new char[0];
this.completionTriggerChars = NO_CHARS;
this.contextTriggerChars = NO_CHARS;

this.completionLanguageServersFuture = LanguageServers.forDocument(document)
.withFilter(capabilities -> capabilities.getCompletionProvider() != null) //
Expand Down Expand Up @@ -368,7 +371,7 @@ private void getFuture(@Nullable CompletableFuture<List<Void>> future) {
private static char[] mergeTriggers(char @Nullable [] initialArray,
@Nullable Collection<String> additionalTriggers) {
if (initialArray == null) {
initialArray = new char[0];
initialArray = NO_CHARS;
}
if (additionalTriggers == null) {
additionalTriggers = Collections.emptySet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
public class LSPFoldingReconcilingStrategy
implements IReconcilingStrategy, IReconcilingStrategyExtension, IProjectionListener, ITextViewerLifecycle {

private static final Annotation[] NO_ANNOTATIONS = new Annotation[0];

private @Nullable IDocument document;
private @Nullable ProjectionAnnotationModel projectionAnnotationModel;
private @Nullable ProjectionViewer viewer;
Expand Down Expand Up @@ -181,7 +183,7 @@ private void applyFolding(@Nullable List<FoldingRange> ranges) {
// send the calculated updates to the annotations to the
// annotation model
theProjectionAnnotationModel.modifyAnnotations(deletions.toArray(Annotation[]::new), additions,
new Annotation[0]);
NO_ANNOTATIONS);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public LSSearchResult(LSSearchQuery query) {
super(query);
}

private static final Match[] EMPTY_ARR= new Match[0];
private static final Match[] NO_MATCHES = new Match[0];
private final Set<Object> nonFileElements = ConcurrentHashMap.newKeySet();

@Override
Expand Down Expand Up @@ -68,7 +68,7 @@ public Match[] computeContainedMatches(AbstractTextSearchResult result, IEditorP
} else if (ei instanceof IURIEditorInput uriInput) {
return getMatches(uriInput.getURI());
}
return EMPTY_ARR;
return NO_MATCHES;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.lsp4e.LSPEclipseUtils;
import org.eclipse.lsp4e.internal.ArrayUtil;
import org.eclipse.lsp4e.operations.references.FileAndURIMatchLabelProvider.FileAndURIMatchBaseLabelProvider;
import org.eclipse.search.internal.ui.text.DecoratingFileSearchLabelProvider;
import org.eclipse.search.internal.ui.text.FileMatch;
Expand Down Expand Up @@ -81,7 +82,7 @@ protected void elementsChanged(Object[] objects) {

@Override
protected void clear() {
getViewer().setInput(new Object[0]);
getViewer().setInput(ArrayUtil.NO_OBJECTS);
}

@Override
Expand Down
Loading

0 comments on commit c81f927

Please sign in to comment.