@@ -1,4 +1,6 @@
11use std::{
2+ borrow::{Borrow, BorrowMut},
3+ cell::Cell,
24 cmp::{self, Ordering},
35 rc::Rc,
46 str::FromStr,
@@ -18,7 +20,7 @@ pub enum Noun {
1820 Cell {
1921 p: Rc<Noun>,
2022 q: Rc<Noun>,
21− hash: Hash,
23+ hash: Cell<Option<Hash>>,
2224 },
2325}
2426
@@ -151,7 +153,14 @@ impl Noun {
151153
152154 pub fn hash(self: &Self) -> Hash {
153155 match self {
154− Noun::Cell { hash, .. } => *hash,
156+ Noun::Cell { hash, p, q } => match hash.get() {
157+ Some(x) => x.clone(),
158+ None => {
159+ let hash_value = hash_pair(p.clone(), q.clone());
160+ hash.replace(Some(hash_value));
161+ hash_value
162+ }
163+ },
155164 Noun::Atom(a) => xxh3_128(&*a.to_bytes_le()),
156165 }
157166 }
@@ -348,7 +357,7 @@ pub fn cell(p: Rc<Noun>, q: Rc<Noun>) -> Rc<Noun> {
348357 Rc::new(Noun::Cell {
349358 p: p.clone(),
350359 q: q.clone(),
351− hash: hash_pair(p, q),
360+ hash: Cell::new(None),
352361 })
353362}
354363