Some speed improvements

This commit is contained in:
Nathan Braswell
2016-06-11 00:45:18 -07:00
parent 59969e7114
commit 2c8c3af48a
9 changed files with 85 additions and 65 deletions

View File

@@ -1263,7 +1263,7 @@ fun function_lookup(name: string, scope: *ast_node, param_types: vector<*type>):
/*println(string("function lookup failed for ") + name)*/
return null<ast_node>()
}
fun identifier_lookup(name: string, scope: *ast_node): *ast_node {
fun identifier_lookup(name: ref string, scope: *ast_node): *ast_node {
/*println(string("doing identifier lookup for: ") + name)*/
var results = scope_lookup(name, scope)
if (!results.size) {
@@ -1272,7 +1272,7 @@ fun identifier_lookup(name: string, scope: *ast_node): *ast_node {
}
return results[0]
}
fun scope_lookup(name: string, scope: *ast_node): vector<*ast_node> {
fun scope_lookup(name: ref string, scope: *ast_node): vector<*ast_node> {
// println("*****Doing a name lookup for*****")
// println(name)
var results = vector(scope)
@@ -1291,7 +1291,7 @@ fun scope_lookup(name: string, scope: *ast_node): vector<*ast_node> {
})
return results
}
fun scope_lookup_helper(name: string, scope: *ast_node, visited: set<*ast_node>): vector<*ast_node> {
fun scope_lookup_helper(name: ref string, scope: *ast_node, visited: set<*ast_node>): vector<*ast_node> {
// need to do properly scopded lookups
// print("scope is: ")
// get_ast_scope(scope)->for_each(fun(key: string, value: vector<*ast_node>) print(key + " ");)

View File

@@ -1071,9 +1071,11 @@ obj c_generator (Object) {
return string("impossible type") + indirection
}
fun get_name(node: *ast_node): string {
if (ast_name_map.contains_key(node))
return ast_name_map[node]
var maybe_it = ast_name_map.get_ptr_or_null(node);
if (maybe_it)
return *maybe_it
var result = string("impossible name")
var make_unique = true
match (*node) {
ast_node::type_def(backing) {
var upper = backing.scope[string("~enclosing_scope")][0]
@@ -1086,29 +1088,28 @@ obj c_generator (Object) {
}
ast_node::function(backing) {
// be careful, operators like . come through this, but so do adt constructor funcs
if (!backing.body_statement && !backing.scope.contains_key(string("~enclosing_scope")))
return backing.name
if (backing.name == "main")
return backing.name
if (backing.is_extern)
return backing.name
result = "fun_"
var upper = backing.scope.get_with_default(string("~enclosing_scope"), vector(null<ast_node>()))[0]
if (upper && is_type_def(upper))
result += get_name(upper) + "_"
result += cify_name(node->function.name)
node->function.parameters.for_each(fun(param: *ast_node) result += string("_") + type_decoration(param->identifier.type);)
if ((backing.name == "main") || backing.is_extern || (!backing.body_statement && !backing.scope.contains_key(string("~enclosing_scope")))) {
result = backing.name
make_unique = false
} else {
result = "fun_"
var upper = backing.scope.get_with_default(string("~enclosing_scope"), vector(null<ast_node>()))[0]
if (upper && is_type_def(upper))
result += get_name(upper) + "_"
result += cify_name(node->function.name)
node->function.parameters.for_each(fun(param: *ast_node) result += string("_") + type_decoration(param->identifier.type);)
}
}
ast_node::identifier(backing) {
if (backing.name == "this")
return backing.name
make_unique = false
result = backing.name
}
}
if (result == "impossible name")
error("HUGE PROBLEMS")
// TODO keyword avoid seems not to work
if (ast_name_map.contains_value(result) || c_keyword_avoid.contains(result))
if (make_unique && (ast_name_map.contains_value(result) || c_keyword_avoid.contains(result)))
result += get_id()
/*println("HERE: " + result)*/
ast_name_map.set(node, result)

View File

@@ -8,7 +8,7 @@ fun hash_map<T,U>(): hash_map<T,U> {
var toRet.construct(): hash_map<T,U>
return toRet
}
fun hash_map<T,U>(key:T, value:U): hash_map<T,U> {
fun hash_map<T,U>(key: ref T, value: ref U): hash_map<T,U> {
var toRet.construct(): hash_map<T,U>
toRet.set(key, value)
return toRet
@@ -38,9 +38,9 @@ obj hash_map<T,U> (Object, Serializable) {
/*io::print("old size of data:")*/
/*io::println(old->data.size)*/
}
fun operator=(rhs: hash_map<T,U>) {
destruct()
copy_construct(&rhs)
fun operator=(rhs: ref hash_map<T,U>) {
data = rhs.data
size = rhs.size
}
fun destruct() {
/*io::print("destructed hash_map, this: ")*/
@@ -60,7 +60,7 @@ obj hash_map<T,U> (Object, Serializable) {
fun operator==<V>(other: ref hash_map<T,V>): bool {
return data == other.data
}
fun set(key: T, value: U) {
fun set(key: ref T, value: ref U) {
/*io::print("doing set! this:")*/
/*io::println((this) cast int)*/
/*io::print("size of data:")*/
@@ -82,40 +82,40 @@ obj hash_map<T,U> (Object, Serializable) {
}
data[key_hash%data.size].set(key, value)
}
fun get(key: T): ref U {
fun get(key: ref T): ref U {
return data[(util::hash(key)) cast int%data.size].get(key)
}
fun contains_key(key: T): bool {
fun contains_key(key: ref T): bool {
return data[(util::hash(key)) cast int%data.size].contains_key(key)
}
fun contains_value(value: U): bool {
fun contains_value(value: ref U): bool {
for (var i = 0; i < data.size; i++;) {
if (data[i].contains_value(value))
return true
}
return false
}
fun reverse_get(value: U): ref T {
fun reverse_get(value: ref U): ref T {
for (var i = 0; i < data.size; i++;) {
if (data[i].contains_value(value))
return data[i].reverse_get(value)
}
io::println("trying to reverse get a value that is not in the hash_map")
}
fun remove(key: T) {
fun remove(key: ref T) {
data[(util::hash(key)) cast int%data.size].remove(key)
}
fun for_each(func: fun(T, U):void) {
for (var i = 0; i < data.size; i++;)
data[i].for_each(func)
}
fun operator[](key: T): ref U {
fun operator[](key: ref T): ref U {
return get(key)
}
fun operator[]=(key: T, value: U) {
fun operator[]=(key: ref T, value: ref U) {
set(key,value)
}
fun get_with_default(key: T, default_val: ref U): ref U {
fun get_with_default(key: ref T, default_val: ref U): ref U {
if (contains_key(key))
return get(key)
return default_val

View File

@@ -1,4 +1,5 @@
import vector
import mem
import io
import serialize
import util
@@ -7,7 +8,7 @@ fun map<T,U>(): map<T,U> {
var toRet.construct(): map<T,U>
return toRet
}
fun map<T,U>(key:T, value:U): map<T,U> {
fun map<T,U>(key: ref T, value: ref U): map<T,U> {
var toRet.construct(): map<T,U>
toRet.set(key, value)
return toRet
@@ -26,9 +27,9 @@ obj map<T,U> (Object, Serializable) {
keys.copy_construct(&old->keys)
values.copy_construct(&old->values)
}
fun operator=(rhs: map<T,U>) {
destruct()
copy_construct(&rhs)
fun operator=(rhs: ref map<T,U>) {
keys = rhs.keys
values = rhs.values
}
fun destruct() {
keys.destruct()
@@ -38,9 +39,6 @@ obj map<T,U> (Object, Serializable) {
return serialize::serialize(keys) + serialize::serialize(values)
}
fun unserialize(it: ref vector::vector<char>, pos: int): int {
/*construct()*/
/*util::unpack(keys, pos) = serialize::unserialize<vector::vector<T>>(it, pos)*/
/*util::unpack(values, pos) = serialize::unserialize<vector::vector<U>>(it, pos)*/
pos = keys.unserialize(it, pos)
pos = values.unserialize(it, pos)
return pos
@@ -50,10 +48,10 @@ obj map<T,U> (Object, Serializable) {
fun operator==<V>(other: ref map<T,V>): bool {
return keys == other.keys && values == other.values
}
fun operator[]=(key: T, value: U) {
fun operator[]=(key: ref T, value: ref U) {
set(key,value)
}
fun set(key: T, value: U) {
fun set(key: ref T, value: ref U) {
var keyIdx = keys.find(key)
if (keyIdx >= 0) {
values.set(keyIdx, value)
@@ -62,32 +60,37 @@ obj map<T,U> (Object, Serializable) {
keys.add(key)
values.add(value)
}
fun contains_key(key: T): bool {
fun contains_key(key: ref T): bool {
return keys.contains(key)
}
fun contains_value(value: U): bool {
fun contains_value(value: ref U): bool {
return values.contains(value)
}
fun get(key: T): ref U {
/*return values.get(keys.find(key))*/
fun get(key: ref T): ref U {
var key_loc = keys.find(key)
if (key_loc == -1)
util::error("trying to access nonexistant key-value!")
return values.get(key_loc)
}
fun get_with_default(key: T, default_val: ref U): ref U {
fun get_ptr_or_null(key: ref T): *U {
var key_loc = keys.find(key)
if (key_loc == -1)
return mem::null<U>()
return &values.get(key_loc)
}
fun get_with_default(key: ref T, default_val: ref U): ref U {
if (contains_key(key))
return get(key)
return default_val
}
fun reverse_get(value: U): ref T {
fun reverse_get(value: ref U): ref T {
/*return values.get(keys.find(key))*/
var value_loc = values.find(value)
if (value_loc == -1)
util::error("trying to access nonexistant value-key!")
return keys.get(value_loc)
}
fun remove(key: T) {
fun remove(key: ref T) {
var idx = keys.find(key)
if (idx < 0)
util::error("trying to remove nonexistant key-value!")
@@ -98,7 +101,7 @@ obj map<T,U> (Object, Serializable) {
keys.clear()
values.clear()
}
fun operator[](key: T): ref U {
fun operator[](key: ref T): ref U {
return get(key)
}
fun for_each(func: fun(T, U):void) {

View File

@@ -218,7 +218,9 @@ obj regex (Object, Serializable) {
return -1
return regexString.length();
}
var next = set::set(begin)
/*var next = set::set(begin)*/
var next.construct(): set::set<*regexState>
next.add(begin)
var longest = -1
for (var i = 0; i < end-position; i++;) {
if (next.size() == 0)

View File

@@ -1,7 +1,6 @@
import vector
import io
import serialize
import util
fun set<T>(): set<T> {
var toRet.construct() : set<T>
@@ -34,8 +33,6 @@ obj set<T> (Object, Serializable) {
data.copy_construct(&old->data)
}
fun operator=(rhs: ref set<T>) {
/*destruct()*/
/*copy_construct(&rhs)*/
data = rhs.data
}
fun serialize(): vector::vector<char> {

View File

@@ -88,11 +88,15 @@ obj string (Object, Serializable) {
return this;
}
fun construct(str: *char): *string {
data.construct();
while(*str) {
data.addEnd(*str);
str += 1;
}
var len = 0
while (str[len] != 0) len++
data.construct(len);
data.set_size(len);
mem::memmove((data.getBackingMemory()) cast *void, (str) cast *void, (len) cast ulong)
/*while(*str) {*/
/*data.addEnd(*str);*/
/*str += 1;*/
/*}*/
// no null terminator
return this;
}
@@ -118,8 +122,6 @@ obj string (Object, Serializable) {
}
fun operator=(str: ref string): void {
/*destruct();*/
/*data.copy_construct(&str.data)*/
data = str.data
}

View File

@@ -11,6 +11,9 @@ import serialize
// maybe my favorite function
fun do_nothing() {}
fun is_object<T>(): bool return false
fun is_object<T(Object)>(): bool return true
fun error(message: *char) error(string::string(message));
fun error(message: string::string) {
io::printlnerr("****ERROR****")

View File

@@ -36,9 +36,12 @@ obj vector<T> (Object, Serializable) {
fun copy_construct(old: *vector<T>): void {
construct(old->size)
size = old->size
for (var i = 0; i < old->size; i++;)
maybe_copy_construct(&data[i], &old->data[i]);
//addEnd(old->get(i))
if (is_object<T>()) {
for (var i = 0; i < old->size; i++;)
maybe_copy_construct(&data[i], &old->data[i]);
} else {
memmove((data) cast *void, (old->data) cast *void, size * #sizeof<T>)
}
}
fun serialize(): vector<char> {
var toRet = serialize(size)
@@ -65,14 +68,23 @@ obj vector<T> (Object, Serializable) {
data = 0
}
fun set_size(s: int) {
size = s
}
fun operator=(other:ref vector<T>):void {
if (size < other.size) {
destruct()
copy_construct(&other)
} else {
clear()
for (var i = 0; i < other.size; i++;)
addEnd(other.get(i))
if (is_object<T>()) {
for (var i = 0; i < other.size; i++;)
addEnd(other.get(i))
} else {
size = other.size
memmove((data) cast *void, (other.data) cast *void, size * #sizeof<T>)
}
}
}