Skip to content

Commit

Permalink
create reusable buffers for methods which are called many times
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed Dec 24, 2022
1 parent 4be1357 commit 7deb52f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/ExpandingBuffer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Buffer } from 'node:buffer'
import { EMPTY_BUFFER } from './constants'
import { clamp } from './functions'

export class ExpandingBuffer {
Expand Down Expand Up @@ -46,7 +47,7 @@ export class ExpandingBuffer {
*/
read(offset: number = 0, limit: number = this.size()) {
if (offset < 0 || limit < 1) {
return Buffer.from([])
return EMPTY_BUFFER
}

if (offset + limit < this.size()) {
Expand Down
7 changes: 5 additions & 2 deletions src/Explode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
DictionarySize,
DistBits,
DistCode,
EMPTY_BUFFER,
ExLenBits,
LenBase,
LenBits,
Expand Down Expand Up @@ -95,6 +96,7 @@ export class Explode {
#asciiTable2D34: number[] = repeat(0, 0x100)
#asciiTable2E34: number[] = repeat(0, 0x80)
#asciiTable2EB4: number[] = repeat(0, 0x100)
#reusableByte: Buffer = Buffer.alloc(1)

constructor(config: Config = {}) {
this.#verbose = config?.verbose ?? false
Expand Down Expand Up @@ -126,7 +128,7 @@ export class Explode {
const blockSize = 0x1000

if (instance.#outputBuffer.size() <= blockSize) {
callback(null, Buffer.from([]))
callback(null, EMPTY_BUFFER)
return
}

Expand Down Expand Up @@ -347,7 +349,8 @@ export class Explode {
addition = availableData
}
} else {
addition = Buffer.from([nextLiteral])
this.#reusableByte[0] = nextLiteral
addition = this.#reusableByte
}

this.#outputBuffer.append(addition)
Expand Down
15 changes: 10 additions & 5 deletions src/Implode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import {
DictionarySize,
DistBits,
DistCode,
EMPTY_BUFFER,
ExLenBits,
LenBits,
LenCode,
LONGEST_ALLOWED_REPETITION,
SINGLE_ZERO_BUFFER,
} from './constants'
import { InvalidCompressionTypeError, InvalidDictionarySizeError } from './errors'
import { ExpandingBuffer } from './ExpandingBuffer'
Expand Down Expand Up @@ -73,6 +75,7 @@ export class Implode {
#outBits: number = 0
#nChBits: number[] = repeat(0, 0x306)
#nChCodes: number[] = repeat(0, 0x306)
#reusableByte: Buffer = Buffer.alloc(1)

constructor(compressionType: Compression, dictionarySize: DictionarySize, config: Config) {
if (!(compressionType in Compression) || compressionType === Compression.Unknown) {
Expand Down Expand Up @@ -112,7 +115,7 @@ export class Implode {
const blockSize = 0x800

if (instance.#outputBuffer.size() <= blockSize) {
callback(null, Buffer.from([]))
callback(null, EMPTY_BUFFER)
return
}

Expand All @@ -129,7 +132,7 @@ export class Implode {
if (instance.#outBits === 0) {
// set last byte to 0
instance.#outputBuffer.dropEnd(1)
instance.#outputBuffer.append(Buffer.from([0]))
instance.#outputBuffer.append(SINGLE_ZERO_BUFFER)
}

callback(null, output)
Expand Down Expand Up @@ -350,18 +353,20 @@ export class Implode {

const lastBytes = this.#outputBuffer.readByte(this.#outputBuffer.size() - 1)
this.#outputBuffer.dropEnd(1)
this.#outputBuffer.append(Buffer.from([lastBytes | getLowestNBits(8, bitBuffer << outBits)]))
this.#reusableByte[0] = lastBytes | getLowestNBits(8, bitBuffer << outBits)
this.#outputBuffer.append(this.#reusableByte)

this.#outBits = this.#outBits + nBits

if (this.#outBits > 8) {
bitBuffer = bitBuffer >> (8 - outBits)
this.#outputBuffer.append(Buffer.from([getLowestNBits(8, bitBuffer)]))
this.#reusableByte[0] = getLowestNBits(8, bitBuffer)
this.#outputBuffer.append(this.#reusableByte)
this.#outBits = getLowestNBits(3, this.#outBits)
} else {
this.#outBits = getLowestNBits(3, this.#outBits)
if (this.#outBits === 0) {
this.#outputBuffer.append(Buffer.from([0]))
this.#outputBuffer.append(SINGLE_ZERO_BUFFER)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export enum DictionarySize {
}

export const EMPTY_BUFFER = Buffer.from([])
export const SINGLE_ZERO_BUFFER = Buffer.from([0])

export const LONGEST_ALLOWED_REPETITION = 0x204

Expand Down

0 comments on commit 7deb52f

Please sign in to comment.