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

Preallocate buffer size when reading data #239

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 28 additions & 7 deletions lib/eventsource.js
Expand Up @@ -15,6 +15,8 @@ var colon = 58
var space = 32
var lineFeed = 10
var carriageReturn = 13
// Beyond 256KB we could not observe any gain in performance
var maxBufferAheadAllocation = 1024 * 256

function hasBom (buf) {
return bom.every(function (charCode, index) {
Expand Down Expand Up @@ -179,19 +181,36 @@ function EventSource (url, eventSourceInitDict) {

// text/event-stream parser adapted from webkit's
// Source/WebCore/page/EventSource.cpp
var isFirst = true
var buf
var newBuffer
var startingPos = 0
var startingFieldLength = -1
var newBufferSize = 0
var bytesUsed = 0

res.on('data', function (chunk) {
buf = buf ? Buffer.concat([buf, chunk]) : chunk
if (isFirst && hasBom(buf)) {
buf = buf.slice(bom.length)
if (!buf) {
buf = chunk
if (hasBom(buf)) {
buf = buf.slice(bom.length)
}
bytesUsed = buf.length
} else {
if (chunk.length > buf.length - bytesUsed) {
newBufferSize = (buf.length * 2) + chunk.length
if (newBufferSize > maxBufferAheadAllocation) {
newBufferSize = buf.length + chunk.length + maxBufferAheadAllocation
}
newBuffer = Buffer.alloc(newBufferSize)
buf.copy(newBuffer, 0, 0, bytesUsed)
buf = newBuffer
}
chunk.copy(buf, bytesUsed)
bytesUsed += chunk.length
}

isFirst = false
var pos = 0
var length = buf.length
var length = bytesUsed

while (pos < length) {
if (discardTrailingNewline) {
Expand Down Expand Up @@ -235,8 +254,10 @@ function EventSource (url, eventSourceInitDict) {

if (pos === length) {
buf = void 0
bytesUsed = 0
} else if (pos > 0) {
buf = buf.slice(pos)
buf = buf.slice(pos, bytesUsed)
bytesUsed = buf.length
}
})
})
Expand Down
2 changes: 1 addition & 1 deletion test/eventsource_test.js
Expand Up @@ -454,7 +454,7 @@ describe('Parser', function () {
})

it('parses relatively huge messages efficiently', function (done) {
this.timeout(1000)
this.timeout(200)

createServer(function (err, server) {
if (err) return done(err)
Expand Down