Skip to content

Commit

Permalink
perf: remove transient strings to reduce memory
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Mar 14, 2020
1 parent 0f25017 commit 3dfdf98
Show file tree
Hide file tree
Showing 16 changed files with 124 additions and 125 deletions.
16 changes: 10 additions & 6 deletions benchmark/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ function html () {
for (let i = 0; i < SAMPLE_COUNT; i++) {
templates.push(engine.parse(str))
}
global.gc()
const diff1 = (getHeapStatistics().used_heap_size - base) / SAMPLE_COUNT
console.log(`${h(str.length)} lorem-html before GC x ${h(diff1)}/tpl (${SAMPLE_COUNT} instances sampled)`)

const diff = (getHeapStatistics().used_heap_size - base) / SAMPLE_COUNT
console.log(`${h(str.length)} HTML template x ${h(diff)}/instance (${SAMPLE_COUNT} instances sampled)`)
global.gc()
const diff2 = (getHeapStatistics().used_heap_size - base) / SAMPLE_COUNT
console.log(`${h(str.length)} lorem-html after GC x ${h(diff2)}/tpl (${SAMPLE_COUNT} instances sampled)`)
}

function todolist () {
Expand All @@ -44,10 +46,12 @@ function todolist () {
for (let i = 0; i < SAMPLE_COUNT; i++) {
templates.push(engine.parse(str))
}
global.gc()
const diff1 = (getHeapStatistics().used_heap_size - base) / SAMPLE_COUNT
console.log(`${h(str.length)} todolist before GC x ${h(diff1)}/tpl (${SAMPLE_COUNT} instances sampled)`)

const diff = (getHeapStatistics().used_heap_size - base) / SAMPLE_COUNT
console.log(`${h(str.length)} Todo template x ${h(diff)}/instance (${SAMPLE_COUNT} instances sampled)`)
global.gc()
const diff2 = (getHeapStatistics().used_heap_size - base) / SAMPLE_COUNT
console.log(`${h(str.length)} todolist after GC x ${h(diff2)}/tpl (${SAMPLE_COUNT} instances sampled)`)
}

function h (size) {
Expand Down
15 changes: 0 additions & 15 deletions rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ const esm = {
delimiters: ['', ''],
'./fs/node': './fs/browser'
}),
replace({
include: './src/parser/token.ts',
delimiters: ['', ''],
'./flatten/node': './flatten/browser'
}),
typescript({
tsconfigOverride: {
include: [ 'src' ],
Expand Down Expand Up @@ -83,11 +78,6 @@ const umd = {
delimiters: ['', ''],
'./fs/node': './fs/browser'
}),
replace({
include: './src/parser/token.ts',
delimiters: ['', ''],
'./flatten/node': './flatten/browser'
}),
typescript({
tsconfigOverride: {
include: [ 'src' ],
Expand Down Expand Up @@ -116,11 +106,6 @@ const min = {
delimiters: ['', ''],
'./fs/node': './fs/browser'
}),
replace({
include: './src/parser/token.ts',
delimiters: ['', ''],
'./flatten/node': './flatten/browser'
}),
typescript({
tsconfigOverride: {
include: [ 'src' ],
Expand Down
6 changes: 1 addition & 5 deletions src/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export class Context {
private parseProp (str: string) {
str = String(str)
const seq: string[] = []
const push = () => name.length && (seq.push(name), (name = ''))
let name = ''
let j
let i = 0
Expand Down Expand Up @@ -120,11 +121,6 @@ export class Context {
throw new TypeError(`invalid path:"${str}"`)
}
return seq

function push () {
if (name.length) seq.push(name)
name = ''
}
}
}

Expand Down
1 change: 0 additions & 1 deletion src/parser/filter-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ export class FilterToken extends Token {
file?: string
) {
super(raw, input, line, col, file)
this.type = 'filter'
}
}
3 changes: 0 additions & 3 deletions src/parser/flatten/browser.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/parser/flatten/node.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/parser/html-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { Token } from './token'
export class HTMLToken extends Token {
public constructor (str: string, input: string, line: number, col: number, file?: string) {
super(str, input, line, col, file)
this.type = 'html'
this.content = str
}
public static is (token: Token): token is HTMLToken {
return token.type === 'html'
return token instanceof HTMLToken
}
}
3 changes: 1 addition & 2 deletions src/parser/output-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ export class OutputToken extends DelimitedToken {
file?: string
) {
super(raw, value, input, line, pos, options.trimOutputLeft, options.trimOutputRight, file)
this.type = 'output'
}
public static is (token: Token): token is OutputToken {
return token.type === 'output'
return token instanceof OutputToken
}
}
19 changes: 19 additions & 0 deletions src/parser/substr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export class Substr {
constructor (
public str: string,
public begin: number,
public end: number = begin
) {}
size () {
return this.end - this.begin
}
toString () {
return this.str.slice(this.begin, this.end)
}
first () {
return this.str[this.begin]
}
last () {
return this.str[this.end - 1]
}
}
3 changes: 1 addition & 2 deletions src/parser/tag-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export class TagToken extends DelimitedToken {
file?: string
) {
super(raw, value, input, line, pos, options.trimTagLeft, options.trimTagRight, file)
this.type = 'tag'
const match = this.content.match(lexical.tagLine)
if (!match) {
throw new TokenizationError(`illegal tag syntax`, this)
Expand All @@ -26,6 +25,6 @@ export class TagToken extends DelimitedToken {
this.args = match[2]
}
public static is (token: Token): token is TagToken {
return token.type === 'tag'
return token instanceof TagToken
}
}
8 changes: 2 additions & 6 deletions src/parser/token.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { flatten } from './flatten/node'

export class Token {
public trimLeft = false
public trimRight = false
public type = 'notset'
public raw: string
public content: string
public constructor (raw: string,
public constructor (
public raw: string,
public input: string,
public line: number,
public col: number,
public file?: string
) {
this.raw = flatten(raw)
this.content = raw
}
}
Loading

0 comments on commit 3dfdf98

Please sign in to comment.