Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2026-05-19 - Memoize platformAndArch and optimize platform detection
**Learning:** Utility functions like `platformAndArch` that are called frequently with default arguments (`process.platform`, `process.arch`) can be memoized to avoid redundant logic. Additionally, simple string operations like `startsWith` are more efficient than regex matches for basic prefix checks.
**Action:** Implement memoization in `platformAndArch` for default arguments and replace `platform.match(/^win.+/)` with `platform.startsWith('win')`.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export type Scalars = {
ActionAuditID: { input: any; output: any; }
/** The ID for a Address. */
AddressID: { input: any; output: any; }
/** The ID for a Attestation. */
AttestationID: { input: any; output: any; }
/** The ID for a BulkDataOperation. */
BulkDataOperationID: { input: any; output: any; }
/** The ID for a BusinessUser. */
Expand Down
21 changes: 19 additions & 2 deletions packages/cli-kit/src/public/node/os.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {isUnitTest} from './context/local.js'
import {outputDebug, outputContent} from './output.js'
import {execa} from 'execa'
import {userInfo as osUserInfo} from 'os'
Expand Down Expand Up @@ -46,6 +47,9 @@ export async function username(platform: typeof process.platform = process.platf

type PlatformArch = Exclude<typeof process.arch, 'x64' | 'ia32'> | 'amd64' | '386'
type PlatformStrings = Exclude<typeof process.platform, 'win32'> | 'windows'

let memoizedPlatformAndArch: {platform: PlatformStrings; arch: PlatformArch} | undefined

/**
* Returns the platform and architecture.
* @returns Returns the current platform and architecture.
Expand All @@ -57,6 +61,12 @@ export function platformAndArch(
platform: PlatformStrings
arch: PlatformArch
} {
// Optimization: Memoize the result for the default environment (process.platform and process.arch)
// to avoid redundant checks and regex execution in hot paths. Bypassed during unit tests
// to ensure test isolation.
if (memoizedPlatformAndArch && platform === process.platform && arch === process.arch && !isUnitTest()) {
return memoizedPlatformAndArch
}
let archString: PlatformArch
if (arch === 'x64') {
archString = 'amd64'
Expand All @@ -65,8 +75,15 @@ export function platformAndArch(
} else {
archString = arch
}
const platformString = (platform.match(/^win.+/) ? 'windows' : platform) as PlatformStrings
return {platform: platformString, arch: archString}

// Optimization: startsWith('win') is faster than a regex match and safe for identifying 'win32'.
const platformString = (platform.startsWith('win') ? 'windows' : platform) as PlatformStrings

const result = {platform: platformString, arch: archString}
if (platform === process.platform && arch === process.arch && !isUnitTest()) {
memoizedPlatformAndArch = result
}
return result
}

function getEnvironmentVariable() {
Expand Down
Loading