Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(core): optimize memory allocation when reconcilling lists (#52245)
This change replaces the implementation of the multi-map used to store detached views while reconciling lists. The new implementation optimizes memory allocation for such map and avoid arrays allocation when there are no duplicated keys. PR Close #52245
- Loading branch information
1 parent
f88b77b
commit 044cb55
Showing
3 changed files
with
140 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {UniqueValueMultiKeyMap} from '@angular/core/src/render3/list_reconciliation'; | ||
|
||
describe('MultiMap', () => { | ||
it('should set, get and remove items with duplicated keys', () => { | ||
const map = new UniqueValueMultiKeyMap(); | ||
|
||
map.set('k', 'v1'); | ||
map.set('k', 'v2'); | ||
|
||
expect(map.has('k')).toBeTrue(); | ||
expect(map.get('k')).toBe('v1'); | ||
|
||
map.delete('k'); | ||
expect(map.has('k')).toBeTrue(); | ||
expect(map.get('k')).toBe('v2'); | ||
|
||
map.delete('k'); | ||
expect(map.has('k')).toBeFalse(); | ||
}); | ||
|
||
it('should set, get and remove items without duplicated keys', () => { | ||
const map = new UniqueValueMultiKeyMap(); | ||
|
||
map.set('k', 'v1'); | ||
|
||
expect(map.has('k')).toBeTrue(); | ||
expect(map.get('k')).toBe('v1'); | ||
|
||
map.delete('k'); | ||
expect(map.has('k')).toBeFalse(); | ||
}); | ||
|
||
it('should iterate with forEach', () => { | ||
const map = new UniqueValueMultiKeyMap<string, string>(); | ||
|
||
map.set('km', 'v1'); | ||
map.set('km', 'v2'); | ||
map.set('ks', 'v'); | ||
|
||
const items: string[][] = []; | ||
|
||
map.forEach((v, k) => items.push([v, k])); | ||
expect(items).toEqual([['v1', 'km'], ['v2', 'km'], ['v', 'ks']]); | ||
}); | ||
|
||
it('should throw upon detecting duplicate values', () => { | ||
const map = new UniqueValueMultiKeyMap(); | ||
|
||
map.set('k', 'v'); | ||
expect(() => { | ||
map.set('k', 'v'); | ||
}).toThrowError(/Detected a duplicated value v for the key k/); | ||
}); | ||
}); |