From 2c8c3af48a8fdbd13395138a2fc66c86ab0b5feb Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Sat, 11 Jun 2016 00:45:18 -0700 Subject: [PATCH] Some speed improvements --- stdlib/ast_transformation.krak | 6 +++--- stdlib/c_generator.krak | 33 +++++++++++++++--------------- stdlib/hash_map.krak | 26 ++++++++++++------------ stdlib/map.krak | 37 ++++++++++++++++++---------------- stdlib/regex.krak | 4 +++- stdlib/set.krak | 3 --- stdlib/string.krak | 16 ++++++++------- stdlib/util.krak | 3 +++ stdlib/vector.krak | 22 +++++++++++++++----- 9 files changed, 85 insertions(+), 65 deletions(-) diff --git a/stdlib/ast_transformation.krak b/stdlib/ast_transformation.krak index 76b96a1..561fe83 100644 --- a/stdlib/ast_transformation.krak +++ b/stdlib/ast_transformation.krak @@ -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() } -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 + " ");) diff --git a/stdlib/c_generator.krak b/stdlib/c_generator.krak index 904e61f..c222be1 100644 --- a/stdlib/c_generator.krak +++ b/stdlib/c_generator.krak @@ -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()))[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()))[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) diff --git a/stdlib/hash_map.krak b/stdlib/hash_map.krak index f1f3148..2c5ef60 100644 --- a/stdlib/hash_map.krak +++ b/stdlib/hash_map.krak @@ -8,7 +8,7 @@ fun hash_map(): hash_map { var toRet.construct(): hash_map return toRet } -fun hash_map(key:T, value:U): hash_map { +fun hash_map(key: ref T, value: ref U): hash_map { var toRet.construct(): hash_map toRet.set(key, value) return toRet @@ -38,9 +38,9 @@ obj hash_map (Object, Serializable) { /*io::print("old size of data:")*/ /*io::println(old->data.size)*/ } - fun operator=(rhs: hash_map) { - destruct() - copy_construct(&rhs) + fun operator=(rhs: ref hash_map) { + data = rhs.data + size = rhs.size } fun destruct() { /*io::print("destructed hash_map, this: ")*/ @@ -60,7 +60,7 @@ obj hash_map (Object, Serializable) { fun operator==(other: ref hash_map): 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 (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 diff --git a/stdlib/map.krak b/stdlib/map.krak index 2fe9a9a..b37cfbd 100644 --- a/stdlib/map.krak +++ b/stdlib/map.krak @@ -1,4 +1,5 @@ import vector +import mem import io import serialize import util @@ -7,7 +8,7 @@ fun map(): map { var toRet.construct(): map return toRet } -fun map(key:T, value:U): map { +fun map(key: ref T, value: ref U): map { var toRet.construct(): map toRet.set(key, value) return toRet @@ -26,9 +27,9 @@ obj map (Object, Serializable) { keys.copy_construct(&old->keys) values.copy_construct(&old->values) } - fun operator=(rhs: map) { - destruct() - copy_construct(&rhs) + fun operator=(rhs: ref map) { + keys = rhs.keys + values = rhs.values } fun destruct() { keys.destruct() @@ -38,9 +39,6 @@ obj map (Object, Serializable) { return serialize::serialize(keys) + serialize::serialize(values) } fun unserialize(it: ref vector::vector, pos: int): int { - /*construct()*/ - /*util::unpack(keys, pos) = serialize::unserialize>(it, pos)*/ - /*util::unpack(values, pos) = serialize::unserialize>(it, pos)*/ pos = keys.unserialize(it, pos) pos = values.unserialize(it, pos) return pos @@ -50,10 +48,10 @@ obj map (Object, Serializable) { fun operator==(other: ref map): 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 (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() + 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 (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) { diff --git a/stdlib/regex.krak b/stdlib/regex.krak index 2abb4df..558229e 100644 --- a/stdlib/regex.krak +++ b/stdlib/regex.krak @@ -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) diff --git a/stdlib/set.krak b/stdlib/set.krak index 2b6e1cb..96848e3 100644 --- a/stdlib/set.krak +++ b/stdlib/set.krak @@ -1,7 +1,6 @@ import vector import io import serialize -import util fun set(): set { var toRet.construct() : set @@ -34,8 +33,6 @@ obj set (Object, Serializable) { data.copy_construct(&old->data) } fun operator=(rhs: ref set) { - /*destruct()*/ - /*copy_construct(&rhs)*/ data = rhs.data } fun serialize(): vector::vector { diff --git a/stdlib/string.krak b/stdlib/string.krak index 2434646..0c0380d 100644 --- a/stdlib/string.krak +++ b/stdlib/string.krak @@ -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 } diff --git a/stdlib/util.krak b/stdlib/util.krak index 7326b94..a59318e 100644 --- a/stdlib/util.krak +++ b/stdlib/util.krak @@ -11,6 +11,9 @@ import serialize // maybe my favorite function fun do_nothing() {} +fun is_object(): bool return false +fun is_object(): bool return true + fun error(message: *char) error(string::string(message)); fun error(message: string::string) { io::printlnerr("****ERROR****") diff --git a/stdlib/vector.krak b/stdlib/vector.krak index 72e29f8..c09f697 100644 --- a/stdlib/vector.krak +++ b/stdlib/vector.krak @@ -36,9 +36,12 @@ obj vector (Object, Serializable) { fun copy_construct(old: *vector): 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()) { + 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) + } } fun serialize(): vector { var toRet = serialize(size) @@ -65,14 +68,23 @@ obj vector (Object, Serializable) { data = 0 } + fun set_size(s: int) { + size = s + } + fun operator=(other:ref vector):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()) { + 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) + } } }