mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
Massive dict copying optimisation, read more
I simply pre-allocated the new dict to the right size. This skips internal copying logic which was repeated on every expansion. This Should increase scope copying as well. I applied the same logic to arrays, but the increase in speed was tiny.
This commit is contained in:
16
scripts/test_copy_speed.toy
Normal file
16
scripts/test_copy_speed.toy
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
|
||||
fn identity(x) {
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
var dict: [int:int] = [:];
|
||||
|
||||
for (var i: int = 0; i < 1000; i++) {
|
||||
dict[i] = i;
|
||||
}
|
||||
|
||||
for (var i: int = 0; i < 100_000; i++) {
|
||||
var x = identity(dict);
|
||||
}
|
||||
@@ -119,6 +119,10 @@ Toy_Literal Toy_copyLiteral(Toy_Literal original) {
|
||||
Toy_LiteralArray* array = TOY_ALLOCATE(Toy_LiteralArray, 1);
|
||||
Toy_initLiteralArray(array);
|
||||
|
||||
//preallocate enough space
|
||||
array->capacity = TOY_AS_ARRAY(original)->capacity;
|
||||
array->literals = TOY_GROW_ARRAY(Toy_Literal, array->literals, 0, array->capacity);
|
||||
|
||||
//copy each element
|
||||
for (int i = 0; i < TOY_AS_ARRAY(original)->count; i++) {
|
||||
Toy_pushLiteralArray(array, TOY_AS_ARRAY(original)->literals[i]);
|
||||
@@ -131,6 +135,15 @@ Toy_Literal Toy_copyLiteral(Toy_Literal original) {
|
||||
Toy_LiteralDictionary* dictionary = TOY_ALLOCATE(Toy_LiteralDictionary, 1);
|
||||
Toy_initLiteralDictionary(dictionary);
|
||||
|
||||
//preallocate enough space
|
||||
dictionary->capacity = TOY_AS_DICTIONARY(original)->capacity;
|
||||
dictionary->entries = TOY_ALLOCATE(Toy_private_dictionary_entry, dictionary->capacity);
|
||||
|
||||
for (int i = 0; i < dictionary->capacity; i++) {
|
||||
dictionary->entries[i].key = TOY_TO_NULL_LITERAL;
|
||||
dictionary->entries[i].value = TOY_TO_NULL_LITERAL;
|
||||
}
|
||||
|
||||
//copy each entry
|
||||
for (int i = 0; i < TOY_AS_DICTIONARY(original)->capacity; i++) {
|
||||
if ( !TOY_IS_NULL(TOY_AS_DICTIONARY(original)->entries[i].key) ) {
|
||||
@@ -168,7 +181,7 @@ Toy_Literal Toy_copyLiteral(Toy_Literal original) {
|
||||
return original; //literally a shallow copy
|
||||
}
|
||||
|
||||
case TOY_LITERAL_ARRAY_INTERMEDIATE: {
|
||||
case TOY_LITERAL_ARRAY_INTERMEDIATE: { //TODO: efficient preallocation?
|
||||
Toy_LiteralArray* array = TOY_ALLOCATE(Toy_LiteralArray, 1);
|
||||
Toy_initLiteralArray(array);
|
||||
|
||||
@@ -184,7 +197,7 @@ Toy_Literal Toy_copyLiteral(Toy_Literal original) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
case TOY_LITERAL_DICTIONARY_INTERMEDIATE: {
|
||||
case TOY_LITERAL_DICTIONARY_INTERMEDIATE: { //TODO: efficient preallocation?
|
||||
Toy_LiteralArray* array = TOY_ALLOCATE(Toy_LiteralArray, 1);
|
||||
Toy_initLiteralArray(array);
|
||||
|
||||
@@ -200,7 +213,7 @@ Toy_Literal Toy_copyLiteral(Toy_Literal original) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
case TOY_LITERAL_TYPE_INTERMEDIATE: {
|
||||
case TOY_LITERAL_TYPE_INTERMEDIATE: { //TODO: efficient preallocation?
|
||||
Toy_LiteralArray* array = TOY_ALLOCATE(Toy_LiteralArray, 1);
|
||||
Toy_initLiteralArray(array);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user