Use HashMap instead of BTreeMap where possible

This shrinks the kernel from 905840 bytes to 862408 bytes.
This commit is contained in:
bjorn3
2023-12-12 22:23:16 +01:00
parent 78beae5c92
commit 2d065083df
28 changed files with 117 additions and 57 deletions
+1
View File
@@ -39,6 +39,7 @@ enum HandleKind {
ShutdownPipe,
}
// Using BTreeMap as hashbrown doesn't have a const constructor.
static HANDLES: RwLock<BTreeMap<usize, Handle>> = RwLock::new(BTreeMap::new());
static NEXT_FD: AtomicUsize = AtomicUsize::new(0);
+1
View File
@@ -18,6 +18,7 @@ struct Handle {
flags: usize,
}
// Using BTreeMap as hashbrown doesn't have a const constructor.
static HANDLES: RwLock<BTreeMap<usize, Handle>> = RwLock::new(BTreeMap::new());
/// Add to the input queue
+3 -3
View File
@@ -1,7 +1,7 @@
use core::sync::atomic::{self, AtomicUsize};
use alloc::boxed::Box;
use alloc::collections::BTreeMap;
use hashbrown::HashMap;
use spin::{Once, RwLock};
use crate::dtb::DTB_BINARY;
@@ -30,7 +30,7 @@ struct Handle {
stat: bool,
}
static HANDLES: RwLock<BTreeMap<usize, Handle>> = RwLock::new(BTreeMap::new());
static HANDLES: RwLock<HashMap<usize, Handle>> = RwLock::new(HashMap::new());
static NEXT_FD: AtomicUsize = AtomicUsize::new(0);
static DATA: Once<Box<[u8]>> = Once::new();
static SCHEME_ID: Once<SchemeId> = Once::new();
@@ -157,7 +157,7 @@ impl KernelScheme for DtbScheme {
let handles = HANDLES.read();
let handle = handles.get(&id).ok_or(Error::new(EBADF))?;
buf.copy_exactly(&match handle.kind {
HandleKind::RawData => {
HandleKind::RawData => {
let data = DATA.get().ok_or(Error::new(EBADFD))?;
Stat {
st_mode: MODE_FILE,
+1
View File
@@ -21,6 +21,7 @@ use super::{GlobalSchemes, OpenResult, CallerCtx, calc_seek_offset};
/// IRQ queues
pub(super) static COUNTS: Mutex<[usize; 224]> = Mutex::new([0; 224]);
// Using BTreeMap as hashbrown doesn't have a const constructor.
static HANDLES: RwLock<BTreeMap<usize, Handle>> = RwLock::new(BTreeMap::new());
/// These are IRQs 0..=15 (corresponding to interrupt vectors 32..=47). They are opened without the
+1
View File
@@ -12,6 +12,7 @@ use super::{KernelScheme, CallerCtx, OpenResult};
pub struct ITimerScheme;
static NEXT_ID: AtomicUsize = AtomicUsize::new(1);
// Using BTreeMap as hashbrown doesn't have a const constructor.
static HANDLES: RwLock<BTreeMap<usize, usize>> = RwLock::new(BTreeMap::new());
impl KernelScheme for ITimerScheme {
+8 -7
View File
@@ -13,6 +13,7 @@ use alloc::{
sync::Arc,
vec::Vec,
};
use hashbrown::HashMap;
use syscall::{MunmapFlags, SendFdFlags, EventFlags, SEEK_SET, SEEK_CUR, SEEK_END};
use core::sync::atomic::AtomicUsize;
use spin::{Once, RwLock, RwLockReadGuard, RwLockWriteGuard};
@@ -95,7 +96,7 @@ int_like!(SchemeId, usize);
int_like!(FileHandle, AtomicFileHandle, usize, AtomicUsize);
pub struct SchemeIter<'a> {
inner: Option<::alloc::collections::btree_map::Iter<'a, Box<str>, SchemeId>>
inner: Option<hashbrown::hash_map::Iter<'a, Box<str>, SchemeId>>
}
impl<'a> Iterator for SchemeIter<'a> {
@@ -108,8 +109,8 @@ impl<'a> Iterator for SchemeIter<'a> {
/// Scheme list type
pub struct SchemeList {
map: BTreeMap<SchemeId, KernelSchemes>,
pub(crate) names: BTreeMap<SchemeNamespace, BTreeMap<Box<str>, SchemeId>>,
map: HashMap<SchemeId, KernelSchemes>,
pub(crate) names: HashMap<SchemeNamespace, HashMap<Box<str>, SchemeId>>,
next_ns: usize,
next_id: usize,
}
@@ -117,8 +118,8 @@ impl SchemeList {
/// Create a new scheme list.
pub fn new() -> Self {
let mut list = SchemeList {
map: BTreeMap::new(),
names: BTreeMap::new(),
map: HashMap::new(),
names: HashMap::new(),
// Scheme namespaces always start at 1. 0 is a reserved namespace, the null namespace
next_ns: 1,
next_id: MAX_GLOBAL_SCHEMES,
@@ -150,7 +151,7 @@ impl SchemeList {
/// Initialize the null namespace
fn new_null(&mut self) {
let ns = SchemeNamespace(0);
self.names.insert(ns, BTreeMap::new());
self.names.insert(ns, HashMap::new());
//TODO: Only memory: is in the null namespace right now. It should be removed when
//anonymous mmap's are implemented
@@ -163,7 +164,7 @@ impl SchemeList {
fn new_ns(&mut self) -> SchemeNamespace {
let ns = SchemeNamespace(self.next_ns);
self.next_ns += 1;
self.names.insert(ns, BTreeMap::new());
self.names.insert(ns, HashMap::new());
self.insert(ns, "", |scheme_id| KernelSchemes::Root(Arc::new(RootScheme::new(ns, scheme_id)))).unwrap();
self.insert_global(ns, "event", GlobalSchemes::Event).unwrap();
+1
View File
@@ -19,6 +19,7 @@ use super::{KernelScheme, OpenResult, CallerCtx, GlobalSchemes};
static PIPE_NEXT_ID: AtomicUsize = AtomicUsize::new(1);
// TODO: SLOB?
// Using BTreeMap as hashbrown doesn't have a const constructor.
static PIPES: RwLock<BTreeMap<usize, Arc<Pipe>>> = RwLock::new(BTreeMap::new());
const MAX_QUEUE_SIZE: usize = 65536;
+1
View File
@@ -226,6 +226,7 @@ impl Handle {
pub struct ProcScheme<const FULL: bool>;
static NEXT_ID: AtomicUsize = AtomicUsize::new(1);
// Using BTreeMap as hashbrown doesn't have a const constructor.
static HANDLES: RwLock<BTreeMap<usize, Handle>> = RwLock::new(BTreeMap::new());
#[derive(PartialEq)]
+4 -4
View File
@@ -1,10 +1,10 @@
use alloc::{
boxed::Box,
collections::BTreeMap,
string::ToString,
sync::Arc,
vec::Vec,
};
use hashbrown::HashMap;
use core::str;
use core::sync::atomic::{AtomicUsize, Ordering};
use spin::{Mutex, RwLock};
@@ -54,7 +54,7 @@ pub struct RootScheme {
scheme_ns: SchemeNamespace,
scheme_id: SchemeId,
next_id: AtomicUsize,
handles: RwLock<BTreeMap<usize, Handle>>,
handles: RwLock<HashMap<usize, Handle>>,
}
impl RootScheme {
@@ -63,7 +63,7 @@ impl RootScheme {
scheme_ns,
scheme_id,
next_id: AtomicUsize::new(0),
handles: RwLock::new(BTreeMap::new()),
handles: RwLock::new(HashMap::new()),
}
}
}
@@ -298,7 +298,7 @@ impl KernelScheme for RootScheme {
}
}
}
fn kfstat(&self, file: usize, buf: UserSliceWo) -> Result<()> {
let handle = {
let handles = self.handles.read();
+1
View File
@@ -22,6 +22,7 @@ struct Handle {
flags: usize,
}
// Using BTreeMap as hashbrown doesn't have a const constructor.
static HANDLES: RwLock<BTreeMap<usize, Handle>> = RwLock::new(BTreeMap::new());
/// Add to the input queue
+1
View File
@@ -36,6 +36,7 @@ type SysFn = fn() -> Result<Vec<u8>>;
/// System information scheme
pub struct SysScheme;
static NEXT_ID: AtomicUsize = AtomicUsize::new(1);
// Using BTreeMap as hashbrown doesn't have a const constructor.
static HANDLES: RwLock<BTreeMap<usize, Handle>> = RwLock::new(BTreeMap::new());
const FILES: &[(&'static str, SysFn)] = &[
+1
View File
@@ -13,6 +13,7 @@ use crate::time;
use super::{GlobalSchemes, KernelScheme, CallerCtx, OpenResult};
static NEXT_ID: AtomicUsize = AtomicUsize::new(1);
// Using BTreeMap as hashbrown doesn't have a const constructor.
static HANDLES: RwLock<BTreeMap<usize, usize>> = RwLock::new(BTreeMap::new());
pub struct TimeScheme;
+3 -4
View File
@@ -1,13 +1,12 @@
use alloc::collections::btree_map::Entry;
use alloc::sync::{Arc, Weak};
use alloc::boxed::Box;
use alloc::collections::BTreeMap;
use alloc::vec::Vec;
use syscall::{SKMSG_FRETURNFD, SKMSG_PROVIDE_MMAP, MAP_FIXED_NOREPLACE, MunmapFlags, SKMSG_FOBTAINFD, FobtainFdFlags, SendFdFlags};
use core::mem::size_of;
use core::num::NonZeroUsize;
use core::sync::atomic::{AtomicBool, Ordering};
use core::{mem, usize};
use hashbrown::hash_map::{Entry, HashMap};
use spin::{Mutex, RwLock};
use crate::context::context::HardBlockedReason;
@@ -37,7 +36,7 @@ pub struct UserInner {
next_id: Mutex<u64>,
context: Weak<RwLock<Context>>,
todo: WaitQueue<Packet>,
states: Mutex<BTreeMap<u64, State>>,
states: Mutex<HashMap<u64, State>>,
unmounting: AtomicBool,
}
@@ -71,7 +70,7 @@ impl UserInner {
context,
todo: WaitQueue::new(),
unmounting: AtomicBool::new(false),
states: Mutex::new(BTreeMap::new()),
states: Mutex::new(HashMap::new()),
}
}