Skip to content

Commit

Permalink
Only stream large assets from cache
Browse files Browse the repository at this point in the history
Before, all assets were streamed from cache regardless of size, but by
marking assets with content streams as large blobs when being written to
the cache, we can default to reading the assets into memory from cache,
and only stream the assets that were marked as large blobs.
  • Loading branch information
lettertwo committed Nov 18, 2021
1 parent 6d9f276 commit 63f6f2c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/core/core/src/PackagerRunner.js
Expand Up @@ -83,6 +83,7 @@ export type BundleInfo = {|
+hashReferences: Array<string>,
+time?: number,
+cacheKeys: CacheKeyMap,
+isLargeBlob: boolean,
|};

type CacheKeyMap = {|
Expand Down Expand Up @@ -618,9 +619,11 @@ export default class PackagerRunner {
let size = 0;
let hash;
let hashReferences = [];
let isLargeBlob = false;

// TODO: don't replace hash references in binary files??
if (contents instanceof Readable) {
isLargeBlob = true;
let boundaryStr = '';
let h = new Hash();
await this.options.cache.setStream(
Expand Down Expand Up @@ -659,6 +662,7 @@ export default class PackagerRunner {
hash,
hashReferences,
cacheKeys,
isLargeBlob,
};
await this.options.cache.set(cacheKeys.info, info);
return info;
Expand Down
4 changes: 3 additions & 1 deletion packages/core/core/src/Transformation.js
Expand Up @@ -512,7 +512,9 @@ export default class Transformation {
cachedAssets.map(async (value: AssetValue) => {
let content =
value.contentKey != null
? this.options.cache.getStream(value.contentKey)
? value.isLargeBlob
? this.options.cache.getStream(value.contentKey)
: await this.options.cache.getBlob(value.contentKey)
: null;
let mapBuffer =
value.astKey != null
Expand Down
1 change: 1 addition & 0 deletions packages/core/core/src/UncommittedAsset.js
Expand Up @@ -135,6 +135,7 @@ export default class UncommittedAsset {
this.value.stats.size = size;
}

this.value.isLargeBlob = this.content instanceof Readable;
this.value.committed = true;
}

Expand Down
11 changes: 9 additions & 2 deletions packages/core/core/src/requests/WriteBundleRequest.js
Expand Up @@ -16,7 +16,7 @@ import {HASH_REF_PREFIX, HASH_REF_REGEX} from '../constants';
import nullthrows from 'nullthrows';
import path from 'path';
import {NamedBundle} from '../public/Bundle';
import {TapStream} from '@parcel/utils';
import {blobToStream, TapStream} from '@parcel/utils';
import {Readable, Transform, pipeline} from 'stream';
import {
fromProjectPath,
Expand Down Expand Up @@ -123,7 +123,14 @@ async function run({input, options, api}: RunInput) {
: {
mode: (await inputFS.stat(mainEntry.filePath)).mode,
};
let contentStream = options.cache.getStream(cacheKeys.content);
let contentStream: Readable;
if (info.isLargeBlob) {
contentStream = options.cache.getStream(cacheKeys.content);
} else {
contentStream = blobToStream(
await options.cache.getBlob(cacheKeys.content),
);
}
let size = 0;
contentStream = contentStream.pipe(
new TapStream(buf => {
Expand Down
1 change: 1 addition & 0 deletions packages/core/core/src/types.js
Expand Up @@ -180,6 +180,7 @@ export type Asset = {|
configPath?: ProjectPath,
plugin: ?PackageName,
configKeyPath?: string,
isLargeBlob?: boolean,
|};

export type InternalGlob = ProjectPath;
Expand Down

0 comments on commit 63f6f2c

Please sign in to comment.