[naga] Move HandleSet from compact into arena.

This commit is contained in:
Jim Blandy
2024-06-22 22:33:02 -07:00
committed by Teodor Tanasoaia
parent 34f5376517
commit aac6fc7267
6 changed files with 82 additions and 61 deletions

View File

@@ -0,0 +1,64 @@
//! The [`HandleSet`] type and associated definitions.
use crate::arena::{Arena, Handle, UniqueArena};
/// A set of `Handle<T>` values.
pub struct HandleSet<T> {
/// Bound on indexes of handles stored in this set.
len: usize,
/// `members[i]` is true if the handle with index `i` is a member.
members: bit_set::BitSet,
/// This type is indexed by values of type `T`.
as_keys: std::marker::PhantomData<T>,
}
impl<T> HandleSet<T> {
pub fn for_arena(arena: &impl ArenaType<T>) -> Self {
let len = arena.len();
Self {
len,
members: bit_set::BitSet::with_capacity(len),
as_keys: std::marker::PhantomData,
}
}
/// Return an iterator over all handles that could be made members
/// of this set.
pub fn all_possible(&self) -> impl Iterator<Item = Handle<T>> {
super::Range::full_range_from_size(self.len)
}
/// Add `handle` to the set.
pub fn insert(&mut self, handle: Handle<T>) {
self.members.insert(handle.index());
}
/// Add handles from `iter` to the set.
pub fn insert_iter(&mut self, iter: impl IntoIterator<Item = Handle<T>>) {
for handle in iter {
self.insert(handle);
}
}
pub fn contains(&self, handle: Handle<T>) -> bool {
self.members.contains(handle.index())
}
}
pub trait ArenaType<T> {
fn len(&self) -> usize;
}
impl<T> ArenaType<T> for Arena<T> {
fn len(&self) -> usize {
self.len()
}
}
impl<T: std::hash::Hash + Eq> ArenaType<T> for UniqueArena<T> {
fn len(&self) -> usize {
self.len()
}
}

View File

@@ -21,11 +21,13 @@ source code span with each element.
*/
mod handle;
mod handle_set;
mod handlevec;
mod range;
mod unique_arena;
pub use handle::{BadHandle, Handle};
pub(crate) use handle_set::HandleSet;
pub(crate) use handlevec::HandleVec;
pub use range::{BadRangeError, Range};
pub use unique_arena::UniqueArena;

View File

@@ -94,6 +94,14 @@ impl<T> Range<T> {
}
}
/// Return a range covering all handles with indices from `0` to `size`.
pub(super) fn full_range_from_size(size: usize) -> Self {
Self {
inner: 0..size as u32,
marker: Default::default(),
}
}
/// return the first and last handles included in `self`.
///
/// If `self` is an empty range, there are no handles included, so

View File

@@ -1,4 +1,4 @@
use super::handle_set_map::HandleSet;
use super::arena::HandleSet;
use super::{FunctionMap, ModuleMap};
pub struct FunctionTracer<'a> {

View File

@@ -1,62 +1,7 @@
use crate::arena::{Arena, Handle, Range, UniqueArena};
use crate::arena::{Arena, Handle, HandleSet, Range};
type Index = crate::non_max_u32::NonMaxU32;
/// A set of `Handle<T>` values.
pub struct HandleSet<T> {
/// Bound on indexes of handles stored in this set.
len: usize,
/// `members[i]` is true if the handle with index `i` is a member.
members: bit_set::BitSet,
/// This type is indexed by values of type `T`.
as_keys: std::marker::PhantomData<T>,
}
impl<T> HandleSet<T> {
pub fn for_arena(arena: &impl ArenaType<T>) -> Self {
let len = arena.len();
Self {
len,
members: bit_set::BitSet::with_capacity(len),
as_keys: std::marker::PhantomData,
}
}
/// Add `handle` to the set.
pub fn insert(&mut self, handle: Handle<T>) {
self.members.insert(handle.index());
}
/// Add handles from `iter` to the set.
pub fn insert_iter(&mut self, iter: impl IntoIterator<Item = Handle<T>>) {
for handle in iter {
self.insert(handle);
}
}
pub fn contains(&self, handle: Handle<T>) -> bool {
self.members.contains(handle.index())
}
}
pub trait ArenaType<T> {
fn len(&self) -> usize;
}
impl<T> ArenaType<T> for Arena<T> {
fn len(&self) -> usize {
self.len()
}
}
impl<T: std::hash::Hash + Eq> ArenaType<T> for UniqueArena<T> {
fn len(&self) -> usize {
self.len()
}
}
/// A map from old handle indices to new, compressed handle indices.
pub struct HandleMap<T> {
/// The indices assigned to handles in the compacted module.
@@ -74,9 +19,10 @@ impl<T: 'static> HandleMap<T> {
pub fn from_set(set: HandleSet<T>) -> Self {
let mut next_index = Index::new(0).unwrap();
Self {
new_index: (0..set.len)
.map(|index| {
if set.members.contains(index) {
new_index: set
.all_possible()
.map(|handle| {
if set.contains(handle) {
// This handle will be retained in the compacted version,
// so assign it a new index.
let this = next_index;

View File

@@ -4,8 +4,9 @@ mod handle_set_map;
mod statements;
mod types;
use crate::arena::HandleSet;
use crate::{arena, compact::functions::FunctionTracer};
use handle_set_map::{HandleMap, HandleSet};
use handle_set_map::HandleMap;
/// Remove unused types, expressions, and constants from `module`.
///