From d60f0439d70153f822dedbeedf4712af8f315122 Mon Sep 17 00:00:00 2001 From: Kristoffer K Date: Mon, 1 Jun 2020 10:35:38 +0200 Subject: [PATCH] perf(core): skip await if response has already resolved (#1422) * perf(core): avoid unnecessary await when get response is in cache * chore: versions --- .yarn/versions/8c6a6df8.yml | 30 ++++++++++++++++++++++ packages/yarnpkg-core/sources/httpUtils.ts | 14 ++++++---- 2 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 .yarn/versions/8c6a6df8.yml diff --git a/.yarn/versions/8c6a6df8.yml b/.yarn/versions/8c6a6df8.yml new file mode 100644 index 000000000000..8812efe8c3c6 --- /dev/null +++ b/.yarn/versions/8c6a6df8.yml @@ -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" diff --git a/packages/yarnpkg-core/sources/httpUtils.ts b/packages/yarnpkg-core/sources/httpUtils.ts index b9626829b0b7..2226dca63a41 100644 --- a/packages/yarnpkg-core/sources/httpUtils.ts +++ b/packages/yarnpkg-core/sources/httpUtils.ts @@ -12,7 +12,7 @@ const NETWORK_CONCURRENCY = 8; const limit = plimit(NETWORK_CONCURRENCY); -const cache = new Map>>(); +const cache = new Map | Buffer>(); const globalHttpAgent = new HttpAgent({keepAlive: true}); const globalHttpsAgent = new HttpsAgent({keepAlive: true}); @@ -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; } }