on master help sign in
Optimize space for DocumentId with NonZeroUsize (#1097)
67bf425Ivan Tham
helix-view/src/editor.rs+15−14
@@ -11,6 +11,7 @@ use futures_util::future;
11use std::{
11use std::{
12 collections::BTreeMap,
12 collections::BTreeMap,
13 io::stdin,
13 io::stdin,
14+ num::NonZeroUsize,
14 path::{Path, PathBuf},
15 path::{Path, PathBuf},
15 pin::Pin,
16 pin::Pin,
16 sync::Arc,
17 sync::Arc,
@@ -154,7 +155,7 @@ impl std::fmt::Debug for Motion {
154#[derive(Debug)]
155#[derive(Debug)]
155pub struct Editor {
156pub struct Editor {
156 pub tree: Tree,
157 pub tree: Tree,
157− pub next_document_id: usize,
158+ pub next_document_id: DocumentId,
158 pub documents: BTreeMap<DocumentId, Document>,
159 pub documents: BTreeMap<DocumentId, Document>,
159 pub count: Option<std::num::NonZeroUsize>,
160 pub count: Option<std::num::NonZeroUsize>,
160 pub selected_register: Option<char>,
161 pub selected_register: Option<char>,
@@ -198,7 +199,8 @@ impl Editor {
198
199
199 Self {
200 Self {
200 tree: Tree::new(area),
201 tree: Tree::new(area),
202+ // Safety: 1 is non-zero
201− next_document_id: 0,
203+ next_document_id: DocumentId::default(),
202 documents: BTreeMap::new(),
204 documents: BTreeMap::new(),
203 count: None,
205 count: None,
204 selected_register: None,
206 selected_register: None,
@@ -367,16 +369,19 @@ impl Editor {
367 self._refresh();
369 self._refresh();
368 }
370 }
369
371
372+ /// Generate an id for a new document and register it.
370− fn new_document(&mut self, mut document: Document) -> DocumentId {
373+ fn new_document(&mut self, mut doc: Document) -> DocumentId {
371− let id = DocumentId(self.next_document_id);
374+ let id = self.next_document_id;
375+ // Safety: adding 1 from 1 is fine, probably impossible to reach usize max
376+ self.next_document_id =
372− self.next_document_id += 1;
377+ DocumentId(unsafe { NonZeroUsize::new_unchecked(self.next_document_id.0.get() + 1) });
373− document.id = id;
378+ doc.id = id;
374− self.documents.insert(id, document);
379+ self.documents.insert(id, doc);
375 id
380 id
376 }
381 }
377
382
378− fn new_file_from_document(&mut self, action: Action, document: Document) -> DocumentId {
383+ fn new_file_from_document(&mut self, action: Action, doc: Document) -> DocumentId {
379− let id = self.new_document(document);
384+ let id = self.new_document(doc);
380 self.switch(id, action);
385 self.switch(id, action);
381 id
386 id
382 }
387 }
@@ -435,11 +440,7 @@ impl Editor {
435 doc.set_language_server(Some(language_server));
440 doc.set_language_server(Some(language_server));
436 }
441 }
437
442
438− let id = DocumentId(self.next_document_id);
443+ self.new_document(doc)
439− self.next_document_id += 1;
440− doc.id = id;
441− self.documents.insert(id, doc);
442− id
443 };
444 };
444
445
445 self.switch(id, action);
446 self.switch(id, action);
helix-view/src/lib.rs+12−2
@@ -12,8 +12,18 @@ pub mod theme;
12pub mod tree;
12pub mod tree;
13pub mod view;
13pub mod view;
14
14
15+use std::num::NonZeroUsize;
16+
17+// uses NonZeroUsize so Option<DocumentId> use a byte rather than two
15−#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
18+#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
16−pub struct DocumentId(usize);
19+pub struct DocumentId(NonZeroUsize);
20+
21+impl Default for DocumentId {
22+ fn default() -> DocumentId {
23+ // Safety: 1 is non-zero
24+ DocumentId(unsafe { NonZeroUsize::new_unchecked(1) })
25+ }
26+}
17
27
18slotmap::new_key_type! {
28slotmap::new_key_type! {
19 pub struct ViewId;
29 pub struct ViewId;