Skip to content

Commit

Permalink
NEW LIST with std::shared_ptr<S[]> items;
Browse files Browse the repository at this point in the history
slower but ok?
  • Loading branch information
pannous committed Dec 10, 2024
1 parent 571dbbb commit 17bb6cf
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 45 deletions.
18 changes: 9 additions & 9 deletions source/Angle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,16 +375,16 @@ Signature &groupFunctionArgs(Function &function, Node &params) {
function.is_polymorphic = true;
// function.is_used … todo copy other attributes?
function.signature = *new Signature();// empty
function.variants.add(old_variant);
function.variants.add(new_variant);
function.variants.add(&old_variant);
function.variants.add(&new_variant);
} else if (function.is_polymorphic) {
variant = new Function();
for (Function &fun: function.variants)
if (fun.signature == signature)
return fun.signature;// signature;
for (Function *fun: function.variants)
if (fun->signature == signature)
return fun->signature;// signature;
variant->name = function.name;
variant->signature = signature;
function.variants.add(*variant);
function.variants.add(variant);
} else if (!already_defined) {
function.signature = signature;
}
Expand Down Expand Up @@ -1805,16 +1805,16 @@ Function *use_required(Function *function) {
functions["proc_exit"].is_used = true;
if (function->name == "puts")
functions["fd_write"].is_used = true;
for (Function &variant: function->variants) {
addLibraryFunctionAsImport(variant);
for (Function *variant: function->variants) {
addLibraryFunctionAsImport(*variant);
}
for (String &alias: aliases(function->name)) {
if (alias == function->name)continue;
auto ali = findLibraryFunction(alias, false);
if (ali)addLibraryFunctionAsImport(*ali);
}
for (auto vari: function->variants) {
vari.is_used = true;
vari->is_used = true;
}
// for(Function& dep:function.required)
// dep.is_used = true;
Expand Down
2 changes: 1 addition & 1 deletion source/Code.h
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,7 @@ class Function {
bool is_builtin = false;// hard coded functions, tests only? todo remove
bool is_used = false;// called imports / buildins
bool is_polymorphic = false;// IF polymorph, this 'Function' acts as abstract only, all REAL Functions are in variants
List<Function> variants;// = 20;//={.capacity=20};// multi dispatch!
List<Function *> variants;// = 20;//={.capacity=20};// multi dispatch!

// Code* code; // todo: use
Map<String, Local> locals; // todo: use, instead of global locals!
Expand Down
44 changes: 29 additions & 15 deletions source/List.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <cstdlib> // OK in WASM!

#define LIST_DEFAULT_CAPACITY 10 // todo grow doesn't work, BREAKS SYSTEM!
#define LIST_DEFAULT_CAPACITY 200 // todo grow doesn't work, BREAKS SYSTEM!
#define LIST_MAX_CAPACITY 0x1000000000l // debug only!

#include "Util.h"
Expand Down Expand Up @@ -58,20 +58,22 @@ class List {
// capacity = size;
// items = (S *) calloc(size, sizeof(S));
// }
List(size_t initial_size = LIST_DEFAULT_CAPACITY) : size_(initial_size) {
if (initial_size > 0) {
List(size_t initial_size = LIST_DEFAULT_CAPACITY) {
if (initial_size > 0)
items = std::make_unique<S[]>(initial_size);
}
if (initial_size > capacity)capacity = initial_size;//!
}

List(const List &old) : items(old.items) { // todo: memcopy?
size_ = old.size_;
capacity = old.capacity;
}

List(Array_Header a) {
if (a.length == 0xA0000000)
error1("double header");// todo: just shift by 4 bytes
size_ = a.length;
capacity = a.length; //!
items = (S *) &a.data;// ok? copy data?
// todo("a.typ");
}
Expand All @@ -94,8 +96,10 @@ class List {
//#ifndef PURE_WASM
List(const std::initializer_list<S> &inis) : List() {
auto item_count = inis.end() - inis.begin();
while (item_count >= capacity)grow();
while (item_count >= capacity)
grow();
for (const S &s: inis) {
if (item_count-- < 0)break;
if (&s == nullptr)continue;
items[size_++] = s;
}
Expand All @@ -113,6 +117,7 @@ class List {
print("va_arg#");
print(size_);
print(item);
if (size_ >= capacity)grow();
items[size_++] = item;
item = (S) va_arg(args, S);
} while (item);
Expand All @@ -136,6 +141,7 @@ class List {
if (args == 0)return;
// check_silent(count < LIST_MAX_CAPACITY)
size_ = count;
capacity = count;//!
if (share)
items = std::shared_ptr<S[]>(args, [](S *p) { delete[] p; });
else {
Expand Down Expand Up @@ -175,12 +181,20 @@ class List {
_type = type;
}

void grow() {
// warn("grow");
auto new_size = capacity * 2;
resize(new_size);
//
// if (new_size < 0)error("List capacity overflow");
void grow() { /// ≠ resize() !
auto new_capacity = capacity * 2;
if (new_capacity < 0)error("List capacity overflow");
warn("grow");
// print(new_capacity);
std::shared_ptr<S[]> new_items(new S[new_capacity], std::default_delete<S[]>());
// Copy old items to new_items (up to the smaller of old and new sizes)
if (items) {
for (size_t i = 0; i < size_; ++i) {
new_items[i] = std::move(items[i]);
}
}
items = new_items;
capacity = new_capacity;
//// check_silent(new_size < LIST_MAX_CAPACITY);
// S *neu = (S *) alloc(new_size, sizeof(S));
// memcpy((void *) neu, (void *) items, capacity * sizeof(S));
Expand All @@ -190,13 +204,13 @@ class List {
//// indeed List<int> FUCKS UP just by growing even without references
//// malloc: Heap corruption detected, free list is damaged
// items = neu;
// capacity = new_size;
}

S &add(S s) {
// if(!items)grow();// how?
if (size_ >= capacity - 1)
grow();
items[size_++] = s;
if (size_ >= capacity)grow();
return items[size_ - 1];
}

Expand Down Expand Up @@ -444,6 +458,7 @@ class List {
}

void resize(size_t new_size) {
if (new_size > capacity)capacity = new_size;
std::shared_ptr<S[]> new_items(new S[new_size], std::default_delete<S[]>());
// Copy old items to new_items (up to the smaller of old and new sizes)
if (items) {
Expand All @@ -456,9 +471,8 @@ class List {
}

void append(S *value, size_t len) {
if (size_ + len >= capacity) {
if (size_ + len >= capacity)
grow();
}
memcpy((void *) &items[size_], (void *) value, len * sizeof(S));
size_ += len;
}
Expand Down
20 changes: 10 additions & 10 deletions source/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ void testListGrowth() {
list.add(*new S());
}
check_eq(list.size(), 1000);
for (int i = 0; i < 10; i++) { // 20 => growth by 2^20!!!
for (int i = 0; i < 5; i++) { // 10 VERY SLOW in new implementation! DONT DO 20 => growth by 2^20!!!
list.grow();
}
check(list.capacity > 100000);
check(list.capacity > 1000);
}

void testListGrowthWithStrings() {
Expand Down Expand Up @@ -409,12 +409,12 @@ void testPolymorphism() {
auto function = functions["test"];
check_is(function.is_polymorphic, true);
check_is(function.variants.size(), 2);
check_is(function.variants[0].signature.size(), 1);
check_is(function.variants[0]->signature.size(), 1);
// check_is(function.variants[0].signature.parameters[0].type, (Type) strings); todo
check_is(function.variants[0].signature.parameters[0].type, (Type) stringp);
check_is(function.variants[0]->signature.parameters[0].type, (Type) stringp);
auto variant = function.variants[1];
check_is(variant.signature.size(), 1);
check_is(variant.signature.parameters[0].type, (Type) float32);
check_is(variant->signature.size(), 1);
check_is(variant->signature.parameters[0].type, (Type) float32);
}

void testPolymorphism2() {
Expand All @@ -424,10 +424,10 @@ void testPolymorphism2() {
auto function = functions["test"];
check_is(function.is_polymorphic, true);
check_is(function.variants.size(), 2);
check_is(function.variants[0].signature.size(), 1);
check_is(function.variants[0].signature.parameters[0].type, (Type) int32);
check_is(function.variants[1].signature.size(), 1);
check_is(function.variants[1].signature.parameters[0].type, (Type) float32);
check_is(function.variants[0]->signature.size(), 1);
check_is(function.variants[0]->signature.parameters[0].type, (Type) int32);
check_is(function.variants[1]->signature.size(), 1);
check_is(function.variants[1]->signature.parameters[0].type, (Type) float32);
}


Expand Down
21 changes: 11 additions & 10 deletions source/wasm_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ Valtype mapArgToValtype(String &arg);

#define consume(len, match) if(!consume_x(code,&pos,len,match)){if(debug_reader)printf("\nNOT consuming %s",#match);backtrace_line();}

bool consume_x(byte *code, int *pos, int len, byte *bytes) {
if (*pos + len > size)
bool consume_x(byte *code_bytes, int *posi, int len, byte *bytes) {
if (*posi + len > size)
error("END OF FILE");
if (not bytes) {
*pos = *pos + len;
*posi = *posi + len;
return true;
}

int i = 0;
while (i < len) {
if (bytes and code[*pos] != bytes[i])
if (bytes and code_bytes[*posi] != bytes[i])
return false;
*pos = *pos + 1;
*posi = *posi + 1;
i++;
}
return true;
Expand Down Expand Up @@ -505,10 +505,11 @@ void consumeExportSection() {
trace("function %s already has signature "s % func + fun.signature.serialize());
trace("function %s old code_index %d new code_index %d"s % func % fun.code_index % lower_index);
Function &abstract = *new Function{.name=func, .module=module, .is_runtime=true, .is_polymorphic=true};
abstract.variants.add(fun);
abstract.variants.add(&fun);
module->functions[func] = abstract;
fun = abstract.variants.items[2];
// fun = *abstract.variants.items[2];
fun = *new Function{.code_index=lower_index, .name=func, .module=fun.module, .is_runtime=true};
abstract.variants.items[2] = &fun;
} else {
fun0.code_index = lower_index;
fun.code_index = lower_index;
Expand Down Expand Up @@ -627,9 +628,9 @@ void consumeSections() {
Map<int64, Module *> module_cache{.capacity=100};

Code &read_code(chars file) {
int size;
char *data = readFile(file, &size);
Code &cod = *new Code(data, size, false);
int code_size;
char *data = readFile(file, &code_size);
Code &cod = *new Code(data, code_size, false);
cod.name = String(file);
return cod;
}
Expand Down

0 comments on commit 17bb6cf

Please sign in to comment.