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

Improve language server performance #1206

Merged
merged 4 commits into from
Apr 25, 2019
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- `apollo-language-server`
- Fix on-hover bugs introduced by replacing visitWithTypeInfo [#1196](https://github.com/apollographql/apollo-tooling/pull/1196)
- Add `gql` extension to the default `includes` configuration [#1176](https://github.com/apollographql/apollo-tooling/pull/1176)
- Simple perf improvements (debouncer + cache) [#1206](https://github.com/apollographql/apollo-tooling/pull/1206)
- `apollo-tools`
- <First `apollo-tools` related entry goes here>
- `vscode-apollo`
Expand Down
3,633 changes: 590 additions & 3,043 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"@types/jest": "24.0.11",
"@types/listr": "0.13.0",
"@types/lodash": "4.14.123",
"@types/lodash.debounce": "^4.0.6",
"@types/lodash.sortby": "4.7.6",
"@types/minimatch": "3.0.3",
"@types/nock": "9.3.1",
Expand Down
1 change: 1 addition & 0 deletions packages/apollo-language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"graphql": "^14.0.2",
"graphql-tag": "^2.10.1",
"lodash": "^4.17.11",
"lodash.debounce": "^4.0.8",
"minimatch": "^3.0.4",
"minimist": "^1.2.0",
"moment": "^2.22.2",
Expand Down
1 change: 0 additions & 1 deletion packages/apollo-language-server/src/languageProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
getTokenAtPosition,
getTypeInfo
} from "@apollographql/graphql-language-service-interface/dist/getAutocompleteSuggestions";

import { GraphQLWorkspace } from "./workspace";
import { DocumentUri } from "./project/base";

Expand Down
31 changes: 19 additions & 12 deletions packages/apollo-language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { QuickPickItem } from "vscode";
import { GraphQLWorkspace } from "./workspace";
import { GraphQLLanguageProvider } from "./languageProvider";
import { LanguageServerLoadingHandler } from "./loadingHandler";
import { debounceHandler } from "./utilities";

const connection = createConnection(ProposedFeatures.all);

Expand Down Expand Up @@ -117,12 +118,14 @@ const documents: TextDocuments = new TextDocuments();
// for open, change and close text document events
documents.listen(connection);

documents.onDidChangeContent(params => {
const project = workspace.projectForFile(params.document.uri);
if (!project) return;
documents.onDidChangeContent(
debounceHandler(params => {
const project = workspace.projectForFile(params.document.uri);
if (!project) return;

project.documentDidChange(params.document);
});
project.documentDidChange(params.document);
})
);

connection.onDidChangeWatchedFiles(params => {
for (const { uri, type } of params.changes) {
Expand Down Expand Up @@ -181,16 +184,20 @@ connection.onWorkspaceSymbol((params, token) =>
languageProvider.provideWorkspaceSymbol(params.query, token)
);

connection.onCompletion((params, token) =>
languageProvider.provideCompletionItems(
params.textDocument.uri,
params.position,
token
connection.onCompletion(
debounceHandler((params, token) =>
languageProvider.provideCompletionItems(
params.textDocument.uri,
params.position,
token
)
)
);

connection.onCodeLens((params, token) =>
languageProvider.provideCodeLenses(params.textDocument.uri, token)
connection.onCodeLens(
debounceHandler((params, token) =>
languageProvider.provideCodeLenses(params.textDocument.uri, token)
)
);

connection.onNotification("apollographql/reloadService", () =>
Expand Down
5 changes: 5 additions & 0 deletions packages/apollo-language-server/src/utilities/debouncer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import debounce from "lodash.debounce";

export function debounceHandler(handler: (...args: any[]) => any) {
return debounce(handler, 400, { trailing: true });
}
1 change: 1 addition & 0 deletions packages/apollo-language-server/src/utilities/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./debouncer";
7 changes: 7 additions & 0 deletions packages/apollo-language-server/src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class GraphQLWorkspace {
private _onDecorations?: NotificationHandler<any>;
private _onSchemaTags?: NotificationHandler<[ServiceID, SchemaTag[]]>;
private _onConfigFilesFound?: NotificationHandler<ApolloConfig[]>;
private _projectForFileCache: Map<string, GraphQLProject> = new Map();

private projectsByFolderUri: Map<string, GraphQLProject[]> = new Map();

Expand Down Expand Up @@ -225,9 +226,15 @@ export class GraphQLWorkspace {
}

projectForFile(uri: DocumentUri): GraphQLProject | undefined {
const cachedResult = this._projectForFileCache.get(uri);
if (cachedResult) {
return cachedResult;
}

for (const projects of this.projectsByFolderUri.values()) {
const project = projects.find(project => project.includesFile(uri));
if (project) {
this._projectForFileCache.set(uri, project);
return project;
}
}
Expand Down