-
Notifications
You must be signed in to change notification settings - Fork 11.9k
perf(@angular/ssr): cache generated inline CSS for HTML #28503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,35 @@ | ||
/** | ||
* @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.dev/license | ||
*/ | ||
|
||
/** | ||
* Generates a SHA-256 hash of the provided string. | ||
* | ||
* @param data - The input string to be hashed. | ||
* @returns A promise that resolves to the SHA-256 hash of the input, | ||
* represented as a hexadecimal string. | ||
*/ | ||
export async function sha256(data: string): Promise<string> { | ||
if (typeof crypto === 'undefined') { | ||
// TODO(alanagius): remove once Node.js version 18 is no longer supported. | ||
throw new Error( | ||
`The global 'crypto' module is unavailable. ` + | ||
`If you are running on Node.js, please ensure you are using version 20 or later, ` + | ||
`which includes built-in support for the Web Crypto module.`, | ||
); | ||
} | ||
|
||
const encodedData = new TextEncoder().encode(data); | ||
const hashBuffer = await crypto.subtle.digest('SHA-256', encodedData); | ||
const hashParts: string[] = []; | ||
|
||
for (const h of new Uint8Array(hashBuffer)) { | ||
hashParts.push(h.toString(16).padStart(2, '0')); | ||
} | ||
|
||
return hashParts.join(''); | ||
} |
This file contains hidden or 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,162 @@ | ||
/** | ||
* @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.dev/license | ||
*/ | ||
|
||
/** | ||
* Represents a node in the doubly linked list. | ||
*/ | ||
interface Node<Key, Value> { | ||
key: Key; | ||
value: Value; | ||
prev: Node<Key, Value> | undefined; | ||
next: Node<Key, Value> | undefined; | ||
} | ||
|
||
/** | ||
* A Least Recently Used (LRU) cache implementation. | ||
* | ||
* This cache stores a fixed number of key-value pairs, and when the cache exceeds its capacity, | ||
* the least recently accessed items are evicted. | ||
* | ||
* @template Key - The type of the cache keys. | ||
* @template Value - The type of the cache values. | ||
*/ | ||
export class LRUCache<Key, Value> { | ||
/** | ||
* The maximum number of items the cache can hold. | ||
*/ | ||
capacity: number; | ||
|
||
/** | ||
* Internal storage for the cache, mapping keys to their associated nodes in the linked list. | ||
*/ | ||
private readonly cache = new Map<Key, Node<Key, Value>>(); | ||
|
||
/** | ||
* Head of the doubly linked list, representing the most recently used item. | ||
*/ | ||
private head: Node<Key, Value> | undefined; | ||
|
||
/** | ||
* Tail of the doubly linked list, representing the least recently used item. | ||
*/ | ||
private tail: Node<Key, Value> | undefined; | ||
|
||
/** | ||
* Creates a new LRUCache instance. | ||
* @param capacity The maximum number of items the cache can hold. | ||
*/ | ||
constructor(capacity: number) { | ||
this.capacity = capacity; | ||
} | ||
|
||
/** | ||
* Gets the value associated with the given key. | ||
* @param key The key to retrieve the value for. | ||
* @returns The value associated with the key, or undefined if the key is not found. | ||
*/ | ||
get(key: Key): Value | undefined { | ||
const node = this.cache.get(key); | ||
if (node) { | ||
this.moveToHead(node); | ||
|
||
return node.value; | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
/** | ||
* Puts a key-value pair into the cache. | ||
* If the key already exists, the value is updated. | ||
* If the cache is full, the least recently used item is evicted. | ||
* @param key The key to insert or update. | ||
* @param value The value to associate with the key. | ||
*/ | ||
put(key: Key, value: Value): void { | ||
const cachedNode = this.cache.get(key); | ||
if (cachedNode) { | ||
// Update existing node | ||
cachedNode.value = value; | ||
this.moveToHead(cachedNode); | ||
|
||
return; | ||
} | ||
|
||
// Create a new node | ||
const newNode: Node<Key, Value> = { key, value, prev: undefined, next: undefined }; | ||
this.cache.set(key, newNode); | ||
this.addToHead(newNode); | ||
|
||
if (this.cache.size > this.capacity) { | ||
// Evict the LRU item | ||
const tail = this.removeTail(); | ||
if (tail) { | ||
this.cache.delete(tail.key); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Adds a node to the head of the linked list. | ||
* @param node The node to add. | ||
*/ | ||
private addToHead(node: Node<Key, Value>): void { | ||
node.next = this.head; | ||
node.prev = undefined; | ||
|
||
if (this.head) { | ||
this.head.prev = node; | ||
} | ||
|
||
this.head = node; | ||
|
||
if (!this.tail) { | ||
this.tail = node; | ||
} | ||
} | ||
|
||
/** | ||
* Removes a node from the linked list. | ||
* @param node The node to remove. | ||
*/ | ||
private removeNode(node: Node<Key, Value>): void { | ||
if (node.prev) { | ||
node.prev.next = node.next; | ||
} else { | ||
this.head = node.next; | ||
} | ||
|
||
if (node.next) { | ||
node.next.prev = node.prev; | ||
} else { | ||
this.tail = node.prev; | ||
} | ||
} | ||
|
||
/** | ||
* Moves a node to the head of the linked list. | ||
* @param node The node to move. | ||
*/ | ||
private moveToHead(node: Node<Key, Value>): void { | ||
this.removeNode(node); | ||
this.addToHead(node); | ||
} | ||
|
||
/** | ||
* Removes the tail node from the linked list. | ||
* @returns The removed tail node, or undefined if the list is empty. | ||
*/ | ||
private removeTail(): Node<Key, Value> | undefined { | ||
const node = this.tail; | ||
if (node) { | ||
this.removeNode(node); | ||
} | ||
|
||
return node; | ||
} | ||
} |
This file contains hidden or 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,68 @@ | ||
/** | ||
* @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.dev/license | ||
*/ | ||
|
||
import { LRUCache } from '../../src/utils/lru-cache'; | ||
|
||
describe('LRUCache', () => { | ||
let cache: LRUCache<string, number>; | ||
|
||
beforeEach(() => { | ||
cache = new LRUCache<string, number>(3); | ||
}); | ||
|
||
it('should create a cache with the correct capacity', () => { | ||
expect(cache.capacity).toBe(3); // Test internal capacity | ||
}); | ||
|
||
it('should store and retrieve a key-value pair', () => { | ||
cache.put('a', 1); | ||
expect(cache.get('a')).toBe(1); | ||
}); | ||
|
||
it('should return undefined for non-existent keys', () => { | ||
expect(cache.get('nonExistentKey')).toBeUndefined(); | ||
}); | ||
|
||
it('should remove the least recently used item when capacity is exceeded', () => { | ||
cache.put('a', 1); | ||
cache.put('b', 2); | ||
cache.put('c', 3); | ||
|
||
// Cache is full now, adding another item should evict the least recently used ('a') | ||
cache.put('d', 4); | ||
|
||
expect(cache.get('a')).toBeUndefined(); // 'a' should be evicted | ||
expect(cache.get('b')).toBe(2); // 'b', 'c', 'd' should remain | ||
expect(cache.get('c')).toBe(3); | ||
expect(cache.get('d')).toBe(4); | ||
}); | ||
|
||
it('should update the value if the key already exists', () => { | ||
cache.put('a', 1); | ||
cache.put('a', 10); // Update the value of 'a' | ||
|
||
expect(cache.get('a')).toBe(10); // 'a' should have the updated value | ||
}); | ||
|
||
it('should move the accessed key to the most recently used position', () => { | ||
cache.put('a', 1); | ||
cache.put('b', 2); | ||
cache.put('c', 3); | ||
|
||
// Access 'a', it should be moved to the most recently used position | ||
expect(cache.get('a')).toBe(1); | ||
|
||
// Adding 'd' should now evict 'b', since 'a' was just accessed | ||
cache.put('d', 4); | ||
|
||
expect(cache.get('b')).toBeUndefined(); // 'b' should be evicted | ||
expect(cache.get('a')).toBe(1); // 'a' should still be present | ||
expect(cache.get('c')).toBe(3); | ||
expect(cache.get('d')).toBe(4); | ||
}); | ||
}); |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.