Skip to content

Commit

Permalink
fix: High CPU usage while going to the symbol definition
Browse files Browse the repository at this point in the history
  • Loading branch information
slavek-kucera authored Mar 15, 2023
1 parent c52e4f7 commit ef737c8
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions clients/vscode-hlasmplugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#### Fixed
- Operands no longer classified as remarks when instruction or macro is not recognized
- New or changed files are parsed only when they are opened in the editor
- High CPU usage while going to the symbol definition

## [1.7.0](https://github.com/eclipse/che-che4z-lsp-for-hlasm/compare/1.6.0...1.7.0) (2023-03-08)

Expand Down
4 changes: 3 additions & 1 deletion clients/vscode-hlasmplugin/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { blockCommentCommand, CommentOption, lineCommentCommand } from './commen
import { HLASMCodeActionsProvider } from './hlasmCodeActionsProvider';
import { hlasmplugin_folder } from './constants';
import { ConfigurationsHandler } from './configurationsHandler';
import { getLanguageClientMiddleware } from './languageClientMiddleware';

export const EXTENSION_ID = "broadcommfd.hlasm-language-support";

Expand Down Expand Up @@ -80,7 +81,8 @@ export async function activate(context: vscode.ExtensionContext) {
vscode.workspace.createFileSystemWatcher(hlasmplugin_folder + '/*.json'),
]
},
errorHandler: clientErrorHandler
errorHandler: clientErrorHandler,
middleware: getLanguageClientMiddleware(),
};


Expand Down
73 changes: 73 additions & 0 deletions clients/vscode-hlasmplugin/src/languageClientMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2023 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* 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:
* Broadcom, Inc. - initial API and implementation
*/

import { Middleware } from 'vscode-languageclient';

export function getLanguageClientMiddleware(): Middleware {
// VSCode generates flood of didOpen/didClose event when trying to go to a symbol definition using mouse (CTRL+point).
// This behavior is documented (and apprently work on) but it causes meaningless activity in the language server.

// The purpose of the middleware is to delay sending the didOpen event by a fraction of a second
// and attempt to pair it with didClose generated almost immediatelly after.
// In that case, we drop both events.

const pendingOpens = new Map<string, {
send: () => void,
clearTimeout: () => void,
}>();
const timeout = 50;

return {
didOpen: (data, next) => {
const uri = data.uri.toString();

const didOpenEvent = pendingOpens.get(uri);
if (didOpenEvent) {
console.error('Double open detected for', uri);
pendingOpens.delete(uri);
didOpenEvent.clearTimeout();
didOpenEvent.send();
}

const timerId = setTimeout(() => {
pendingOpens.delete(uri);
next(data);
}, timeout);
pendingOpens.set(uri, {
send: () => { next(data); },
clearTimeout: () => { clearTimeout(timerId); },
});
},
didChange: (data, next) => {
const uri = data.document.uri.toString();
const didOpenEvent = pendingOpens.get(uri);
if (didOpenEvent) {
pendingOpens.delete(uri);
didOpenEvent.clearTimeout();
didOpenEvent.send();
}
next(data);
},
didClose: (data, next) => {
const uri = data.uri.toString();
const didOpenEvent = pendingOpens.get(uri);
if (didOpenEvent) {
pendingOpens.delete(uri);
didOpenEvent.clearTimeout();
}
else
next(data);
},
};
}

0 comments on commit ef737c8

Please sign in to comment.