Skip to content

Commit 3f0bdc6

Browse files
committed
perf: use web worker to run wasm logic to minimize main thread works
1 parent df2ba65 commit 3f0bdc6

File tree

12 files changed

+1100
-368
lines changed

12 files changed

+1100
-368
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"echarts": "^5.4.2",
2323
"prismjs": "^1.29.0",
2424
"vite-plugin-top-level-await": "^1.3.0",
25-
"vite-plugin-wasm": "^3.2.2",
2625
"vue": "^3.2.47",
2726
"vue-router": "^4.1.6",
2827
"xterm": "^5.3.0"
@@ -45,8 +44,9 @@
4544
"ts-node": "^10.9.1",
4645
"typescript": "^4.9.5",
4746
"vite": "^4.3.1",
48-
"vite-plugin-monaco-editor": "^1.1.0",
47+
"vite-bundle-analyzer": "^0.13.0",
4948
"vite-plugin-prismjs": "^0.0.8",
49+
"vite-plugin-wasm": "^3.2.2",
5050
"vue-tsc": "^1.4.4"
5151
}
5252
}

pnpm-lock.yaml

Lines changed: 428 additions & 284 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/gcEcharts.vue

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<script lang="ts" setup>
2-
import * as echarts from 'echarts';
32
import { ref, Ref, onMounted } from 'vue';
43
const echartsDom = ref<HTMLInputElement | null>(null);
54
const option = {
@@ -40,15 +39,17 @@ const option = {
4039
],
4140
};
4241
onMounted(() => {
43-
const myChart = echarts.init(echartsDom.value as HTMLElement);
44-
const loadEcharts = () => {
45-
const { top, bottom } = (echartsDom.value as HTMLElement).getBoundingClientRect();
46-
if (top < window.innerHeight && bottom > 0) {
47-
window.removeEventListener('scroll', loadEcharts);
48-
myChart.setOption(option);
49-
}
50-
};
51-
window.addEventListener('scroll', loadEcharts);
42+
import('echarts').then(async (echarts) => {
43+
const myChart = echarts.init(echartsDom.value as HTMLElement);
44+
const loadEcharts = () => {
45+
const { top, bottom } = (echartsDom.value as HTMLElement).getBoundingClientRect();
46+
if (top < window.innerHeight && bottom > 0) {
47+
window.removeEventListener('scroll', loadEcharts);
48+
myChart.setOption(option);
49+
}
50+
};
51+
window.addEventListener('scroll', loadEcharts);
52+
});
5253
});
5354
function gotoBenchmark() {
5455
window.open('https://pivotlang.tech/docs/systemlib/immix.html#%E6%80%A7%E8%83%BD');

src/hooks/monaco/index.ts

Lines changed: 70 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as pl from '@pivot-lang/pivot-lang';
1+
// import * as pl from '@pivot-lang/pivot-lang';
22
import * as monaco from 'monaco-editor';
33
import * as tp from 'vscode-languageserver-types';
4-
import monacotp from 'monaco-editor';
4+
import wrapper from './workerWrapper';
55

66
interface Diagnostic {
77
code: number;
@@ -41,12 +41,17 @@ export class PlMonaco {
4141
suggestOnTriggerCharacters: true,
4242
model: this.model,
4343
});
44-
initializeEditor(this.editor, code, uri);
44+
this.code = code;
4545
this.uri = uri;
4646
}
4747
private uri: string;
4848
editor: monaco.editor.IStandaloneCodeEditor;
4949
private model: monaco.editor.ITextModel;
50+
private code: string;
51+
52+
async initialize() {
53+
await initializeEditor(this.editor, this.code, this.uri);
54+
}
5055
setContent(code: string) {
5156
this.editor.revealLine(0);
5257
this.model.applyEdits([{
@@ -59,9 +64,11 @@ export class PlMonaco {
5964
// 暂不支持同时创建多个Monaco的DOM
6065
let created = false;
6166
let plMonaco: PlMonaco;
62-
export default function createPlMonaco(container: HTMLElement, code: string = '') {
67+
export default async function createPlMonaco(container: HTMLElement, code: string = '') {
6368
if (!created) {
69+
legend = await wrapper.get_legend();
6470
plMonaco = new PlMonaco(container, code, `http://www.test.com/main.pi`);
71+
await plMonaco.initialize();
6572
created = true;
6673
}
6774
return plMonaco;
@@ -71,35 +78,35 @@ export default function createPlMonaco(container: HTMLElement, code: string = ''
7178
let first = true;
7279
function initializeDocumentSemanticTokensProvider() {
7380
monaco.languages.registerDocumentSemanticTokensProvider('pivot-lang', {
74-
provideDocumentSemanticTokens: (m, id, token) => {
81+
provideDocumentSemanticTokens: async (m, id, token) => {
7582
if (first) {
7683
first = false;
77-
let tokens = pl.get_semantic_tokens_full();
84+
let tokens = await wrapper.get_semantic_tokens_full();
7885
// console.log(tokens);
7986
let a: any = {
8087
resultId: null,
81-
data: JSON.parse(tokens).data,
88+
data: tokens.data,
8289
};
8390
return a;
8491
}
85-
let tokens = pl.get_semantic_tokens();
92+
let tokens = await wrapper.get_semantic_tokens();
8693
// console.error(tokens);
8794
return {
8895
resultId: null,
89-
edits: JSON.parse(tokens).edits,
96+
edits: tokens.edits,
9097
};
9198
},
9299
getLegend: () => {
93-
let legend = pl.get_legend();
94-
// console.error(legend);
95-
return JSON.parse(legend);
100+
return legend;
96101
},
97-
releaseDocumentSemanticTokens: () => {},
102+
releaseDocumentSemanticTokens: () => { },
98103
});
99104
}
100105

101-
function initializeEditor(editor: monaco.editor.IStandaloneCodeEditor, code: string, uri: string) {
102-
let resp: Diags = JSON.parse(pl.set_init_content(code));
106+
let legend: monaco.languages.SemanticTokensLegend;
107+
108+
async function initializeEditor(editor: monaco.editor.IStandaloneCodeEditor, code: string, uri: string) {
109+
let resp: Diags = await wrapper.set_init_content(code);
103110
let markers = resp.diagnostics.map((d, n, a) => {
104111
let sev = monaco.MarkerSeverity.Warning;
105112
if (d.severity === 1) {
@@ -115,25 +122,24 @@ function initializeEditor(editor: monaco.editor.IStandaloneCodeEditor, code: str
115122
};
116123
});
117124
monaco.editor.setModelMarkers(editor.getModel()!, 'pivot-lang', markers);
118-
editor.getModel()?.onDidChangeContent((e) => {
125+
editor.getModel()?.onDidChangeContent(async (e) => {
119126
let re = e.changes.map((change) => {
120127
return {
121128
range: tp.Range.create(change.range.startLineNumber - 1, change.range.startColumn - 1, change.range.endLineNumber - 1, change.range.endColumn - 1),
122129
text: change.text,
123130
rangeLength: change.rangeLength,
124131
};
125132
});
126-
let resp: Diags = JSON.parse(
127-
pl.on_change_doc(
133+
let resp: Diags = await
134+
wrapper.on_change_doc(
128135
JSON.stringify({
129136
textDocument: {
130137
version: 0,
131138
uri,
132139
},
133140
contentChanges: re,
134141
})
135-
)
136-
);
142+
);
137143
// console.log(resp);
138144

139145
let markers = resp.diagnostics.map((d, n, a) => {
@@ -199,19 +205,19 @@ function initializeMonaco() {
199205
});
200206

201207
enum colorType {
202-
chalky= '#E5C07B',
203-
coral= '#e06c75',
204-
dark= '#5c6370',
205-
error= '#f44747',
206-
fountainBlue= '#56b6c2',
207-
green= '#98c379',
208-
invalid= '#ffffff',
209-
lightDark= '#7f848e',
210-
lightWhite= '#abb2bf',
211-
malibu= '#61afef',
212-
purple= '#c678dd',
213-
whiskey= '#d19a66',
214-
deepRed= '#BE5046',
208+
chalky = '#E5C07B',
209+
coral = '#e06c75',
210+
dark = '#5c6370',
211+
error = '#f44747',
212+
fountainBlue = '#56b6c2',
213+
green = '#98c379',
214+
invalid = '#ffffff',
215+
lightDark = '#7f848e',
216+
lightWhite = '#abb2bf',
217+
malibu = '#61afef',
218+
purple = '#c678dd',
219+
whiskey = '#d19a66',
220+
deepRed = '#BE5046',
215221
}
216222
// Define a new theme that contains only rules that match this language
217223
monaco.editor.defineTheme('pltheme', {
@@ -247,38 +253,48 @@ function initializeMonaco() {
247253
});
248254

249255
monaco.languages.registerInlayHintsProvider('pivot-lang', {
250-
provideInlayHints(model, range, token) {
251-
// console.log(JSON.parse(pl.get_inlay_hints()))
252-
let hints: tp.InlayHint[] = JSON.parse(pl.get_inlay_hints());
256+
provideInlayHints: async (model, range, token) => {
257+
let hints: tp.InlayHint[] = await wrapper.get_inlay_hints();
253258
console.error(hints);
254259
return {
255260
hints: hints.map((h) => {
261+
let label: string | monaco.languages.InlayHintLabelPart[];
262+
if (typeof h.label === 'string') {
263+
label = h.label;
264+
} else {
265+
label = h.label.map((l) => {
266+
return {
267+
label: l.value,
268+
tooltip: l.tooltip,
269+
};
270+
});
271+
}
256272
return {
257273
kind: h.kind,
258-
label: h.label,
274+
label: label,
259275
position: {
260276
lineNumber: h.position.line + 1,
261277
column: h.position.character + 1,
262278
},
263279
};
264280
}),
265-
dispose: () => {},
266-
} as any;
281+
dispose: () => { },
282+
} as monaco.languages.InlayHintList;
267283
},
268284
});
269285

270286
// Register a completion item provider for the new language
271287
monaco.languages.registerCompletionItemProvider('pivot-lang', {
272288
triggerCharacters: ['.', ':'],
273-
provideCompletionItems: (model, position, ctx) => {
289+
provideCompletionItems: async (model, position, ctx) => {
274290
var word = model.getWordUntilPosition(position);
275291
var range = {
276292
startLineNumber: position.lineNumber,
277293
endLineNumber: position.lineNumber,
278294
startColumn: word.startColumn,
279295
endColumn: word.endColumn,
280296
};
281-
let dynamic_suggestions = JSON.parse(pl.get_completions());
297+
let dynamic_suggestions = await wrapper.get_completions();
282298
// console.warn(dynamic_suggestions);
283299
for (let sug of dynamic_suggestions) {
284300
sug.insertTextRules = 0;
@@ -287,7 +303,7 @@ function initializeMonaco() {
287303
sug.kind = monaco.languages.CompletionItemKind.Keyword;
288304
break;
289305
case 3:
290-
306+
291307
sug.kind = monaco.languages.CompletionItemKind.Function;
292308
sug.insertTextRules = monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet;
293309
break;
@@ -407,15 +423,15 @@ function initializeMonaco() {
407423
},
408424
});
409425
monaco.languages.registerDefinitionProvider('pivot-lang', {
410-
provideDefinition: (model, position, token) => {
411-
let re: tp.Location | any[] = JSON.parse(
412-
pl.go_to_def(
426+
provideDefinition: async (model, position, token) => {
427+
let re: tp.Location | any[] = await
428+
wrapper.go_to_def(
413429
JSON.stringify({
414430
line: position.lineNumber - 1,
415431
character: position.column - 1,
416432
})
417433
)
418-
);
434+
;
419435
if (re instanceof Array) {
420436
return [];
421437
}
@@ -429,15 +445,14 @@ function initializeMonaco() {
429445
},
430446
});
431447
monaco.languages.registerReferenceProvider('pivot-lang', {
432-
provideReferences: (model, position, context, token) => {
433-
let re: tp.Location[] = JSON.parse(
434-
pl.get_refs(
435-
JSON.stringify({
436-
line: position.lineNumber - 1,
437-
character: position.column - 1,
438-
})
439-
)
440-
);
448+
provideReferences: async (model, position, context, token) => {
449+
let re: tp.Location[] = await wrapper.get_refs(
450+
JSON.stringify({
451+
line: position.lineNumber - 1,
452+
character: position.column - 1,
453+
})
454+
)
455+
;
441456
console.log(re);
442457
let refs = re.map((r, n, a) => {
443458
let uri = monaco.Uri.parse(r.uri);

src/hooks/monaco/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@
3838
},
3939
"devDependencies": {
4040
"css-loader": "^6.7.3",
41+
"promise-worker": "^2.0.1",
4142
"style-loader": "^3.3.2",
4243
"ts-loader": "^9.4.2",
4344
"tslib": "^2.5.0",
4445
"typescript": "^5.0.4",
46+
"vite-plugin-wasm": "^3.3.0",
4547
"webpack-cli": "^5.1.0",
4648
"webpack-dev-server": "^4.14.0"
4749
}

0 commit comments

Comments
 (0)