Skip to content

Commit 9af2029

Browse files
committed
Refactor PyString to hold StringData inside
1 parent babaeba commit 9af2029

File tree

7 files changed

+220
-161
lines changed

7 files changed

+220
-161
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bytecode/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ num-bigint = { version = "0.2", features = ["serde"] }
1616
num-complex = { version = "0.2", features = ["serde"] }
1717
serde = { version = "1.0", features = ["derive"] }
1818
itertools = "0.8"
19+
once_cell = "1.3"

bytecode/src/bytecode.rs

+55
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use bitflags::bitflags;
55
use itertools::Itertools;
66
use num_bigint::BigInt;
77
use num_complex::Complex64;
8+
use once_cell::sync::OnceCell;
89
use serde::{Deserialize, Serialize};
910
use std::collections::{HashMap, HashSet};
1011
use std::fmt;
@@ -649,3 +650,57 @@ pub struct FrozenModule {
649650
pub code: CodeObject,
650651
pub package: bool,
651652
}
653+
654+
#[derive(Debug, Clone)]
655+
pub struct StringData {
656+
s: Box<str>,
657+
hash: OnceCell<i64>,
658+
len: OnceCell<usize>,
659+
}
660+
661+
impl StringData {
662+
pub fn as_str(&self) -> &str {
663+
self.as_ref()
664+
}
665+
666+
pub fn hash(&self) -> i64 {
667+
*self.hash.get_or_init(|| {
668+
use std::hash::*;
669+
let mut hasher = std::collections::hash_map::DefaultHasher::new();
670+
self.s.hash(&mut hasher);
671+
hasher.finish() as i64
672+
})
673+
}
674+
675+
pub fn char_len(&self) -> usize {
676+
*self.len.get_or_init(|| self.s.chars().count())
677+
}
678+
}
679+
680+
impl AsRef<str> for StringData {
681+
fn as_ref(&self) -> &str {
682+
&self.s
683+
}
684+
}
685+
686+
impl From<Box<str>> for StringData {
687+
fn from(s: Box<str>) -> Self {
688+
StringData {
689+
s,
690+
hash: OnceCell::new(),
691+
len: OnceCell::new(),
692+
}
693+
}
694+
}
695+
696+
impl From<String> for StringData {
697+
fn from(s: String) -> Self {
698+
s.into_boxed_str().into()
699+
}
700+
}
701+
702+
impl From<&str> for StringData {
703+
fn from(s: &str) -> Self {
704+
Box::<str>::from(s).into()
705+
}
706+
}

vm/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ rustc_version_runtime = "0.1.*"
4848
statrs = "0.12.0"
4949
caseless = "0.2.1"
5050
chrono = { version = "0.4", features = ["wasmbind"] }
51-
once_cell = "1.3.1"
51+
once_cell = "1.3"
5252
lexical = "4"
5353
itertools = "0.8"
5454
hex = "0.4.0"

vm/src/obj/objbyteinner.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1325,10 +1325,6 @@ const ASCII_WHITESPACES: [u8; 6] = [0x20, 0x09, 0x0a, 0x0c, 0x0d, 0x0b];
13251325
impl PyCommonString<u8> for [u8] {
13261326
type Container = Vec<u8>;
13271327

1328-
fn with_capacity(capacity: usize) -> Self::Container {
1329-
Vec::with_capacity(capacity)
1330-
}
1331-
13321328
fn get_bytes<'a>(&'a self, range: std::ops::Range<usize>) -> &'a Self {
13331329
&self[range]
13341330
}

0 commit comments

Comments
 (0)