mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-08 05:35:22 +00:00
try cache 1
This commit is contained in:
parent
c89a2b2378
commit
47f854ac2d
1 changed files with 37 additions and 3 deletions
|
@ -1,6 +1,9 @@
|
|||
use std::cell::UnsafeCell;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::Node;
|
||||
use num_complex::Complex64;
|
||||
use pyo3::{intern, prelude::*};
|
||||
use pyo3::prelude::*;
|
||||
use pyo3::types::{PyBytes, PyList, PyString, PyTuple};
|
||||
|
||||
pub trait ToPyo3Ast {
|
||||
|
@ -35,17 +38,48 @@ impl<T: ToPyo3Ast> ToPyo3Ast for Vec<T> {
|
|||
}
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
pub static STRING_POOL: UnsafeCell<HashMap<String, Py<PyString>>> = UnsafeCell::new(HashMap::new());
|
||||
}
|
||||
|
||||
fn cached(py: Python, s: &str) -> Py<PyString> {
|
||||
STRING_POOL.with(|cell| {
|
||||
let pool = unsafe { &mut *cell.get() };
|
||||
if let Some(s) = pool.get(s) {
|
||||
return s.clone();
|
||||
}
|
||||
let s = PyString::new(py, s);
|
||||
let key = s.to_str().unwrap();
|
||||
let s = s.into_py(py);
|
||||
pool.entry(key.to_owned()).or_insert(s).clone()
|
||||
})
|
||||
}
|
||||
|
||||
impl ToPyo3Ast for crate::Identifier {
|
||||
#[inline]
|
||||
fn to_pyo3_ast(&self, py: Python) -> PyResult<Py<PyAny>> {
|
||||
Ok(self.as_str().to_object(py))
|
||||
let s = self.as_str();
|
||||
Ok(if s.len() <= 12 {
|
||||
let py = unsafe { std::mem::transmute(py) };
|
||||
cached(py, s).to_object(py)
|
||||
// PyString::new(py, s).to_object(py)
|
||||
} else {
|
||||
self.as_str().to_object(py)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPyo3Ast for crate::String {
|
||||
#[inline]
|
||||
fn to_pyo3_ast(&self, py: Python) -> PyResult<Py<PyAny>> {
|
||||
Ok(self.as_str().to_object(py))
|
||||
let s = self.as_str();
|
||||
Ok(if s.len() <= 12 {
|
||||
let py = unsafe { std::mem::transmute(py) };
|
||||
cached(py, s).to_object(py)
|
||||
// PyString::new(py, s).to_object(py)
|
||||
} else {
|
||||
self.as_str().to_object(py)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue