Skip to content

Commit

Permalink
perf(core): skip await if response has already resolved (#1422)
Browse files Browse the repository at this point in the history
* perf(core): avoid unnecessary await when get response is in cache

* chore: versions
  • Loading branch information
merceyz authored Jun 1, 2020
1 parent f7371b7 commit d60f043
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
30 changes: 30 additions & 0 deletions .yarn/versions/8c6a6df8.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
releases:
"@yarnpkg/cli": prerelease
"@yarnpkg/core": prerelease

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-http"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-node-modules"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- "@yarnpkg/builder"
- "@yarnpkg/doctor"
- "@yarnpkg/pnpify"
14 changes: 9 additions & 5 deletions packages/yarnpkg-core/sources/httpUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const NETWORK_CONCURRENCY = 8;

const limit = plimit(NETWORK_CONCURRENCY);

const cache = new Map<string, Promise<Response<Buffer>>>();
const cache = new Map<string, Promise<Buffer> | Buffer>();

const globalHttpAgent = new HttpAgent({keepAlive: true});
const globalHttpsAgent = new HttpsAgent({keepAlive: true});
Expand Down Expand Up @@ -97,16 +97,20 @@ export async function get(target: string, {configuration, json, ...rest}: Option
let entry = cache.get(target);

if (!entry) {
entry = request(target, null, {configuration, ...rest});
entry = request(target, null, {configuration, ...rest}).then(response => {
cache.set(target, response.body);
return response.body;
});
cache.set(target, entry);
}

const response = await entry;
if (Buffer.isBuffer(entry) === false)
entry = await entry;

if (json) {
return JSON.parse(response.body.toString());
return JSON.parse(entry.toString());
} else {
return response.body;
return entry;
}
}

Expand Down

0 comments on commit d60f043

Please sign in to comment.