Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: reduce AST size optimization #1456

Merged
merged 1 commit into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions packages/message-compiler/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ export function baseCompile(
// transform ASTs
transform(ast, assignedOptions)

// optimize ASTs
doOptimize && optimize(ast)

// generate javascript codes
return generate(ast, assignedOptions)
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/message-compiler/src/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type MessageElementNode =

export interface TextNode extends Node {
type: NodeTypes.Text
value: string
value?: string
}

export interface NamedNode extends Node {
Expand All @@ -65,7 +65,7 @@ export interface ListNode extends Node {

export interface LiteralNode extends Node {
type: NodeTypes.Literal
value: string
value?: string
}

export interface LinkedNode extends Node {
Expand Down
10 changes: 10 additions & 0 deletions packages/message-compiler/src/optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function optimizeMessageNode(message: MessageNode) {
const item = message.items[0]
if (item.type === NodeTypes.Text || item.type === NodeTypes.Literal) {
message.static = item.value
delete item.value // optimization for size
}
} else {
const values: string[] = []
Expand All @@ -26,10 +27,19 @@ function optimizeMessageNode(message: MessageNode) {
if (!(item.type === NodeTypes.Text || item.type === NodeTypes.Literal)) {
break
}
if (item.value == null) {
break
}
values.push(item.value)
}
if (values.length === message.items.length) {
message.static = join(values)
for (let i = 0; i < message.items.length; i++) {
const item = message.items[i]
if (item.type === NodeTypes.Text || item.type === NodeTypes.Literal) {
delete item.value // optimization for size
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ exports[`compiler options > jit: true > ast 1`] = `
"items": [
{
"type": 3,
"value": "hello world",
},
],
"static": "hello world",
Expand All @@ -282,7 +281,6 @@ exports[`compiler options > location: false > ast 1`] = `
"value": "hello world",
},
],
"static": "hello world",
"type": 2,
},
"type": 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ exports[`full text items: foo{'@'}domain.com 1`] = `
"items": [
{
"type": 3,
"value": "foo",
},
{
"type": 9,
"value": "@",
},
{
"type": 3,
"value": "domain.com",
},
],
"static": "foo@domain.com",
Expand All @@ -32,7 +29,6 @@ exports[`full text message on plural: no apples | one apple | many apples 1`] =
"items": [
{
"type": 3,
"value": "no apples",
},
],
"static": "no apples",
Expand All @@ -42,7 +38,6 @@ exports[`full text message on plural: no apples | one apple | many apples 1`] =
"items": [
{
"type": 3,
"value": "one apple",
},
],
"static": "one apple",
Expand All @@ -52,7 +47,6 @@ exports[`full text message on plural: no apples | one apple | many apples 1`] =
"items": [
{
"type": 3,
"value": "many apples",
},
],
"static": "many apples",
Expand All @@ -73,7 +67,6 @@ exports[`incldue dynamic node in pluarl: no apples | {0} apple | {n} apples 1`]
"items": [
{
"type": 3,
"value": "no apples",
},
],
"static": "no apples",
Expand Down Expand Up @@ -141,7 +134,6 @@ exports[`literal only: {'hello world'} 1`] = `
"items": [
{
"type": 9,
"value": "hello world",
},
],
"static": "hello world",
Expand All @@ -157,7 +149,6 @@ exports[`text only: hello world 1`] = `
"items": [
{
"type": 3,
"value": "hello world",
},
],
"static": "hello world",
Expand Down