Skip to content

Commit

Permalink
Tweaked standard bucket sizes, see #160
Browse files Browse the repository at this point in the history
Hyacinth: It's pronounced "Bouquet"!
  • Loading branch information
Ratstail91 committed Dec 17, 2024
1 parent 3e17916 commit 04c7999
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
36 changes: 23 additions & 13 deletions source/toy_bucket.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// - It can only expand until it is freed
// - It cannot be copied around within RAM
// - It cannot allocate more memory than it has capacity
// If each of these rules are followed, the bucket is actually more efficient than any other option
// If each of these rules are followed, this is actually more efficient than other options

//a custom allocator
typedef struct Toy_Bucket { //32 | 64 BITNESS
Expand All @@ -20,27 +20,37 @@ TOY_API Toy_Bucket* Toy_allocateBucket(unsigned int capacity);
TOY_API void* Toy_partitionBucket(Toy_Bucket** bucketHandle, unsigned int amount);
TOY_API void Toy_freeBucket(Toy_Bucket** bucketHandle);

//some useful sizes, could be swapped out as needed
#ifndef TOY_BUCKET_TINY
#define TOY_BUCKET_TINY (1024 * 2)
//standard capacity sizes
#ifndef TOY_BUCKET_1KB
#define TOY_BUCKET_1KB (1 << 10)
#endif

#ifndef TOY_BUCKET_SMALL
#define TOY_BUCKET_SMALL (1024 * 4)
#ifndef TOY_BUCKET_2KB
#define TOY_BUCKET_2KB (1 << 11)
#endif

#ifndef TOY_BUCKET_MEDIUM
#define TOY_BUCKET_MEDIUM (1024 * 8)
#ifndef TOY_BUCKET_4KB
#define TOY_BUCKET_4KB (1 << 12)
#endif

#ifndef TOY_BUCKET_LARGE
#define TOY_BUCKET_LARGE (1024 * 16)
#ifndef TOY_BUCKET_8KB
#define TOY_BUCKET_8KB (1 << 13)
#endif

#ifndef TOY_BUCKET_HUGE
#define TOY_BUCKET_HUGE (1024 * 32)
#ifndef TOY_BUCKET_16KB
#define TOY_BUCKET_16KB (1 << 14)
#endif

#ifndef TOY_BUCKET_32KB
#define TOY_BUCKET_32KB (1 << 15)
#endif

#ifndef TOY_BUCKET_64KB
#define TOY_BUCKET_64KB (1 << 16)
#endif

//CPU L1 caches tend to be 64kb, but that's far from guaranteed
#ifndef TOY_BUCKET_IDEAL
#define TOY_BUCKET_IDEAL (TOY_BUCKET_HUGE - sizeof(Toy_Bucket))
#define TOY_BUCKET_IDEAL (TOY_BUCKET_64KB - sizeof(Toy_Bucket))
#endif

2 changes: 1 addition & 1 deletion source/toy_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ void Toy_bindVMToModule(Toy_VM* vm, unsigned char* module) {
vm->stringBucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
}
if (vm->scopeBucket == NULL) {
vm->scopeBucket = Toy_allocateBucket(TOY_BUCKET_SMALL);
vm->scopeBucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
}
if (vm->stack == NULL) {
vm->stack = Toy_allocateStack();
Expand Down
22 changes: 11 additions & 11 deletions tests/cases/test_value.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ int test_value_creation(void) {
//test creating strings
{
//setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_SMALL);
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);

Toy_Value greeting = TOY_VALUE_FROM_STRING(Toy_createString(&bucket, "Hello world!"));

Expand Down Expand Up @@ -116,7 +116,7 @@ int test_value_copying(void) {
//test string copy
{
//setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_SMALL);
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);

Toy_Value original = TOY_VALUE_FROM_STRING(Toy_createString(&bucket, "Hello world!"));
Toy_Value result = Toy_copyValue(original);
Expand Down Expand Up @@ -240,7 +240,7 @@ int test_value_equality(void) {
//again with strings
{
//setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_SMALL);
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);

Toy_Value answer = TOY_VALUE_FROM_STRING(Toy_createString(&bucket, "poe wrote on both"));
Toy_Value question = TOY_VALUE_FROM_STRING(Toy_createString(&bucket, "why is a raven like a writing desk?"));
Expand Down Expand Up @@ -332,7 +332,7 @@ int test_value_comparison(void) {
//again with strings
{
//setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_SMALL);
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);

Toy_Value answer = TOY_VALUE_FROM_STRING(Toy_createString(&bucket, "poe wrote on both"));
Toy_Value question = TOY_VALUE_FROM_STRING(Toy_createString(&bucket, "why is a raven like a writing desk?"));
Expand Down Expand Up @@ -361,7 +361,7 @@ int test_value_stringify(void) {
//stringify null
{
//setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_SMALL);
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Value value = TOY_VALUE_FROM_NULL();

//run
Expand All @@ -387,7 +387,7 @@ int test_value_stringify(void) {
//stringify boolean (true)
{
//setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_SMALL);
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Value value = TOY_VALUE_FROM_BOOLEAN(true);

//run
Expand All @@ -413,7 +413,7 @@ int test_value_stringify(void) {
//stringify boolean (false)
{
//setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_SMALL);
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Value value = TOY_VALUE_FROM_BOOLEAN(false);

//run
Expand All @@ -439,7 +439,7 @@ int test_value_stringify(void) {
//stringify integer
{
//setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_SMALL);
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Value value = TOY_VALUE_FROM_INTEGER(42);

//run
Expand All @@ -465,7 +465,7 @@ int test_value_stringify(void) {
//stringify float
{
//setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_SMALL);
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Value value = TOY_VALUE_FROM_FLOAT(3.1415f);

//run
Expand All @@ -491,7 +491,7 @@ int test_value_stringify(void) {
//stringify strings
{
//setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_SMALL);
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);

Toy_Value value = TOY_VALUE_FROM_STRING(Toy_createString(&bucket, "Hello world!"));
Toy_String* string = Toy_stringifyValue(&bucket, value);
Expand All @@ -513,7 +513,7 @@ int test_value_stringify(void) {
//stringify array
{
//setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_SMALL);
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);

//setup
Toy_Array* array = Toy_resizeArray(NULL, TOY_ARRAY_INITIAL_CAPACITY);
Expand Down

0 comments on commit 04c7999

Please sign in to comment.