difftreelog
feat(jrsonnet-interner) implement gc
in: master
Interned string is backed by Rc<str>, there is nothing to trace here
2 files changed
crates/jrsonnet-interner/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-interner/Cargo.toml
+++ b/crates/jrsonnet-interner/Cargo.toml
@@ -9,3 +9,4 @@
[dependencies]
serde = { version = "1.0" }
rustc-hash = "1.1.0"
+gc = { version = "0.4.1", features = ["derive"] }
\ No newline at end of file
crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth1use rustc_hash::FxHashMap;2use serde::{Deserialize, Serialize};3use std::{4 cell::RefCell,5 fmt::{self, Display},6 hash::{BuildHasherDefault, Hash, Hasher},7 ops::Deref,8 rc::Rc,9};1011#[derive(Clone, PartialOrd, Ord, Eq)]12pub struct IStr(Rc<str>);1314impl Deref for IStr {15 type Target = str;1617 fn deref(&self) -> &Self::Target {18 &self.019 }20}2122impl PartialEq for IStr {23 fn eq(&self, other: &Self) -> bool {24 // It is ok, since all IStr should be inlined into same pool25 Rc::ptr_eq(&self.0, &other.0)26 }27}2829impl PartialEq<str> for IStr {30 fn eq(&self, other: &str) -> bool {31 &self.0 as &str == other32 }33}3435impl Hash for IStr {36 fn hash<H: Hasher>(&self, state: &mut H) {37 state.write_usize(Rc::as_ptr(&self.0) as *const () as usize)38 }39}4041impl Drop for IStr {42 fn drop(&mut self) {43 // First reference - current object, second - POOL44 if Rc::strong_count(&self.0) <= 2 {45 let _result = STR_POOL.try_with(|pool| pool.borrow_mut().remove(&self.0));46 }47 }48}4950impl fmt::Debug for IStr {51 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {52 write!(f, "{:?}", &self.0)53 }54}5556impl Display for IStr {57 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {58 f.write_str(&self.0)59 }60}6162thread_local! {63 static STR_POOL: RefCell<FxHashMap<Rc<str>, ()>> = RefCell::new(FxHashMap::with_capacity_and_hasher(200, BuildHasherDefault::default()));64}6566impl From<&str> for IStr {67 fn from(str: &str) -> Self {68 IStr(STR_POOL.with(|pool| {69 let mut pool = pool.borrow_mut();70 if let Some((k, _)) = pool.get_key_value(str) {71 k.clone()72 } else {73 let rc: Rc<str> = str.into();74 pool.insert(rc.clone(), ());75 rc76 }77 }))78 }79}8081impl From<String> for IStr {82 fn from(str: String) -> Self {83 (&str as &str).into()84 }85}8687impl Serialize for IStr {88 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>89 where90 S: serde::Serializer,91 {92 (&self.0 as &str).serialize(serializer)93 }94}9596impl<'de> Deserialize<'de> for IStr {97 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>98 where99 D: serde::Deserializer<'de>,100 {101 let s = <&str>::deserialize(deserializer)?;102 Ok(s.into())103 }104}1use gc::{unsafe_empty_trace, Finalize, Trace};2use rustc_hash::FxHashMap;3use serde::{Deserialize, Serialize};4use std::{5 cell::RefCell,6 fmt::{self, Display},7 hash::{BuildHasherDefault, Hash, Hasher},8 ops::Deref,9 rc::Rc,10};1112#[derive(Clone, PartialOrd, Ord, Eq)]13pub struct IStr(Rc<str>);14impl Finalize for IStr {}15unsafe impl Trace for IStr {16 unsafe_empty_trace!();17}1819impl Deref for IStr {20 type Target = str;2122 fn deref(&self) -> &Self::Target {23 &self.024 }25}2627impl PartialEq for IStr {28 fn eq(&self, other: &Self) -> bool {29 // It is ok, since all IStr should be inlined into same pool30 Rc::ptr_eq(&self.0, &other.0)31 }32}3334impl PartialEq<str> for IStr {35 fn eq(&self, other: &str) -> bool {36 &self.0 as &str == other37 }38}3940impl Hash for IStr {41 fn hash<H: Hasher>(&self, state: &mut H) {42 state.write_usize(Rc::as_ptr(&self.0) as *const () as usize)43 }44}4546impl Drop for IStr {47 fn drop(&mut self) {48 // First reference - current object, second - POOL49 if Rc::strong_count(&self.0) <= 2 {50 let _result = STR_POOL.try_with(|pool| pool.borrow_mut().remove(&self.0));51 }52 }53}5455impl fmt::Debug for IStr {56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {57 write!(f, "{:?}", &self.0)58 }59}6061impl Display for IStr {62 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {63 f.write_str(&self.0)64 }65}6667thread_local! {68 static STR_POOL: RefCell<FxHashMap<Rc<str>, ()>> = RefCell::new(FxHashMap::with_capacity_and_hasher(200, BuildHasherDefault::default()));69}7071impl From<&str> for IStr {72 fn from(str: &str) -> Self {73 IStr(STR_POOL.with(|pool| {74 let mut pool = pool.borrow_mut();75 if let Some((k, _)) = pool.get_key_value(str) {76 k.clone()77 } else {78 let rc: Rc<str> = str.into();79 pool.insert(rc.clone(), ());80 rc81 }82 }))83 }84}8586impl From<String> for IStr {87 fn from(str: String) -> Self {88 (&str as &str).into()89 }90}9192impl Serialize for IStr {93 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>94 where95 S: serde::Serializer,96 {97 (&self.0 as &str).serialize(serializer)98 }99}100101impl<'de> Deserialize<'de> for IStr {102 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>103 where104 D: serde::Deserializer<'de>,105 {106 let s = <&str>::deserialize(deserializer)?;107 Ok(s.into())108 }109}