Skip to content

Commit f8b1c65

Browse files
committed
PyStrInterned
1 parent bbc3136 commit f8b1c65

File tree

5 files changed

+47
-47
lines changed

5 files changed

+47
-47
lines changed

vm/src/builtins/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub(crate) mod bool_;
5959
pub use bool_::PyBool;
6060
#[path = "str.rs"]
6161
pub(crate) mod pystr;
62-
pub use pystr::{PyStr, PyStrRef};
62+
pub use pystr::{PyStr, PyStrInterned, PyStrRef};
6363
#[path = "super.rs"]
6464
pub(crate) mod super_;
6565
pub use super_::PySuper;

vm/src/builtins/str.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{
99
convert::{ToPyException, ToPyObject},
1010
format::{FormatSpec, FormatString, FromTemplate},
1111
function::{ArgIterable, FuncArgs, OptionalArg, OptionalOption, PyComparisonValue},
12+
intern::PyInterned,
1213
protocol::{PyIterReturn, PyMappingMethods, PySequenceMethods},
1314
sequence::SequenceOp,
1415
sliceable::{SequenceIndex, SliceableSequenceOp},
@@ -227,7 +228,7 @@ impl IntoPyStrRef for &str {
227228
}
228229
}
229230

230-
impl IntoPyStrRef for &'static crate::intern::PyStrInterned {
231+
impl IntoPyStrRef for &'static PyStrInterned {
231232
#[inline]
232233
fn into_pystr_ref(self, _vm: &VirtualMachine) -> PyRef<PyStr> {
233234
self.to_owned()
@@ -1741,3 +1742,27 @@ impl<'s> AnyStr<'s> for str {
17411742
splited
17421743
}
17431744
}
1745+
1746+
/// The unique reference of interned PyStr
1747+
/// Always intended to be used as a static reference
1748+
pub type PyStrInterned = PyInterned<PyStr>;
1749+
1750+
impl PyStrInterned {
1751+
#[inline]
1752+
pub fn to_exact(&'static self) -> PyRefExact<PyStr> {
1753+
unsafe { PyRefExact::new_unchecked(self.to_owned()) }
1754+
}
1755+
}
1756+
1757+
impl std::fmt::Display for PyStrInterned {
1758+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1759+
std::fmt::Display::fmt(self.as_str(), f)
1760+
}
1761+
}
1762+
1763+
impl AsRef<str> for PyStrInterned {
1764+
#[inline(always)]
1765+
fn as_ref(&self) -> &str {
1766+
self.as_str()
1767+
}
1768+
}

vm/src/intern.rs

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
builtins::{PyStr, PyTypeRef},
2+
builtins::{PyStr, PyStrInterned, PyTypeRef},
33
common::lock::PyRwLock,
44
convert::ToPyObject,
55
AsObject, Py, PyExact, PyObject, PyObjectRef, PyPayload, PyRef, PyRefExact, VirtualMachine,
@@ -42,17 +42,16 @@ impl StringPool {
4242
let cache = CachedPyStrRef { inner: s };
4343
let inserted = zelf.inner.write().insert(cache.clone());
4444
if inserted {
45-
let interned = unsafe { PyStrInterned::borrow_cache(&cache) };
45+
let interned = unsafe { cache.as_interned_str() };
4646
unsafe { interned.as_object().mark_intern() };
4747
interned
4848
} else {
4949
unsafe {
50-
PyStrInterned::borrow_cache(
51-
zelf.inner
52-
.read()
53-
.get(cache.as_ref())
54-
.expect("inserted is false"),
55-
)
50+
zelf.inner
51+
.read()
52+
.get(cache.as_ref())
53+
.expect("inserted is false")
54+
.as_interned_str()
5655
}
5756
}
5857
}
@@ -68,7 +67,7 @@ impl StringPool {
6867
self.inner
6968
.read()
7069
.get(s.as_ref())
71-
.map(|cached| unsafe { PyStrInterned::borrow_cache(cached) })
70+
.map(|cached| unsafe { cached.as_interned_str() })
7271
}
7372
}
7473

@@ -107,6 +106,13 @@ impl AsRef<str> for CachedPyStrRef {
107106
}
108107

109108
impl CachedPyStrRef {
109+
/// # Safety
110+
/// the given cache must be alive while returned reference is alive
111+
#[inline]
112+
unsafe fn as_interned_str(&self) -> &'static PyStrInterned {
113+
std::mem::transmute_copy(self)
114+
}
115+
110116
#[inline]
111117
fn as_str(&self) -> &str {
112118
self.inner.as_str()
@@ -188,37 +194,6 @@ impl<T: PyPayload> ToPyObject for &'static PyInterned<T> {
188194
}
189195
}
190196

191-
/// The unique reference of interned PyStr
192-
/// Always intended to be used as a static reference
193-
pub type PyStrInterned = PyInterned<PyStr>;
194-
195-
impl PyStrInterned {
196-
/// # Safety
197-
/// the given cache must be alive while returned reference is alive
198-
#[inline]
199-
unsafe fn borrow_cache(cache: &CachedPyStrRef) -> &'static Self {
200-
std::mem::transmute_copy(cache)
201-
}
202-
203-
#[inline]
204-
pub fn to_exact(&'static self) -> PyRefExact<PyStr> {
205-
unsafe { PyRefExact::new_unchecked(self.to_owned()) }
206-
}
207-
}
208-
209-
impl std::fmt::Display for PyStrInterned {
210-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
211-
std::fmt::Display::fmt(self.as_str(), f)
212-
}
213-
}
214-
215-
impl AsRef<str> for PyStrInterned {
216-
#[inline(always)]
217-
fn as_ref(&self) -> &str {
218-
self.as_str()
219-
}
220-
}
221-
222197
mod sealed {
223198
use crate::{
224199
builtins::PyStr,

vm/src/stdlib/sys.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,8 @@ mod sys {
512512
}
513513

514514
#[pyfunction]
515-
fn intern(s: PyRefExact<PyStr>, vm: &VirtualMachine) -> PyRefExact<PyStr> {
516-
vm.ctx.intern_str(s).to_exact()
515+
fn intern(s: PyRefExact<PyStr>, vm: &VirtualMachine) -> PyRef<PyStr> {
516+
vm.ctx.intern_str(s).to_owned()
517517
}
518518

519519
#[pyattr]

vm/src/vm/context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ use crate::{
77
object, pystr,
88
type_::PyAttributes,
99
PyBaseException, PyComplex, PyDict, PyDictRef, PyEllipsis, PyFloat, PyFrozenSet, PyInt,
10-
PyIntRef, PyList, PyListRef, PyNone, PyNotImplemented, PyStr, PyTuple, PyTupleRef, PyType,
11-
PyTypeRef,
10+
PyIntRef, PyList, PyListRef, PyNone, PyNotImplemented, PyStr, PyStrInterned, PyTuple,
11+
PyTupleRef, PyType, PyTypeRef,
1212
},
1313
class::{PyClassImpl, StaticType},
1414
exceptions,
1515
function::IntoPyNativeFunc,
16-
intern::{Internable, MaybeInterned, PyStrInterned, StringPool},
16+
intern::{Internable, MaybeInterned, StringPool},
1717
object::{PyObjectPayload, PyObjectRef, PyPayload, PyRef},
1818
types::{PyTypeFlags, PyTypeSlots, TypeZoo},
1919
};

0 commit comments

Comments
 (0)