Skip to content

Commit 70edfd0

Browse files
committed
Start on name pooling #48
1 parent 7abf173 commit 70edfd0

File tree

7 files changed

+140
-102
lines changed

7 files changed

+140
-102
lines changed

src/mango/io/util.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
#[macro_use]
12
use regex::Regex;
23
use std::cell::RefCell;
34
use std::collections::HashMap;
45

6+
// TODO: Use regex! instead of Regex::new to check at compile time. (Also its not on heap). This is actually slower according to 2y old benchmark though. But semantically makes more sense.
7+
thread_local!{
8+
static re: Regex = Regex::new("abc");
9+
}
10+
511
pub struct RegexCache {
612
cache: HashMap<String, Regex>,
713
}

src/mango/towasm/control/block.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use mango::towasm::collect::Statement;
22
use mango::towasm::control::Label;
33
use mango::towasm::util::Name;
4+
use mango::towasm::util::NamePool;
5+
use mango::towasm::util::NamePool;
46
use mango::towasm::Wasm;
57
use std::fs::File;
68
use std::io;
@@ -48,10 +50,10 @@ impl Block {
4850
F: FnOnce(Label) -> Vec<Box<Statement>>,
4951
{
5052
// todo: determine name automatically
51-
Block::new_named(Name::new("b".to_owned()).unwrap(), statements_gen)
53+
Block::new_named(NamePool.borrow_mut().anonymous_prefix("block_"), statements_gen)
5254
}
5355

54-
pub fn new_named<F>(name: Rc<Name>, statements_gen: F) -> Box<Self>
56+
pub fn new_named<F>(name: Name, statements_gen: F) -> Box<Self>
5557
where
5658
F: FnOnce(Label) -> Vec<Box<Statement>>,
5759
{

src/mango/towasm/util/anonymous.rs

+43-43
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
use mango::towasm::Wasm;
2-
use std::fs::File;
3-
use std::io;
4-
use std::rc::Rc;
5-
6-
pub enum AnonymousName {
7-
Pending(AnonymousToken),
8-
Resolved(AnonymousChoice),
9-
}
10-
11-
pub struct AnonymousToken {
12-
13-
}
14-
15-
pub struct AnonymousChoice {
16-
17-
}
18-
19-
pub struct AnonymousTokenPool {
20-
name_tokens: Vec<>
21-
}
22-
23-
impl Name {
24-
pub fn new(name: String) -> Option<Rc<Self>> {
25-
// todo: filter out illegal names
26-
assert!(!name.starts_with("$"));
27-
return Some(Rc::new(Name { name }));
28-
}
29-
30-
pub fn pure_name(&self) -> String {
31-
return self.name.to_owned();
32-
}
33-
}
34-
35-
impl Wasm for Name {
36-
fn as_wat(&self) -> String {
37-
format!("${}", self.name)
38-
}
39-
40-
fn write_wasm(&self, file: &mut File) -> io::Result<()> {
41-
unimplemented!()
42-
}
43-
}
1+
//use mango::towasm::Wasm;
2+
//use std::fs::File;
3+
//use std::io;
4+
//use std::rc::Rc;
5+
//
6+
//pub enum AnonymousName {
7+
// Pending(AnonymousToken),
8+
// Resolved(AnonymousChoice),
9+
//}
10+
//
11+
//pub struct AnonymousToken {
12+
//
13+
//}
14+
//
15+
//pub struct AnonymousChoice {
16+
//
17+
//}
18+
//
19+
//pub struct AnonymousTokenPool {
20+
// name_tokens: Vec<>
21+
//}
22+
//
23+
//impl Name {
24+
// pub fn new(name: String) -> Option<Rc<Self>> {
25+
// // todo: filter out illegal names
26+
// assert!(!name.starts_with("$"));
27+
// return Some(Rc::new(Name { name }));
28+
// }
29+
//
30+
// pub fn pure_name(&self) -> String {
31+
// return self.name.to_owned();
32+
// }
33+
//}
34+
//
35+
//impl Wasm for Name {
36+
// fn as_wat(&self) -> String {
37+
// format!("${}", self.name)
38+
// }
39+
//
40+
// fn write_wasm(&self, file: &mut File) -> io::Result<()> {
41+
// unimplemented!()
42+
// }
43+
//}

src/mango/towasm/util/given_name.rs

+30-30
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
use mango::towasm::Wasm;
2-
use std::fs::File;
3-
use std::io;
4-
use std::rc::Rc;
5-
6-
pub struct GivenName {
7-
name: String,
8-
}
9-
10-
impl GivenName {
11-
pub fn new(name: String) -> Option<Rc<Self>> {
12-
// todo: filter out illegal names (thread_lcoal!)
13-
assert!(!name.starts_with("$"));
14-
return Some(Rc::new(GivenName { name }));
15-
}
16-
17-
pub fn pure_name(&self) -> String {
18-
return self.name.to_owned();
19-
}
20-
}
21-
22-
impl Wasm for GivenName {
23-
fn as_wat(&self) -> String {
24-
format!("${}", self.name)
25-
}
26-
27-
fn write_wasm(&self, file: &mut File) -> io::Result<()> {
28-
unimplemented!()
29-
}
30-
}
1+
//use mango::towasm::Wasm;
2+
//use std::fs::File;
3+
//use std::io;
4+
//use std::rc::Rc;
5+
//
6+
//pub struct GivenName {
7+
// name: String,
8+
//}
9+
//
10+
//impl GivenName {
11+
// pub fn new(name: String) -> Option<Rc<Self>> {
12+
// // todo: filter out illegal names (thread_lcoal!)
13+
// assert!(!name.starts_with("$"));
14+
// return Some(Rc::new(GivenName { name }));
15+
// }
16+
//
17+
// pub fn pure_name(&self) -> String {
18+
// return self.name.to_owned();
19+
// }
20+
//}
21+
//
22+
//impl Wasm for GivenName {
23+
// fn as_wat(&self) -> String {
24+
// format!("${}", self.name)
25+
// }
26+
//
27+
// fn write_wasm(&self, file: &mut File) -> io::Result<()> {
28+
// unimplemented!()
29+
// }
30+
//}

src/mango/towasm/util/mod.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@
55
/// * Names, anonymous or otherwise, should not conflict.
66
/// * It should be possible to provide prefixes.
77
/// * Anonymous names should be short.
8-
98
mod name;
10-
pub use self::name::Name;
11-
12-
mod given_name;
13-
pub use self::given_name::GivenName;
9+
pub use self::name::{KnownName, Name, PendingName, RawName};
1410

15-
mod anonymous;
16-
pub use self::anonymous::AnonymousName;
11+
mod pool;
12+
pub use self::pool::NamePool;

src/mango/towasm/util/name.rs

+18-15
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
use mango::towasm::Wasm;
2-
use std::fs::File;
3-
use std::io;
4-
use std::rc::Rc;
5-
use std::hash::Hash;
62
use std::cell::RefCell;
3+
use std::rc::Rc;
74

85
#[derive(Hash, PartialEq, Eq)]
96
pub struct KnownName {
10-
name: String
7+
name: String,
118
}
129

1310
impl KnownName {
14-
pub fn new(name: String) -> Option<Name> {
15-
// todo: filter out illegal names (thread_lcoal!)
16-
assert!(!name.starts_with("$"));
17-
Some(Rc::new(RefCell::new(RawName::Known(KnownName { name }))))
18-
}
11+
// fn new(name: String) -> Option<Name> {
12+
// todo: filter out illegal names (thread_lcoal!)
13+
// assert!(!name.starts_with("$"));
14+
// Some(Rc::new(RefCell::new(RawName::Known(KnownName { name }))))
15+
// }
1916
}
2017

2118
#[derive(Hash, PartialEq, Eq)]
22-
pub struct PendingName {}
19+
pub struct PendingName {
20+
prefix: Option<String>,
21+
id: u32,
22+
}
23+
// TODO: change Hash, Eq etc to only use id, not prefix, since ids are unique anyway
2324

2425
impl PendingName {
25-
pub fn new() -> Name {
26-
Rc::new(RefCell::new(RawName::Pending()))
27-
}
26+
// fn new() -> Name {
27+
// Rc::new(RefCell::new(RawName::Pending()))
28+
// }
2829
}
2930

3031
#[derive(Hash, PartialEq, Eq)]
@@ -33,4 +34,6 @@ pub enum RawName {
3334
Pending(PendingName),
3435
}
3536

36-
type Name = Rc<RefCell<RawName>>;
37+
impl RawName {}
38+
39+
pub type Name = Rc<RefCell<RawName>>;

src/mango/towasm/util/pool.rs

+36-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,45 @@
1+
use mango::towasm::util::KnownName;
2+
use mango::towasm::util::Name;
3+
use mango::towasm::util::PendingName;
4+
use std::cell::RefCell;
5+
use std::collections::HashSet;
6+
use std::rc::Rc;
17

2-
pub struct NamePool {
3-
names: Vec<Rc<Name>>
8+
pub struct RawNamePool {
9+
counter: u32,
10+
names: HashSet<Name>,
411
}
512

6-
impl NamePool {
7-
pub fn register(name: Rc<Name>) {
13+
impl RawNamePool {
14+
pub fn new() -> NamePool {
15+
Rc::new(RefCell::new(RawNamePool {
16+
names: HashSet::with_capacity(512),
17+
}))
18+
}
819

20+
pub fn declare(&self, name: String) -> Result<Name, ()> {
21+
return if self.names.contains(name) {
22+
Result::Err(())
23+
} else {
24+
let name: Name = Rc::new(RefCell::new(Name::Known(KnownName { name })));
25+
self.names.insert(name.clone());
26+
Result::Ok(name)
27+
};
928
}
1029

11-
pub fn anonymous() {
30+
pub fn reuse(&self, name: String) -> Option<Name> {}
1231

32+
pub fn anonymous(&self) -> Name {
33+
self.counter += 1;
34+
let name: Name = Rc::new(RefCell::new(Name::Pending(PendingName {
35+
prefix: Option::None,
36+
id: self.counter,
37+
})));
38+
self.names.insert(name);
39+
name
1340
}
41+
42+
pub fn anonymous_prefix(&self, prefix: String) -> Name {}
1443
}
44+
45+
pub type NamePool = Rc<RefCell<RawNamePool>>;

0 commit comments

Comments
 (0)