WIP functions working, untested, memory issues, read more

I've ripped out the deep copying, and flattened the bucket usage, but
this results in no memory being freed or reused for the lifetime of the
program.

This is shown most clearly with this script:

```toy
fn makeCounter() {
	var counter: int = 0;

	fn increment() {
		return ++counter;
	}

	return increment;
}

var tally = makeCounter();

while (true) {
	var result = tally();

	if (result >= 10_000_000) {
		break;
	}
}
```

The number of calls vs amount of memory consumed is:

```
function calls -> memory used in megabytes
1_000 -> 0.138128
10_000 -> 2.235536
100_000 -> 21.7021
1_000_000 -> 216.1712
10_000_000 -> 1520.823
```

Obviously this needs to be fixed, as ballooning to gigabytes of memory
in only a moment isn't practical. That will be the next task - to find
some way to free memory that isn't needed anymore.

See #163, #160
This commit is contained in:
2025-02-21 10:48:24 +11:00
parent 9fe6d6b218
commit d3b59eb0da
8 changed files with 42 additions and 167 deletions

View File

@@ -39,9 +39,8 @@ typedef struct Toy_VM {
Toy_Stack* stack;
//easy access to memory
Toy_Bucket* literalBucket; //stores the value literals (strings, functions, etc.)
Toy_Bucket* scopeBucket; //stores the scope instances
Toy_Bucket** scopeBucketHandle; //for reusing the scope bucket to save on alloc/free
Toy_Bucket* memoryBucket;
Toy_Bucket** parentBucketHandle;
} Toy_VM;
TOY_API void Toy_resetVM(Toy_VM* vm, bool preserveScope);
@@ -53,6 +52,6 @@ TOY_API void Toy_bindVM(Toy_VM* vm, Toy_Module* module, bool preserveScope);
TOY_API unsigned int Toy_runVM(Toy_VM* vm);
TOY_API void Toy_freeVM(Toy_VM* vm);
TOY_API Toy_Array* Toy_extractResultsFromVM(Toy_Bucket** scopeBucketHandle, Toy_Bucket** literalBucketHandle, Toy_VM* subVM, unsigned int resultCount);
TOY_API Toy_Array* Toy_extractResultsFromVM(Toy_VM* subVM, unsigned int resultCount);
//TODO: inject extra data (hook system for external libraries)