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)*/ /*println(string("function lookup failed for ") + name)*/
return null<ast_node>() 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)*/ /*println(string("doing identifier lookup for: ") + name)*/
var results = scope_lookup(name, scope) var results = scope_lookup(name, scope)
if (!results.size) { if (!results.size) {
@@ -1272,7 +1272,7 @@ fun identifier_lookup(name: string, scope: *ast_node): *ast_node {
} }
return results[0] 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("*****Doing a name lookup for*****")
// println(name) // println(name)
var results = vector(scope) var results = vector(scope)
@@ -1291,7 +1291,7 @@ fun scope_lookup(name: string, scope: *ast_node): vector<*ast_node> {
}) })
return results 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 // need to do properly scopded lookups
// print("scope is: ") // print("scope is: ")
// get_ast_scope(scope)->for_each(fun(key: string, value: vector<*ast_node>) print(key + " ");) // 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 return string("impossible type") + indirection
} }
fun get_name(node: *ast_node): string { fun get_name(node: *ast_node): string {
if (ast_name_map.contains_key(node)) var maybe_it = ast_name_map.get_ptr_or_null(node);
return ast_name_map[node] if (maybe_it)
return *maybe_it
var result = string("impossible name") var result = string("impossible name")
var make_unique = true
match (*node) { match (*node) {
ast_node::type_def(backing) { ast_node::type_def(backing) {
var upper = backing.scope[string("~enclosing_scope")][0] var upper = backing.scope[string("~enclosing_scope")][0]
@@ -1086,29 +1088,28 @@ obj c_generator (Object) {
} }
ast_node::function(backing) { ast_node::function(backing) {
// be careful, operators like . come through this, but so do adt constructor funcs // be careful, operators like . come through this, but so do adt constructor funcs
if (!backing.body_statement && !backing.scope.contains_key(string("~enclosing_scope"))) if ((backing.name == "main") || backing.is_extern || (!backing.body_statement && !backing.scope.contains_key(string("~enclosing_scope")))) {
return backing.name result = backing.name
if (backing.name == "main") make_unique = false
return backing.name } else {
if (backing.is_extern) result = "fun_"
return backing.name var upper = backing.scope.get_with_default(string("~enclosing_scope"), vector(null<ast_node>()))[0]
result = "fun_" if (upper && is_type_def(upper))
var upper = backing.scope.get_with_default(string("~enclosing_scope"), vector(null<ast_node>()))[0] result += get_name(upper) + "_"
if (upper && is_type_def(upper)) result += cify_name(node->function.name)
result += get_name(upper) + "_" node->function.parameters.for_each(fun(param: *ast_node) result += string("_") + type_decoration(param->identifier.type);)
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) { ast_node::identifier(backing) {
if (backing.name == "this") if (backing.name == "this")
return backing.name make_unique = false
result = backing.name result = backing.name
} }
} }
if (result == "impossible name") if (result == "impossible name")
error("HUGE PROBLEMS") error("HUGE PROBLEMS")
// TODO keyword avoid seems not to work // 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() result += get_id()
/*println("HERE: " + result)*/ /*println("HERE: " + result)*/
ast_name_map.set(node, 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> var toRet.construct(): hash_map<T,U>
return toRet 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> var toRet.construct(): hash_map<T,U>
toRet.set(key, value) toRet.set(key, value)
return toRet return toRet
@@ -38,9 +38,9 @@ obj hash_map<T,U> (Object, Serializable) {
/*io::print("old size of data:")*/ /*io::print("old size of data:")*/
/*io::println(old->data.size)*/ /*io::println(old->data.size)*/
} }
fun operator=(rhs: hash_map<T,U>) { fun operator=(rhs: ref hash_map<T,U>) {
destruct() data = rhs.data
copy_construct(&rhs) size = rhs.size
} }
fun destruct() { fun destruct() {
/*io::print("destructed hash_map, this: ")*/ /*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 { fun operator==<V>(other: ref hash_map<T,V>): bool {
return data == other.data return data == other.data
} }
fun set(key: T, value: U) { fun set(key: ref T, value: ref U) {
/*io::print("doing set! this:")*/ /*io::print("doing set! this:")*/
/*io::println((this) cast int)*/ /*io::println((this) cast int)*/
/*io::print("size of data:")*/ /*io::print("size of data:")*/
@@ -82,40 +82,40 @@ obj hash_map<T,U> (Object, Serializable) {
} }
data[key_hash%data.size].set(key, value) 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) 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) 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++;) { for (var i = 0; i < data.size; i++;) {
if (data[i].contains_value(value)) if (data[i].contains_value(value))
return true return true
} }
return false 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++;) { for (var i = 0; i < data.size; i++;) {
if (data[i].contains_value(value)) if (data[i].contains_value(value))
return data[i].reverse_get(value) return data[i].reverse_get(value)
} }
io::println("trying to reverse get a value that is not in the hash_map") 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) data[(util::hash(key)) cast int%data.size].remove(key)
} }
fun for_each(func: fun(T, U):void) { fun for_each(func: fun(T, U):void) {
for (var i = 0; i < data.size; i++;) for (var i = 0; i < data.size; i++;)
data[i].for_each(func) data[i].for_each(func)
} }
fun operator[](key: T): ref U { fun operator[](key: ref T): ref U {
return get(key) return get(key)
} }
fun operator[]=(key: T, value: U) { fun operator[]=(key: ref T, value: ref U) {
set(key,value) 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)) if (contains_key(key))
return get(key) return get(key)
return default_val return default_val

View File

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

View File

@@ -218,7 +218,9 @@ obj regex (Object, Serializable) {
return -1 return -1
return regexString.length(); 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 var longest = -1
for (var i = 0; i < end-position; i++;) { for (var i = 0; i < end-position; i++;) {
if (next.size() == 0) if (next.size() == 0)

View File

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

View File

@@ -88,11 +88,15 @@ obj string (Object, Serializable) {
return this; return this;
} }
fun construct(str: *char): *string { fun construct(str: *char): *string {
data.construct(); var len = 0
while(*str) { while (str[len] != 0) len++
data.addEnd(*str); data.construct(len);
str += 1; 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 // no null terminator
return this; return this;
} }
@@ -118,8 +122,6 @@ obj string (Object, Serializable) {
} }
fun operator=(str: ref string): void { fun operator=(str: ref string): void {
/*destruct();*/
/*data.copy_construct(&str.data)*/
data = str.data data = str.data
} }

View File

@@ -11,6 +11,9 @@ import serialize
// maybe my favorite function // maybe my favorite function
fun do_nothing() {} 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: *char) error(string::string(message));
fun error(message: string::string) { fun error(message: string::string) {
io::printlnerr("****ERROR****") io::printlnerr("****ERROR****")

View File

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