Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(opt): Small String Optimisations (SSO) #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions som-interpreter-bc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ num-traits = "0.2.11"

# random numbers
rand = "0.7.3"

# small string optimisation
smartstring = "0.2.3"
5 changes: 3 additions & 2 deletions som-interpreter-bc/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::rc::{Rc, Weak};

use indexmap::{IndexMap, IndexSet};
use num_bigint::BigInt;
use smartstring::alias::String as SmallString;

use som_core::ast;
use som_core::bytecode::Bytecode;
Expand All @@ -21,7 +22,7 @@ use crate::SOMRef;
#[derive(Debug, Clone)]
pub enum Literal {
Symbol(Interned),
String(Rc<String>),
String(SmallString),
Double(f64),
Integer(i64),
BigInteger(BigInt),
Expand Down Expand Up @@ -290,7 +291,7 @@ impl MethodCodegen for ast::Expression {
ast::Literal::Symbol(val) => {
Literal::Symbol(ctxt.intern_symbol(val.as_str()))
}
ast::Literal::String(val) => Literal::String(Rc::new(val.clone())),
ast::Literal::String(val) => Literal::String(val.into()),
ast::Literal::Double(val) => Literal::Double(*val),
ast::Literal::Integer(val) => Literal::Integer(*val),
ast::Literal::BigInteger(val) => Literal::BigInteger(val.parse().unwrap()),
Expand Down
3 changes: 1 addition & 2 deletions som-interpreter-bc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#![warn(missing_docs)]

use std::path::PathBuf;
use std::rc::Rc;

use anyhow::anyhow;
use structopt::StructOpt;
Expand Down Expand Up @@ -76,7 +75,7 @@ fn main() -> anyhow::Result<()> {

let args = std::iter::once(String::from(file_stem))
.chain(opts.args.iter().cloned())
.map(Rc::new)
.map(Into::into)
.map(Value::String)
.collect();

Expand Down
4 changes: 1 addition & 3 deletions som-interpreter-bc/src/primitives/double.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::rc::Rc;

use crate::interpreter::Interpreter;
use crate::primitives::PrimitiveFn;
use crate::universe::Universe;
Expand Down Expand Up @@ -49,7 +47,7 @@ fn as_string(interpreter: &mut Interpreter, _: &mut Universe) {
frame
.borrow_mut()
.stack
.push(Value::String(Rc::new(value.to_string())));
.push(Value::String(value.to_string().into()));
}

fn as_integer(interpreter: &mut Interpreter, _: &mut Universe) {
Expand Down
7 changes: 4 additions & 3 deletions som-interpreter-bc/src/primitives/integer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::rc::Rc;

use num_bigint::{BigInt, Sign};
use num_traits::ToPrimitive;
use rand::distributions::Uniform;
Expand Down Expand Up @@ -71,7 +69,10 @@ fn as_string(interpreter: &mut Interpreter, _: &mut Universe) {
};

{
frame.borrow_mut().stack.push(Value::String(Rc::new(value)));
frame
.borrow_mut()
.stack
.push(Value::String(value.to_string().into()));
return;
}
}
Expand Down
5 changes: 2 additions & 3 deletions som-interpreter-bc/src/primitives/string.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::hash_map::DefaultHasher;
use std::convert::TryFrom;
use std::hash::Hasher;
use std::rc::Rc;

use crate::interpreter::Interpreter;
use crate::primitives::PrimitiveFn;
Expand Down Expand Up @@ -144,7 +143,7 @@ fn concatenate(interpreter: &mut Interpreter, universe: &mut Universe) {
frame
.borrow_mut()
.stack
.push(Value::String(Rc::new(format!("{}{}", s1, s2))))
.push(Value::String(format!("{}{}", s1, s2).into()))
}

fn as_symbol(interpreter: &mut Interpreter, universe: &mut Universe) {
Expand Down Expand Up @@ -196,7 +195,7 @@ fn prim_substring_from_to(interpreter: &mut Interpreter, universe: &mut Universe
(_, _, _) => panic!("'{}': wrong types", SIGNATURE),
};

let string = Rc::new(value.chars().skip(from).take(to - from).collect());
let string = value.chars().skip(from).take(to - from).collect();

frame.borrow_mut().stack.push(Value::String(string))
}
Expand Down
8 changes: 3 additions & 5 deletions som-interpreter-bc/src/primitives/symbol.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::rc::Rc;

use crate::interpreter::Interpreter;
use crate::primitives::PrimitiveFn;
use crate::universe::Universe;
Expand All @@ -15,9 +13,9 @@ fn as_string(interpreter: &mut Interpreter, universe: &mut Universe) {
Value::Symbol(sym) => sym,
]);

frame.borrow_mut().stack.push(Value::String(Rc::new(
universe.lookup_symbol(sym).to_string(),
)));
frame.borrow_mut().stack.push(Value::String(
universe.lookup_symbol(sym).to_string().into(),
));
}

/// Search for a primitive matching the given signature.
Expand Down
3 changes: 2 additions & 1 deletion som-interpreter-bc/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fmt;
use std::rc::Rc;

use num_bigint::BigInt;
use smartstring::alias::String as SmallString;

use crate::block::Block;
use crate::class::Class;
Expand Down Expand Up @@ -29,7 +30,7 @@ pub enum Value {
/// An interned symbol value.
Symbol(Interned),
/// A string value.
String(Rc<String>),
String(SmallString),
/// An array of values.
Array(SOMRef<Vec<Self>>),
/// A block value, ready to be evaluated.
Expand Down