Reducing the Number of RPC Calls
As Denops employs RPC to interact with Vim, the volume of RPC calls
significantly influences the plugin's performance. In this section, we aim to
enhance performance by reducing the number of RPC calls using the batch module
from @denops/std. Let's revise the main.ts file as follows:
import type { Entrypoint } from "@denops/std";
import { batch, collect } from "@denops/std/batch";
import * as buffer from "@denops/std/buffer";
import * as fn from "@denops/std/function";
import * as op from "@denops/std/option";
import { Maze } from "maze_generator";
export const main: Entrypoint = (denops) => {
denops.dispatcher = {
async maze() {
const { bufnr, winnr } = await buffer.open(denops, "maze://");
const [winWidth, winHeight] = await collect(denops, (denops) => [
fn.winwidth(denops, winnr),
fn.winheight(denops, winnr),
]);
const maze = new Maze({
xSize: winWidth / 3,
ySize: winHeight / 3,
}).generate();
const content = maze.getString();
await batch(denops, async (denops) => {
await buffer.replace(denops, bufnr, content.split(/\r?\n/g));
await buffer.concrete(denops, bufnr);
await op.bufhidden.setLocal(denops, "wipe");
await op.modifiable.setLocal(denops, false);
});
},
};
};
In this code, we use the collect function to gather window size values and the
batch function to execute multiple commands in a single RPC. This optimization
significantly reduces the number of RPC calls, thereby improving the plugin's
performance.
The collect function is designed for collecting multiple values in a single
RPC, offering the following features:
- Execution of
denops.callordenops.evalwithin thecollectis delayed and executed in a single RPC with the results. - The result of
denops.callordenops.evalin thecollectis always falsy, indicating that branching (if, switch, etc.) is not allowed. - Execution of
denops.redrawordenops.cmdin thecollectis not allowed. - Execution of
batchorcollectin thecollectis not allowed, indicating that nesting is not allowed.
In short, only the following operations are allowed in the collect:
denops.callordenops.evalthat returns a value.- Functions in the
functionmodule that return a value. - Functions in the
optionmodule that return a value. - Functions in the
variablemodule that return a value.
The batch function is designed for executing multiple commands in a single
RPC, offering the following features:
- Execution of
denops.call,denops.cmd, ordenops.evalin thebatchis delayed and executed in a single RPC without the results. - The result of
denops.callordenops.evalin thebatchis always falsy, indicating that branching (if, switch, etc.) is not allowed. - Execution of
denops.redrawis accumulated and only executed once at the end of thebatch. - Execution of
batchin thebatchis allowed, indicating that nesting is allowed. - Execution of
collectin thebatchis not allowed, indicating that nesting is not allowed.
In short, only the following operations are allowed in the batch:
denops.call,denops.cmd, ordenops.eval(without the results).- Functions in the
functionmodule (without the results). - Functions in the
optionmodule (without the results). - Functions in the
variablemodule (without the results). - Functions in other modules that do not call
collectinternally.
In the previous code, the number of RPC calls was more than 7, but after using
batch and collect, the number of RPC calls is reduced to 3. Although this is
a small plugin, the performance improvement may not be noticeable. However, in a
larger plugin, the performance improvement will be significant.
Restart Vim, rerun the :Maze command, and confirm that the plugin works
properly with batch and collect.
Next Steps
In the next step, read API references or real-world plugins