protobuf-fastdsl 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -32
- package/dist/index.js +43 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# protobuf-
|
|
1
|
+
# protobuf-fastdsl
|
|
2
2
|
|
|
3
3
|
一个 Vite 插件,将 TypeScript protobuf 接口在构建时编译为**完全内联**、零依赖的编解码函数。
|
|
4
4
|
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
## 安装
|
|
16
16
|
|
|
17
17
|
```bash
|
|
18
|
-
npm install protobuf-
|
|
18
|
+
npm install protobuf-fastdsl
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
## 快速开始
|
|
@@ -23,7 +23,7 @@ npm install protobuf-dsl
|
|
|
23
23
|
**1. 在 `vite.config.ts` 中添加插件:**
|
|
24
24
|
|
|
25
25
|
```ts
|
|
26
|
-
import protobufVite from 'protobuf-
|
|
26
|
+
import protobufVite from 'protobuf-fastdsl';
|
|
27
27
|
|
|
28
28
|
export default defineConfig({
|
|
29
29
|
plugins: [protobufVite()],
|
|
@@ -35,7 +35,7 @@ export default defineConfig({
|
|
|
35
35
|
```json
|
|
36
36
|
{
|
|
37
37
|
"compilerOptions": {
|
|
38
|
-
"types": ["protobuf-
|
|
38
|
+
"types": ["protobuf-fastdsl/types"]
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
```
|
|
@@ -112,34 +112,34 @@ const data = protobuf_encode<Wrapper<Wrapper<string>>>({
|
|
|
112
112
|
说明:
|
|
113
113
|
- 所有 64 位整数类型在 TypeScript 中统一映射为 `bigint`
|
|
114
114
|
|
|
115
|
-
## ⚡ 性能测试
|
|
116
|
-
|
|
117
|
-
benchmark 的 `.proto` 定义位于 `bench/proto/bench.proto`,生成入口是 `npm run bench:gen`。脚本会先校验所有实现产出的 wire bytes 完全一致,再统计绝对吞吐率。下表统一以 `protobuf-
|
|
118
|
-
|
|
119
|
-
参与对比的实现:
|
|
120
|
-
- `protobuf-ts(protoc)` — `@protobuf-ts/plugin + protoc` 生成代码
|
|
121
|
-
- `protobuf-ts` — 手写 `MessageType` 反射运行时
|
|
122
|
-
- `protobufjs(static)` — `pbjs static-module` 从同一份 `.proto` 生成代码
|
|
123
|
-
- `protobufjs` — 反射 API
|
|
124
|
-
- `protobuf` — `google-protobuf + protoc-gen-js` 生成代码
|
|
125
|
-
|
|
126
|
-
> Node v22.11.0 | Windows x64 | 每项测试 50 万次迭代
|
|
127
|
-
|
|
128
|
-
### 编码性能(ops/sec — 越高越好)
|
|
129
|
-
|
|
130
|
-
| 消息类型 | protobuf-
|
|
131
|
-
|---------|:-----------:|:-------------------:|:-----------:|:------------------:|:----------:|:--------:|
|
|
132
|
-
| 简单消息(1 个字段) | **35,782,016 (1x)** | 8,238,128 (4.34x slower) | 5,828,158 (6.14x slower) | 16,466,923 (2.17x slower) | 16,819,500 (2.13x slower) | 5,794,260 (6.18x slower) |
|
|
133
|
-
| 多字段消息(3 个字段) | **11,361,700 (1x)** | 1,767,940 (6.43x slower) | 1,394,561 (8.15x slower) | 4,481,491 (2.54x slower) | 4,240,911 (2.68x slower) | 1,609,704 (7.06x slower) |
|
|
134
|
-
| 嵌套消息 | **21,264,923 (1x)** | 2,815,065 (7.55x slower) | 2,019,035 (10.53x slower) | 10,287,198 (2.07x slower) | 9,902,049 (2.15x slower) | 2,365,296 (8.99x slower) |
|
|
135
|
-
|
|
136
|
-
### 解码性能(ops/sec — 越高越好)
|
|
137
|
-
|
|
138
|
-
| 消息类型 | protobuf-
|
|
139
|
-
|---------|:-----------:|:-------------------:|:-----------:|:------------------:|:----------:|:--------:|
|
|
140
|
-
| 简单消息(1 个字段) | **99,577,790 (1x)** | 7,586,436 (13.13x slower) | 9,032,575 (11.02x slower) | 27,215,772 (3.66x slower) | 19,002,736 (5.24x slower) | 11,497,372 (8.66x slower) |
|
|
141
|
-
| 多字段消息(3 个字段) | **10,190,107 (1x)** | 3,891,478 (2.62x slower) | 3,219,789 (3.16x slower) | 5,141,816 (1.98x slower) | 4,893,718 (2.08x slower) | 3,606,205 (2.83x slower) |
|
|
142
|
-
| 嵌套消息 | **51,491,185 (1x)** | 8,376,444 (6.15x slower) | 6,033,801 (8.53x slower) | 11,922,077 (4.32x slower) | 13,326,794 (3.86x slower) | 3,148,311 (16.36x slower) |
|
|
115
|
+
## ⚡ 性能测试
|
|
116
|
+
|
|
117
|
+
benchmark 的 `.proto` 定义位于 `bench/proto/bench.proto`,生成入口是 `npm run bench:gen`。脚本会先校验所有实现产出的 wire bytes 完全一致,再统计绝对吞吐率。下表统一以 `protobuf-fastdsl = 1x` 为基线,其他实现显示相对它慢了多少。
|
|
118
|
+
|
|
119
|
+
参与对比的实现:
|
|
120
|
+
- `protobuf-ts(protoc)` — `@protobuf-ts/plugin + protoc` 生成代码
|
|
121
|
+
- `protobuf-ts` — 手写 `MessageType` 反射运行时
|
|
122
|
+
- `protobufjs(static)` — `pbjs static-module` 从同一份 `.proto` 生成代码
|
|
123
|
+
- `protobufjs` — 反射 API
|
|
124
|
+
- `protobuf` — `google-protobuf + protoc-gen-js` 生成代码
|
|
125
|
+
|
|
126
|
+
> Node v22.11.0 | Windows x64 | 每项测试 50 万次迭代
|
|
127
|
+
|
|
128
|
+
### 编码性能(ops/sec — 越高越好)
|
|
129
|
+
|
|
130
|
+
| 消息类型 | protobuf-fastdsl | protobuf-ts(protoc) | protobuf-ts | protobufjs(static) | protobufjs | protobuf |
|
|
131
|
+
|---------|:-----------:|:-------------------:|:-----------:|:------------------:|:----------:|:--------:|
|
|
132
|
+
| 简单消息(1 个字段) | **35,782,016 (1x)** | 8,238,128 (4.34x slower) | 5,828,158 (6.14x slower) | 16,466,923 (2.17x slower) | 16,819,500 (2.13x slower) | 5,794,260 (6.18x slower) |
|
|
133
|
+
| 多字段消息(3 个字段) | **11,361,700 (1x)** | 1,767,940 (6.43x slower) | 1,394,561 (8.15x slower) | 4,481,491 (2.54x slower) | 4,240,911 (2.68x slower) | 1,609,704 (7.06x slower) |
|
|
134
|
+
| 嵌套消息 | **21,264,923 (1x)** | 2,815,065 (7.55x slower) | 2,019,035 (10.53x slower) | 10,287,198 (2.07x slower) | 9,902,049 (2.15x slower) | 2,365,296 (8.99x slower) |
|
|
135
|
+
|
|
136
|
+
### 解码性能(ops/sec — 越高越好)
|
|
137
|
+
|
|
138
|
+
| 消息类型 | protobuf-fastdsl | protobuf-ts(protoc) | protobuf-ts | protobufjs(static) | protobufjs | protobuf |
|
|
139
|
+
|---------|:-----------:|:-------------------:|:-----------:|:------------------:|:----------:|:--------:|
|
|
140
|
+
| 简单消息(1 个字段) | **99,577,790 (1x)** | 7,586,436 (13.13x slower) | 9,032,575 (11.02x slower) | 27,215,772 (3.66x slower) | 19,002,736 (5.24x slower) | 11,497,372 (8.66x slower) |
|
|
141
|
+
| 多字段消息(3 个字段) | **10,190,107 (1x)** | 3,891,478 (2.62x slower) | 3,219,789 (3.16x slower) | 5,141,816 (1.98x slower) | 4,893,718 (2.08x slower) | 3,606,205 (2.83x slower) |
|
|
142
|
+
| 嵌套消息 | **51,491,185 (1x)** | 8,376,444 (6.15x slower) | 6,033,801 (8.53x slower) | 11,922,077 (4.32x slower) | 13,326,794 (3.86x slower) | 3,148,311 (16.36x slower) |
|
|
143
143
|
|
|
144
144
|
## 许可证
|
|
145
145
|
|
package/dist/index.js
CHANGED
|
@@ -418,6 +418,24 @@ function buildSingularBlock(field, index) {
|
|
|
418
418
|
]
|
|
419
419
|
};
|
|
420
420
|
}
|
|
421
|
+
if (typeName === "sint_32") {
|
|
422
|
+
return {
|
|
423
|
+
declare: [` const ${valueVar} = obj.${name};`],
|
|
424
|
+
size: [
|
|
425
|
+
` if (${valueVar} != null && ${valueVar} !== 0) {`,
|
|
426
|
+
` const _val = ((${valueVar} << 1) ^ (${valueVar} >> 31)) >>> 0;`,
|
|
427
|
+
` size += ${tagLength};`,
|
|
428
|
+
varintSize("_val", " "),
|
|
429
|
+
` }`
|
|
430
|
+
],
|
|
431
|
+
write: [
|
|
432
|
+
` if (${valueVar} != null && ${valueVar} !== 0) {`,
|
|
433
|
+
writeTag(fieldNumber, 0, " "),
|
|
434
|
+
writeVarint(`((${valueVar} << 1) ^ (${valueVar} >> 31)) >>> 0`, " "),
|
|
435
|
+
` }`
|
|
436
|
+
]
|
|
437
|
+
};
|
|
438
|
+
}
|
|
421
439
|
if (wireType === 0 /* Varint */) {
|
|
422
440
|
return {
|
|
423
441
|
declare: [` const ${valueVar} = obj.${name};`],
|
|
@@ -639,6 +657,28 @@ function buildRepeatedBlock(field, index) {
|
|
|
639
657
|
]
|
|
640
658
|
};
|
|
641
659
|
}
|
|
660
|
+
if (typeName === "sint_32") {
|
|
661
|
+
return {
|
|
662
|
+
declare: [` const ${arrayVar} = obj.${name};`],
|
|
663
|
+
size: [
|
|
664
|
+
` if (${arrayVar} != null && ${arrayVar}.length > 0) {`,
|
|
665
|
+
` for (let _i = 0; _i < ${arrayVar}.length; _i++) {`,
|
|
666
|
+
` const _val = ((${arrayVar}[_i] << 1) ^ (${arrayVar}[_i] >> 31)) >>> 0;`,
|
|
667
|
+
` size += ${tagLength};`,
|
|
668
|
+
varintSize("_val", " "),
|
|
669
|
+
` }`,
|
|
670
|
+
` }`
|
|
671
|
+
],
|
|
672
|
+
write: [
|
|
673
|
+
` if (${arrayVar} != null && ${arrayVar}.length > 0) {`,
|
|
674
|
+
` for (let _i = 0; _i < ${arrayVar}.length; _i++) {`,
|
|
675
|
+
writeTag(fieldNumber, 0, " "),
|
|
676
|
+
writeVarint(`((${arrayVar}[_i] << 1) ^ (${arrayVar}[_i] >> 31)) >>> 0`, " "),
|
|
677
|
+
` }`,
|
|
678
|
+
` }`
|
|
679
|
+
]
|
|
680
|
+
};
|
|
681
|
+
}
|
|
642
682
|
if (wireType === 0 /* Varint */) {
|
|
643
683
|
return {
|
|
644
684
|
declare: [` const ${arrayVar} = obj.${name};`],
|
|
@@ -845,6 +885,9 @@ function decodeField(field, index) {
|
|
|
845
885
|
} else {
|
|
846
886
|
L.push(assign(`__zigZagDecode64(_val)`));
|
|
847
887
|
}
|
|
888
|
+
} else if (typeName === "sint_32") {
|
|
889
|
+
L.push(varintDec("_val", I));
|
|
890
|
+
L.push(assign(`(_val >>> 1) ^ -(_val & 1)`));
|
|
848
891
|
} else if (wireType === 0 /* Varint */) {
|
|
849
892
|
L.push(varintDec("_val", I));
|
|
850
893
|
L.push(assign(`_val >>> 0`));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "protobuf-fastdsl",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"typescript": "^5.5.0"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
|
-
"vite": "^5.0.0 || ^6.0.0"
|
|
30
|
+
"vite": "^5.0.0 || ^6.0.0 || ^7.0.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@types/node": "^25.4.0",
|