mirror of https://github.com/helix-editor/helix
Optimize space for DocumentId with NonZeroUsize (#1097)
Now Option<DocumentId> uses one byte rather than twopull/1165/head
parent
e8f800a141
commit
67bf4250ca
|
@ -11,6 +11,7 @@ use futures_util::future;
|
||||||
use std::{
|
use std::{
|
||||||
collections::BTreeMap,
|
collections::BTreeMap,
|
||||||
io::stdin,
|
io::stdin,
|
||||||
|
num::NonZeroUsize,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
|
@ -154,7 +155,7 @@ impl std::fmt::Debug for Motion {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Editor {
|
pub struct Editor {
|
||||||
pub tree: Tree,
|
pub tree: Tree,
|
||||||
pub next_document_id: usize,
|
pub next_document_id: DocumentId,
|
||||||
pub documents: BTreeMap<DocumentId, Document>,
|
pub documents: BTreeMap<DocumentId, Document>,
|
||||||
pub count: Option<std::num::NonZeroUsize>,
|
pub count: Option<std::num::NonZeroUsize>,
|
||||||
pub selected_register: Option<char>,
|
pub selected_register: Option<char>,
|
||||||
|
@ -198,7 +199,8 @@ impl Editor {
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
tree: Tree::new(area),
|
tree: Tree::new(area),
|
||||||
next_document_id: 0,
|
// Safety: 1 is non-zero
|
||||||
|
next_document_id: DocumentId::default(),
|
||||||
documents: BTreeMap::new(),
|
documents: BTreeMap::new(),
|
||||||
count: None,
|
count: None,
|
||||||
selected_register: None,
|
selected_register: None,
|
||||||
|
@ -367,16 +369,19 @@ impl Editor {
|
||||||
self._refresh();
|
self._refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_document(&mut self, mut document: Document) -> DocumentId {
|
/// Generate an id for a new document and register it.
|
||||||
let id = DocumentId(self.next_document_id);
|
fn new_document(&mut self, mut doc: Document) -> DocumentId {
|
||||||
self.next_document_id += 1;
|
let id = self.next_document_id;
|
||||||
document.id = id;
|
// Safety: adding 1 from 1 is fine, probably impossible to reach usize max
|
||||||
self.documents.insert(id, document);
|
self.next_document_id =
|
||||||
|
DocumentId(unsafe { NonZeroUsize::new_unchecked(self.next_document_id.0.get() + 1) });
|
||||||
|
doc.id = id;
|
||||||
|
self.documents.insert(id, doc);
|
||||||
id
|
id
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_file_from_document(&mut self, action: Action, document: Document) -> DocumentId {
|
fn new_file_from_document(&mut self, action: Action, doc: Document) -> DocumentId {
|
||||||
let id = self.new_document(document);
|
let id = self.new_document(doc);
|
||||||
self.switch(id, action);
|
self.switch(id, action);
|
||||||
id
|
id
|
||||||
}
|
}
|
||||||
|
@ -435,11 +440,7 @@ impl Editor {
|
||||||
doc.set_language_server(Some(language_server));
|
doc.set_language_server(Some(language_server));
|
||||||
}
|
}
|
||||||
|
|
||||||
let id = DocumentId(self.next_document_id);
|
self.new_document(doc)
|
||||||
self.next_document_id += 1;
|
|
||||||
doc.id = id;
|
|
||||||
self.documents.insert(id, doc);
|
|
||||||
id
|
|
||||||
};
|
};
|
||||||
|
|
||||||
self.switch(id, action);
|
self.switch(id, action);
|
||||||
|
|
|
@ -12,8 +12,18 @@ pub mod theme;
|
||||||
pub mod tree;
|
pub mod tree;
|
||||||
pub mod view;
|
pub mod view;
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
|
use std::num::NonZeroUsize;
|
||||||
pub struct DocumentId(usize);
|
|
||||||
|
// uses NonZeroUsize so Option<DocumentId> use a byte rather than two
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
||||||
|
pub struct DocumentId(NonZeroUsize);
|
||||||
|
|
||||||
|
impl Default for DocumentId {
|
||||||
|
fn default() -> DocumentId {
|
||||||
|
// Safety: 1 is non-zero
|
||||||
|
DocumentId(unsafe { NonZeroUsize::new_unchecked(1) })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
slotmap::new_key_type! {
|
slotmap::new_key_type! {
|
||||||
pub struct ViewId;
|
pub struct ViewId;
|
||||||
|
|
Loading…
Reference in New Issue