Skip to content

Commit

Permalink
refactor(formatter): Use OnceCell in Memoized
Browse files Browse the repository at this point in the history
  • Loading branch information
denbezrukov committed Sep 19, 2023
1 parent c35aa3a commit 9324d07
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions crates/biome_formatter/src/format_extensions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::prelude::*;
use std::cell::RefCell;
use std::cell::OnceCell;
use std::marker::PhantomData;
use std::ops::Deref;

Expand Down Expand Up @@ -70,7 +70,7 @@ impl<T, Context> MemoizeFormat<Context> for T where T: Format<Context> {}
#[derive(Debug)]
pub struct Memoized<F, Context> {
inner: F,
memory: RefCell<Option<FormatResult<Option<FormatElement>>>>,
memory: OnceCell<FormatResult<Option<FormatElement>>>,
options: PhantomData<Context>,
}

Expand All @@ -81,7 +81,7 @@ where
fn new(inner: F) -> Self {
Self {
inner,
memory: RefCell::new(None),
memory: OnceCell::new(),
options: PhantomData,
}
}
Expand Down Expand Up @@ -143,10 +143,7 @@ where
///
/// ```
pub fn inspect(&mut self, f: &mut Formatter<Context>) -> FormatResult<&[FormatElement]> {
let result = self
.memory
.get_mut()
.get_or_insert_with(|| f.intern(&self.inner));
let result = self.memory.get_or_init(|| f.intern(&self.inner));

match result.as_ref() {
Ok(Some(FormatElement::Interned(interned))) => Ok(interned.deref()),
Expand All @@ -162,8 +159,7 @@ where
F: Format<Context>,
{
fn fmt(&self, f: &mut Formatter<Context>) -> FormatResult<()> {
let mut memory = self.memory.borrow_mut();
let result = memory.get_or_insert_with(|| f.intern(&self.inner));
let result = self.memory.get_or_init(|| f.intern(&self.inner));

match result {
Ok(Some(elements)) => {
Expand Down

0 comments on commit 9324d07

Please sign in to comment.