Skip to content

fix: dedupe profile merge entries in linear time and skip merging single-piece profiles - #1867

Open
sfdcmatt wants to merge 1 commit into
forcedotcom:mainfrom
sfdcmatt:fix/profile-merge-linear-dedupe
Open

sfdcmatt wants to merge 1 commit into
forcedotcom:mainfrom
sfdcmatt:fix/profile-merge-linear-dedupe

Conversation

@sfdcmatt

@sfdcmatt sfdcmatt commented Nov 5, 2026

Copy link
Copy Markdown

Fixes forcedotcom/cli#3681.

mergeProfiles deduped each entry with mergedValues.some(isDeepStrictEqual), which is O(n²) per tag and synchronous. On 73 profiles (103 MB) convert took 247 s with the event loop blocked throughout; with this change it takes about 8 s, output byte-identical.

  • One source piece: no merge; entries are returned as written (duplicates included, as before 13.8.3).
  • Several pieces: a per-tag Set of stableKey(entry) (JSON with sorted object keys; undefined encoded distinctly) replaces the linear isDeepStrictEqual scan. isDeepStrictEqual ignores key order, so sorting keys keeps the same equality.
  • Singleton conflict: unchanged (a different value for custom / description / loginHours / userLicense after the first still throws error_profile_singleton_conflict).

Tests to add: single-file profile with a repeated entry converts unchanged; two pieces sharing an entry merge with it once; two pieces with different <custom> still throw.


Report (also filed as forcedotcom/cli#3681)

Versions: @salesforce/source-deploy-retrieve 13.10.1 (same mergeProfiles loop since 13.8.3, when ProfileMetadataFinalizer was added; 13.8.2 and earlier copied profile files as-is). Node 26.0.0, macOS.

Repro: a project with 73 profiles in one package directory (103 MB of *.profile-meta.xml, large fieldPermissions / objectPermissions lists). ComponentSet.fromSource(profilesDir) then new MetadataConverter().convert(cs, 'metadata', { type: 'zip' }):

Version convert longest event-loop block zip bytes
13.7.0 2.1 s ~0.1 s 7,116,094
13.10.1 247 s one continuous block 7,116,094
13.10.1 + fix below 7.9-8.3 s 5.9-6.3 s 7,116,094

Output is byte-identical in all three (per-entry sha256 of the zip match).

Root cause: src/convert/convertContext/profileMetadataFinalizer.ts, mergeProfiles, line 235:

if (mergedValues.some((existing) => isDeepStrictEqual(existing, entry))) continue;

Every entry is compared with every entry already merged for that tag, synchronously, so a tag with n entries costs n²/2 isDeepStrictEqual calls. This runs even when a profile has only one source file, where there is nothing to merge.

Proposed fix: (1) when profiles.length === 1, return the single profile's entries as-is (root attributes built the same way); (2) for several pieces, dedupe with a Set keyed by a deterministic serialization of each entry (sorted object keys) instead of the linear scan. The singleton-conflict check is unchanged.

Behavior note: with (1), exact duplicate entries inside one profile file are kept, as in 13.8.2 and earlier; 13.8.3-13.10.1 drop them. Duplicates across pieces are still removed.

The remaining ~6 s block after the fix is fast-xml-parser parsing all profiles back to back in parseXml (~4 s) plus JsToXml (~1.3 s), from the finalizer parsing and re-serializing profiles that 13.8.2 streamed unchanged. Not addressed here.

@salesforce-cla

salesforce-cla Bot commented Nov 5, 2026

Copy link
Copy Markdown

Thanks for the contribution! Before we can merge this, we need @sfdcmatt to sign the Salesforce Inc. Contributor License Agreement.

@sfdcmatt
sfdcmatt force-pushed the fix/profile-merge-linear-dedupe branch from 4f77b0d to 6318fd6 Compare November 5, 2026 01:45

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Profile convert to metadata format is O(n²) per tag in mergeProfiles (13.8.3+), blocks the event loop for minutes on large profiles

1 participant