Skip to content

Commit 7fea3c5

Browse files
committed
speedup semantic token, if installed use orjson package to speedup json de/encoding
1 parent 0615fe4 commit 7fea3c5

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
"configuration": "./language-configuration.json"
138138
}
139139
],
140-
"grammars": [
140+
"#grammars": [
141141
{
142142
"language": "robotframework",
143143
"scopeName": "source.robotframework",

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ robotremoteserver = "^1.1"
2828
Cython = "^0.29.24"
2929
robotframework-robocop = "^1.7.1"
3030
robotframework-tidy = "^1.5.1"
31+
orjson = "^3.6.3"
3132

3233

3334
[tool.poetry-dynamic-versioning]
34-
enable = true
35+
enable = false
3536
vcs = "git"
3637
style = "semver"
3738

robotcode/jsonrpc2/protocol.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,46 @@ class JsonRPCErrors:
7575
PROTOCOL_VERSION = "2.0"
7676

7777

78+
def get_loads() -> Callable[..., Any]:
79+
80+
try:
81+
import orjson
82+
83+
return cast(Callable[..., Any], orjson.loads)
84+
85+
except ImportError:
86+
pass
87+
88+
import json
89+
90+
return json.loads
91+
92+
93+
def get_dumps() -> Callable[..., Any]:
94+
try:
95+
import orjson
96+
97+
def orjson_dumps(__obj: Any, *, default: Any, indent: Optional[bool] = False) -> str:
98+
# orjson.dumps returns bytes, to match standard json.dumps we need to decode
99+
return orjson.dumps(__obj, default=default, option=orjson.OPT_INDENT_2 if indent else 0).decode()
100+
101+
return orjson_dumps
102+
103+
except ImportError:
104+
pass
105+
106+
import json
107+
108+
return json.dumps
109+
110+
78111
class JsonRPCMessage(BaseModel):
79112
jsonrpc: str = Field(PROTOCOL_VERSION, const=True)
80113

114+
class Config:
115+
json_loads = get_loads()
116+
json_dumps = get_dumps()
117+
81118

82119
class JsonRPCNotification(JsonRPCMessage):
83120
method: str = Field(...)

robotcode/language_server/robotframework/parts/semantic_tokens.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import asyncio
4+
import itertools
45
import operator
56
import re
67
from enum import Enum
@@ -96,6 +97,7 @@ class RobotSemanticTokenProtocolPart(RobotLanguageServerProtocolPart):
9697
def __init__(self, parent: RobotLanguageServerProtocol) -> None:
9798
super().__init__(parent)
9899
parent.semantic_tokens.token_types += [e for e in RobotSemTokenTypes]
100+
99101
parent.semantic_tokens.collect_full.add(self.collect_full)
100102
parent.semantic_tokens.collect_range.add(self.collect_range)
101103
# parent.semantic_tokens.collect_full_delta.add(self.collect_full_delta)
@@ -188,7 +190,7 @@ def generate_sem_sub_tokens(
188190
else:
189191
yield SemTokenInfo.from_token(token, sem_info[0], sem_info[1])
190192

191-
if token.type == RobotToken.ARGUMENT and "\\" in token.value:
193+
elif token.type == RobotToken.ARGUMENT and "\\" in token.value:
192194
if col_offset is None:
193195
col_offset = token.col_offset
194196
if length is None:
@@ -241,19 +243,10 @@ def collect(
241243
last_line = 0
242244
last_col = 0
243245

244-
start = True
245-
246-
for robot_token in tokens:
247-
if range is not None:
248-
if start:
249-
if not token_in_range(robot_token, range):
250-
continue
251-
else:
252-
start = False
253-
else:
254-
if not token_in_range(robot_token, range):
255-
break
256-
246+
for robot_token in itertools.takewhile(
247+
lambda t: range is not None and token_in_range(t, range),
248+
itertools.dropwhile(lambda t: range is None or not token_in_range(t, range), tokens),
249+
):
257250
for token in self.generate_sem_tokens(robot_token):
258251
current_line = token.lineno - 1
259252

0 commit comments

Comments
 (0)