Refine immutable strings.
This commit is contained in:
parent
99ea2b33c9
commit
fca140ef55
@ -105,3 +105,54 @@ fn bench_eval_call(bench: &mut Bencher) {
|
||||
|
||||
bench.iter(|| engine.eval::<bool>(script).unwrap());
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_eval_loop_number(bench: &mut Bencher) {
|
||||
let script = r#"
|
||||
let s = 0;
|
||||
for x in range(0, 10000) {
|
||||
s += 1;
|
||||
}
|
||||
"#;
|
||||
|
||||
let mut engine = Engine::new();
|
||||
engine.set_optimization_level(OptimizationLevel::None);
|
||||
|
||||
let ast = engine.compile(script).unwrap();
|
||||
|
||||
bench.iter(|| engine.consume_ast(&ast).unwrap());
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_eval_loop_strings_build(bench: &mut Bencher) {
|
||||
let script = r#"
|
||||
let s = 0;
|
||||
for x in range(0, 10000) {
|
||||
s += "x";
|
||||
}
|
||||
"#;
|
||||
|
||||
let mut engine = Engine::new();
|
||||
engine.set_optimization_level(OptimizationLevel::None);
|
||||
|
||||
let ast = engine.compile(script).unwrap();
|
||||
|
||||
bench.iter(|| engine.consume_ast(&ast).unwrap());
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_eval_loop_strings_no_build(bench: &mut Bencher) {
|
||||
let script = r#"
|
||||
let s = "hello";
|
||||
for x in range(0, 10000) {
|
||||
s += "";
|
||||
}
|
||||
"#;
|
||||
|
||||
let mut engine = Engine::new();
|
||||
engine.set_optimization_level(OptimizationLevel::None);
|
||||
|
||||
let ast = engine.compile(script).unwrap();
|
||||
|
||||
bench.iter(|| engine.consume_ast(&ast).unwrap());
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
//! Helper module which defines the `Any` trait to to allow dynamic value handling.
|
||||
|
||||
use crate::fn_native::shared_unwrap;
|
||||
use crate::parser::{ImmutableString, INT};
|
||||
use crate::r#unsafe::{unsafe_cast_box, unsafe_try_cast};
|
||||
|
||||
@ -20,7 +21,9 @@ use crate::stdlib::{
|
||||
boxed::Box,
|
||||
collections::HashMap,
|
||||
fmt,
|
||||
rc::Rc,
|
||||
string::String,
|
||||
sync::Arc,
|
||||
vec::Vec,
|
||||
};
|
||||
|
||||
@ -585,7 +588,7 @@ impl Dynamic {
|
||||
/// Returns the name of the actual type if the cast fails.
|
||||
pub fn take_string(self) -> Result<String, &'static str> {
|
||||
match self.0 {
|
||||
Union::Str(s) => Ok((*s).clone()),
|
||||
Union::Str(s) => Ok(shared_unwrap(s).unwrap_or_else(|s| (*s).clone())),
|
||||
_ => Err(self.type_name()),
|
||||
}
|
||||
}
|
||||
|
@ -1512,8 +1512,9 @@ impl Engine {
|
||||
|
||||
let mut args: StaticVec<_> = arg_values.iter_mut().collect();
|
||||
|
||||
if name == KEYWORD_EVAL && args.len() == 1 && args.get(0).is::<String>() {
|
||||
let hash_fn = calc_fn_hash(empty(), name, 1, once(TypeId::of::<String>()));
|
||||
if name == KEYWORD_EVAL && args.len() == 1 && args.get(0).is::<ImmutableString>() {
|
||||
let hash_fn =
|
||||
calc_fn_hash(empty(), name, 1, once(TypeId::of::<ImmutableString>()));
|
||||
|
||||
if !self.has_override(lib, (hash_fn, *hash)) {
|
||||
// eval - only in function call style
|
||||
|
@ -19,6 +19,28 @@ pub type Shared<T> = Rc<T>;
|
||||
#[cfg(feature = "sync")]
|
||||
pub type Shared<T> = Arc<T>;
|
||||
|
||||
pub fn shared_make_mut<T: Clone>(value: &mut Shared<T>) -> &mut T {
|
||||
#[cfg(not(feature = "sync"))]
|
||||
{
|
||||
Rc::make_mut(value)
|
||||
}
|
||||
#[cfg(feature = "sync")]
|
||||
{
|
||||
Arc::make_mut(value)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn shared_unwrap<T: Clone>(value: Shared<T>) -> Result<T, Shared<T>> {
|
||||
#[cfg(not(feature = "sync"))]
|
||||
{
|
||||
Rc::try_unwrap(value)
|
||||
}
|
||||
#[cfg(feature = "sync")]
|
||||
{
|
||||
Arc::try_unwrap(value)
|
||||
}
|
||||
}
|
||||
|
||||
pub type FnCallArgs<'a> = [&'a mut Dynamic];
|
||||
|
||||
#[cfg(not(feature = "sync"))]
|
||||
|
@ -1,5 +1,6 @@
|
||||
use crate::def_package;
|
||||
use crate::engine::{FUNC_TO_STRING, KEYWORD_DEBUG, KEYWORD_PRINT};
|
||||
use crate::fn_native::shared_make_mut;
|
||||
use crate::module::FuncReturn;
|
||||
use crate::parser::{ImmutableString, INT};
|
||||
|
||||
@ -41,8 +42,8 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin
|
||||
lib.set_fn_1(KEYWORD_PRINT, |_: ()| Ok("".to_string()));
|
||||
lib.set_fn_1(FUNC_TO_STRING, |_: ()| Ok("".to_string()));
|
||||
|
||||
lib.set_fn_1(KEYWORD_PRINT, |s: ImmutableString| Ok(s.clone()));
|
||||
lib.set_fn_1(FUNC_TO_STRING, |s: ImmutableString| Ok(s.clone()));
|
||||
lib.set_fn_1(KEYWORD_PRINT, |s: ImmutableString| Ok(s));
|
||||
lib.set_fn_1(FUNC_TO_STRING, |s: ImmutableString| Ok(s));
|
||||
|
||||
reg_op!(lib, KEYWORD_DEBUG, to_debug, INT, bool, (), char, ImmutableString);
|
||||
|
||||
@ -81,6 +82,10 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin
|
||||
lib.set_fn_2(
|
||||
"+",
|
||||
|s: ImmutableString, ch: char| {
|
||||
if s.is_empty() {
|
||||
return Ok(ch.to_string().into());
|
||||
}
|
||||
|
||||
let mut s = (*s).clone();
|
||||
s.push(ch);
|
||||
Ok(s)
|
||||
@ -89,23 +94,28 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin
|
||||
lib.set_fn_2(
|
||||
"+",
|
||||
|s:ImmutableString, s2:ImmutableString| {
|
||||
if s.is_empty() {
|
||||
return Ok(s2);
|
||||
} else if s2.is_empty() {
|
||||
return Ok(s);
|
||||
}
|
||||
|
||||
let mut s = (*s).clone();
|
||||
s.push_str(s2.as_str());
|
||||
Ok(s)
|
||||
Ok(s.into())
|
||||
},
|
||||
);
|
||||
lib.set_fn_2_mut("append", |s: &mut ImmutableString, ch: char| {
|
||||
let mut copy = (**s).clone();
|
||||
copy.push(ch);
|
||||
*s = copy.into();
|
||||
shared_make_mut(s).push(ch);
|
||||
Ok(())
|
||||
});
|
||||
lib.set_fn_2_mut(
|
||||
"append",
|
||||
|s: &mut ImmutableString, s2: ImmutableString| {
|
||||
let mut copy = (**s).clone();
|
||||
copy.push_str(s2.as_str());
|
||||
*s = copy.into();
|
||||
if !s2.is_empty() {
|
||||
shared_make_mut(s).push_str(s2.as_str());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
);
|
||||
|
@ -1,4 +1,5 @@
|
||||
use crate::def_package;
|
||||
use crate::fn_native::shared_make_mut;
|
||||
use crate::module::FuncReturn;
|
||||
use crate::parser::{ImmutableString, INT};
|
||||
use crate::utils::StaticVec;
|
||||
@ -46,23 +47,19 @@ fn sub_string(s: ImmutableString, start: INT, len: INT) -> FuncReturn<ImmutableS
|
||||
.into())
|
||||
}
|
||||
fn crop_string(s: &mut ImmutableString, start: INT, len: INT) -> FuncReturn<()> {
|
||||
let mut copy = (**s).clone();
|
||||
|
||||
let offset = if copy.is_empty() || len <= 0 {
|
||||
copy.clear();
|
||||
*s = copy.into();
|
||||
let offset = if s.is_empty() || len <= 0 {
|
||||
shared_make_mut(s).clear();
|
||||
return Ok(());
|
||||
} else if start < 0 {
|
||||
0
|
||||
} else if (start as usize) >= copy.chars().count() {
|
||||
copy.clear();
|
||||
*s = copy.into();
|
||||
} else if (start as usize) >= s.chars().count() {
|
||||
shared_make_mut(s).clear();
|
||||
return Ok(());
|
||||
} else {
|
||||
start as usize
|
||||
};
|
||||
|
||||
let chars: StaticVec<_> = copy.chars().collect();
|
||||
let chars: StaticVec<_> = s.chars().collect();
|
||||
|
||||
let len = if offset + (len as usize) > chars.len() {
|
||||
chars.len() - offset
|
||||
@ -70,12 +67,10 @@ fn crop_string(s: &mut ImmutableString, start: INT, len: INT) -> FuncReturn<()>
|
||||
len as usize
|
||||
};
|
||||
|
||||
*s = chars
|
||||
.iter()
|
||||
.skip(offset)
|
||||
.take(len)
|
||||
.collect::<String>()
|
||||
.into();
|
||||
let copy = shared_make_mut(s);
|
||||
copy.clear();
|
||||
copy.extend(chars.iter().skip(offset).take(len));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -171,21 +166,17 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
|
||||
},
|
||||
);
|
||||
lib.set_fn_1_mut("clear", |s: &mut ImmutableString| {
|
||||
*s = "".to_string().into();
|
||||
shared_make_mut(s).clear();
|
||||
Ok(())
|
||||
});
|
||||
lib.set_fn_2_mut("append", |s: &mut ImmutableString, ch: char| {
|
||||
let mut copy = (**s).clone();
|
||||
copy.push(ch);
|
||||
*s = copy.into();
|
||||
shared_make_mut(s).push(ch);
|
||||
Ok(())
|
||||
});
|
||||
lib.set_fn_2_mut(
|
||||
"append",
|
||||
|s: &mut ImmutableString, add: ImmutableString| {
|
||||
let mut copy = (**s).clone();
|
||||
copy.push_str(add.as_str());
|
||||
*s = copy.into();
|
||||
shared_make_mut(s).push_str(add.as_str());
|
||||
Ok(())
|
||||
}
|
||||
);
|
||||
@ -205,10 +196,13 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
|
||||
lib.set_fn_2_mut(
|
||||
"truncate",
|
||||
|s: &mut ImmutableString, len: INT| {
|
||||
if len >= 0 {
|
||||
*s = (**s).clone().chars().take(len as usize).collect::<String>().into();
|
||||
if len > 0 {
|
||||
let chars: StaticVec<_> = s.chars().collect();
|
||||
let copy = shared_make_mut(s);
|
||||
copy.clear();
|
||||
copy.extend(chars.into_iter().take(len as usize));
|
||||
} else {
|
||||
*s = "".to_string().into();
|
||||
shared_make_mut(s).clear();
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
@ -216,11 +210,10 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
|
||||
lib.set_fn_3_mut(
|
||||
"pad",
|
||||
|s: &mut ImmutableString, len: INT, ch: char| {
|
||||
let mut copy = (**s).clone();
|
||||
let copy = shared_make_mut(s);
|
||||
for _ in 0..copy.chars().count() - len as usize {
|
||||
copy.push(ch);
|
||||
}
|
||||
*s = copy.into();
|
||||
Ok(())
|
||||
},
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user