-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #77490 - Mark-Simulacrum:beta-backports, r=Mark-Simulacrum
[beta] backports This backports a number of PRs to beta, not all of which have been approved (yet). * Switch to environment files to change the environment on GHA #77418 * cache types during normalization #76928 * Fixing memory exhaustion when formatting short code suggestion #76598 * Issue 72408 nested closures exponential #72412 r? `@Mark-Simulacrum`
- Loading branch information
Showing
33 changed files
with
410 additions
and
94 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
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,61 @@ | ||
use crate::fx::FxHashMap; | ||
use arrayvec::ArrayVec; | ||
|
||
use std::hash::Hash; | ||
|
||
/// Small-storage-optimized implementation of a map | ||
/// made specifically for caching results. | ||
/// | ||
/// Stores elements in a small array up to a certain length | ||
/// and switches to `HashMap` when that length is exceeded. | ||
pub enum MiniMap<K, V> { | ||
Array(ArrayVec<[(K, V); 8]>), | ||
Map(FxHashMap<K, V>), | ||
} | ||
|
||
impl<K: Eq + Hash, V> MiniMap<K, V> { | ||
/// Creates an empty `MiniMap`. | ||
pub fn new() -> Self { | ||
MiniMap::Array(ArrayVec::new()) | ||
} | ||
|
||
/// Inserts or updates value in the map. | ||
pub fn insert(&mut self, key: K, value: V) { | ||
match self { | ||
MiniMap::Array(array) => { | ||
for pair in array.iter_mut() { | ||
if pair.0 == key { | ||
pair.1 = value; | ||
return; | ||
} | ||
} | ||
if let Err(error) = array.try_push((key, value)) { | ||
let mut map: FxHashMap<K, V> = array.drain(..).collect(); | ||
let (key, value) = error.element(); | ||
map.insert(key, value); | ||
*self = MiniMap::Map(map); | ||
} | ||
} | ||
MiniMap::Map(map) => { | ||
map.insert(key, value); | ||
} | ||
} | ||
} | ||
|
||
/// Return value by key if any. | ||
pub fn get(&self, key: &K) -> Option<&V> { | ||
match self { | ||
MiniMap::Array(array) => { | ||
for pair in array { | ||
if pair.0 == *key { | ||
return Some(&pair.1); | ||
} | ||
} | ||
return None; | ||
} | ||
MiniMap::Map(map) => { | ||
return map.get(key); | ||
} | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.