This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 141
(perf): minor optimization on attributes extraction #247
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
51c5bff
(perf): minor optimization on attributes extraction
vigneshshanmugam c4d81d3
compute pipe attributes only once
vigneshshanmugam bc22003
address review comments
vigneshshanmugam c5e35be
pass default value for id and add tests
vigneshshanmugam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,13 @@ const zlib = require('zlib'); | |
const { globalTracer, Tags } = require('opentracing'); | ||
const tracer = globalTracer(); | ||
|
||
const hasValue = value => { | ||
if (value || value === '') { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
|
||
/** | ||
* Merge the attributes based on the fragment tag attributes and context | ||
* | ||
|
@@ -24,12 +31,23 @@ const getAttributes = (tag, context) => { | |
Object.assign(attributes, fragmentCtxt); | ||
} | ||
|
||
return Object.assign({}, attributes, { | ||
url: attributes.src, | ||
const { | ||
src, | ||
async: isAsync, | ||
primary, | ||
public: isPublic, | ||
timeout | ||
} = attributes; | ||
|
||
return { | ||
url: src, | ||
id: fragmentId, | ||
async: hasValue(isAsync), | ||
primary: hasValue(primary), | ||
public: hasValue(isPublic), | ||
fallbackUrl: attributes['fallback-src'], | ||
timeout: parseInt(attributes.timeout || 3000, 10) | ||
}); | ||
timeout: parseInt(timeout || 3000, 10) | ||
}; | ||
}; | ||
|
||
/** | ||
|
@@ -57,23 +75,12 @@ module.exports = class Fragment extends EventEmitter { | |
} = {} | ||
) { | ||
super(); | ||
this.attributes = getAttributes(tag, context, index); | ||
['async', 'primary', 'public'].forEach(key => { | ||
let value = this.attributes[key]; | ||
if (value || value === '') { | ||
value = true; | ||
} else { | ||
value = false; | ||
} | ||
this.attributes[key] = value; | ||
}); | ||
|
||
this.attributes = getAttributes(tag, context); | ||
this.index = index; | ||
this.maxAssetLinks = maxAssetLinks; | ||
this.getPipeAttributes = () => | ||
pipeAttributes( | ||
Object.assign({}, { id: this.index }, tag.attributes) | ||
); | ||
this.pipeAttributes = pipeAttributes( | ||
Object.assign({}, { id: this.index }, tag.attributes) | ||
); | ||
this.requestFragment = requestFragment; | ||
this.pipeInstanceName = pipeInstanceName; | ||
this.stream = new PassThrough(); | ||
|
@@ -96,22 +103,29 @@ module.exports = class Fragment extends EventEmitter { | |
|
||
const spanOptions = parentSpan ? { childOf: parentSpan } : {}; | ||
const span = tracer.startSpan('fetch-fragment', spanOptions); | ||
|
||
const { | ||
id = 'unnamed', | ||
primary, | ||
async: isAsync, | ||
public: isPublic | ||
} = this.attributes; | ||
|
||
span.addTags({ | ||
[Tags.SPAN_KIND]: Tags.SPAN_KIND_RPC_CLIENT, | ||
[Tags.HTTP_URL]: url, | ||
id: this.attributes.id || 'unnamed', | ||
fallback: isFallback, | ||
primary: this.attributes.primary, | ||
async: this.attributes.async, | ||
public: this.attributes.public | ||
public: isPublic, | ||
async: isAsync, | ||
id, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The behaviour for id has changed, default arguments only apply to undefined values. Null or empty string will passthrough. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch. changing it back |
||
primary | ||
}); | ||
|
||
this.requestFragment(url, this.attributes, request, span).then( | ||
res => this.onResponse(res, isFallback, span), | ||
err => { | ||
if (!isFallback) { | ||
const { fallbackUrl } = this.attributes; | ||
if (fallbackUrl) { | ||
if (this.attributes.fallbackUrl) { | ||
this.emit('fallback', err); | ||
this.fetch(request, true, span); | ||
} else { | ||
|
@@ -133,7 +147,7 @@ module.exports = class Fragment extends EventEmitter { | |
} | ||
); | ||
// Async fragments are piped later on the page | ||
if (this.attributes.async) { | ||
if (isAsync) { | ||
return Buffer.from( | ||
`<script data-pipe>${this.pipeInstanceName}.placeholder(${this | ||
.index})</script>` | ||
|
@@ -215,9 +229,10 @@ module.exports = class Fragment extends EventEmitter { | |
* - CSS for the async fragments are loaded using custom loadCSS(available in src/pipe.js) | ||
*/ | ||
insertStart() { | ||
const { async: isAsync, id } = this.attributes; | ||
this.styleRefs.forEach(uri => { | ||
this.stream.write( | ||
this.attributes.async | ||
isAsync | ||
? `<script>${this | ||
.pipeInstanceName}.loadCSS("${uri}")</script>` | ||
: `<link rel="stylesheet" href="${uri}">` | ||
|
@@ -234,12 +249,12 @@ module.exports = class Fragment extends EventEmitter { | |
} | ||
|
||
const range = [this.index, this.index + this.scriptRefs.length - 1]; | ||
const fragmentId = this.attributes.id || range[0]; | ||
const fragmentId = id || range[0]; | ||
const attributes = Object.assign({}, this.pipeAttributes, { | ||
id: fragmentId, | ||
range | ||
}); | ||
this.scriptRefs.forEach(uri => { | ||
const attributes = Object.assign({}, this.getPipeAttributes(), { | ||
id: fragmentId, | ||
range | ||
}); | ||
this.stream.write( | ||
`<script data-pipe>${this.pipeInstanceName}.start(${this | ||
.index}, "${uri}", ${JSON.stringify(attributes)})</script>` | ||
|
@@ -256,11 +271,11 @@ module.exports = class Fragment extends EventEmitter { | |
const range = [this.index - this.scriptRefs.length, this.index - 1]; | ||
this.index--; | ||
const fragmentId = this.attributes.id || range[0]; | ||
const attributes = Object.assign({}, this.pipeAttributes, { | ||
id: fragmentId, | ||
range | ||
}); | ||
this.scriptRefs.reverse().forEach(uri => { | ||
const attributes = Object.assign({}, this.getPipeAttributes(), { | ||
id: fragmentId, | ||
range | ||
}); | ||
this.stream.write( | ||
`<script data-pipe>${this.pipeInstanceName}.end(${this | ||
.index--}, "${uri}", ${JSON.stringify( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: the empty shape is pointless.