Skip to content

lsp: improve performance by converting vim.NIL to nil during JSON deserialization #15109

Closed
@hrsh7th

Description

@hrsh7th
  • nvim --version: v0.6.0-dev+32-ge43dbfdd0
  • language server name/version: [email protected]
  • Operating system/version: Mac OS X BigSur
nvim -c ":checkhealth nvim lspconfig" health#nvim#check ======================================================================== ## Configuration - WARNING: $NVIM_TUI_ENABLE_CURSOR_SHAPE is ignored in Nvim 0.2+ - ADVICE: - Use the 'guicursor' option to configure cursor shape. :help |'guicursor'| - https://github.com/neovim/neovim/wiki/Following-HEAD#20170402

Performance

  • OK: Build type: Release

Remote Plugins

  • OK: Up to date

terminal

  • INFO: key_backspace (kbs) terminfo entry: key_backspace=^H
  • INFO: key_dc (kdch1) terminfo entry: key_dc=\E[3~
  • INFO: $TERM_PROGRAM='iTerm.app'
  • INFO: $COLORTERM='truecolor'

health#lspconfig#check

Checking language server protocol configuration

  • INFO: intelephense: configuration checked.
  • INFO: clangd: configuration checked.
  • INFO: purescriptls: configuration checked.
  • INFO: cssls: configuration checked.
  • ERROR: jdtls: The given command "vim.NIL/bin/java" is not executable.
  • INFO: html: configuration checked.
  • INFO: jsonls: configuration checked.
  • INFO: sumneko_lua: configuration checked.
  • INFO: gopls: configuration checked.
  • INFO: efm: configuration checked.
  • INFO: vimls: configuration checked.
  • INFO: tsserver: configuration checked.
  • INFO: rust_analyzer: configuration checked.

Steps to reproduce using nvim -u minimal_init.lua

  1. Apply the following patch to runtime/lua/vim/lsp/rpc.lua
patch
diff --git a/runtime/lua/vim/lsp/rpc.lua b/runtime/lua/vim/lsp/rpc.lua
index 4c5f02af9..bc4bda1bb 100644
--- a/runtime/lua/vim/lsp/rpc.lua
+++ b/runtime/lua/vim/lsp/rpc.lua
@@ -11,7 +11,9 @@ local validate, schedule, schedule_wrap = vim.validate, vim.schedule, vim.schedu
 --@param data (table) Data to encode
 --@returns (string) Encoded object
 local function json_encode(data)
+  local t = os.clock()
   local status, result = pcall(vim.fn.json_encode, data)
+  print(string.format("json_decode: %sms", 1000 * (os.clock()-t)))
   if status then
     return result
   else
@@ -68,7 +70,10 @@ end
 --@param v (any) Argument
 --@returns (any)
 local function convert_NIL(v)
-  return recursive_convert_NIL(v, {})
+  local t = os.clock()
+  local r = recursive_convert_NIL(v, {})
+  print(string.format("convert_NIL: %sms", 1000 * (os.clock()-t)))
+  return r
 end
 
 --@private
  1. Create sample project with npm
package.json
{
  "name": "sandbox-typescript",
  "main": "index.js",
  "devDependencies": {
    "@babel/cli": "^7.14.5",
    "@babel/core": "^7.14.6",
    "@babel/preset-env": "^7.14.5",
    "@types/date-fns": "^2.6.0",
    "@types/jest": "^26.0.24",
    "@types/lodash": "^4.14.171",
    "@types/node": "^16.3.2",
    "@types/react": "^17.0.14",
    "@types/react-dom": "^17.0.9",
    "babel-loader": "^8.2.2",
    "date-fns": "^2.22.1",
    "jest": "^27.0.6",
    "typescript": "^4.3.5"
  },
  "dependencies": {
    "@emotion/react": "^11.4.0",
    "@fortawesome/fontawesome-svg-core": "^1.2.35",
    "@fortawesome/free-solid-svg-icons": "^5.15.3",
    "@fortawesome/react-fontawesome": "^0.1.14",
    "@material-ui/core": "^4.12.1",
    "@material-ui/icons": "^4.11.2",
    "formik": "^2.2.9",
    "lodash": "^4.17.21",
    "react": "^17.0.2",
    "react-dom": "^17.0.2"
  }
}
  1. Create App.tsx under the above sample project's (src/App.tsx)
App.tsx
import React from 'react';

export const App = () => {
  return (
    <>
    </div>
  );
};
  1. Invoke v:lua.vim.lsp.omnifunc on the <|> | is cursor
  • This step needs a setup typescript-language-server

Actual behaviour

json_decode and convert_NIL will take the time near the 100ms.

screenshot スクリーンショット 2021-07-16 16 28 14

Expected behaviour

It will be good if these move to another thread or do more quickly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    channels-rpcchannels, RPC, msgpackenhancementfeature requestlspperformanceperformance, latency, cpu/memory usage

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions