diff options
author | Kevin Newton <kddnewton@gmail.com> | 2023-10-17 15:33:31 -0400 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2023-10-18 16:08:31 +0000 |
commit | 4358dd207f445259984d5535f1b545c8449fad35 (patch) | |
tree | ffafe8281aa5649178cc92dd09e26bc159a009b1 /prism | |
parent | f5f032295d49758032df4e4c257e423d3c96feb4 (diff) | |
download | ruby-4358dd207f445259984d5535f1b545c8449fad35.tar.gz |
[ruby/prism] Extract out constant pool bucket bits to constants
https://github.com/ruby/prism/commit/1985a9ba51
Diffstat (limited to 'prism')
-rw-r--r-- | prism/templates/src/serialize.c.erb | 2 | ||||
-rw-r--r-- | prism/util/pm_constant_pool.c | 19 | ||||
-rw-r--r-- | prism/util/pm_constant_pool.h | 20 |
3 files changed, 28 insertions, 13 deletions
diff --git a/prism/templates/src/serialize.c.erb b/prism/templates/src/serialize.c.erb index 489765928b..006fae6fc0 100644 --- a/prism/templates/src/serialize.c.erb +++ b/prism/templates/src/serialize.c.erb @@ -234,7 +234,7 @@ pm_serialize_content(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer) pm_constant_t *constant = &parser->constant_pool.constants[bucket->id - 1]; size_t buffer_offset = offset + ((((size_t)bucket->id) - 1) * 8); - if (bucket->owned || bucket->constant) { + if (bucket->type == PM_CONSTANT_POOL_BUCKET_OWNED || bucket->type == PM_CONSTANT_POOL_BUCKET_CONSTANT) { // Since this is an owned or constant constant, we are going to // write its contents into the buffer after the constant pool. // So effectively in place of the source offset, we have a diff --git a/prism/util/pm_constant_pool.c b/prism/util/pm_constant_pool.c index ee4add3ce1..531496fe19 100644 --- a/prism/util/pm_constant_pool.c +++ b/prism/util/pm_constant_pool.c @@ -163,7 +163,7 @@ pm_constant_pool_id_to_constant(pm_constant_pool_t *pool, pm_constant_id_t const // Insert a constant into a constant pool and return its index in the pool. static inline pm_constant_id_t -pm_constant_pool_insert(pm_constant_pool_t *pool, const uint8_t *start, size_t length, bool owned, bool constant) { +pm_constant_pool_insert(pm_constant_pool_t *pool, const uint8_t *start, size_t length, pm_constant_pool_bucket_type_t type) { if (pool->size >= (pool->capacity / 4 * 3)) { if (!pm_constant_pool_resize(pool)) return 0; } @@ -185,19 +185,19 @@ pm_constant_pool_insert(pm_constant_pool_t *pool, const uint8_t *start, size_t l // Since we have found a match, we need to check if this is // attempting to insert a shared or an owned constant. We want to // prefer shared constants since they don't require allocations. - if (owned) { + if (type == PM_CONSTANT_POOL_BUCKET_OWNED) { // If we're attempting to insert an owned constant and we have // an existing constant, then either way we don't want the given // memory. Either it's duplicated with the existing constant or // it's not necessary because we have a shared version. free((void *) start); - } else if (bucket->owned) { + } else if (bucket->type == PM_CONSTANT_POOL_BUCKET_OWNED) { // If we're attempting to insert a shared constant and the // existing constant is owned, then we can free the owned // constant and replace it with the shared constant. free((void *) constant->start); constant->start = start; - bucket->owned = false; + bucket->type = PM_CONSTANT_POOL_BUCKET_DEFAULT; } return bucket->id; @@ -213,8 +213,7 @@ pm_constant_pool_insert(pm_constant_pool_t *pool, const uint8_t *start, size_t l *bucket = (pm_constant_pool_bucket_t) { .id = (unsigned int) (id & 0x3fffffff), - .owned = owned, - .constant = constant, + .type = type, .hash = hash }; @@ -230,7 +229,7 @@ pm_constant_pool_insert(pm_constant_pool_t *pool, const uint8_t *start, size_t l // if any potential calls to resize fail. pm_constant_id_t pm_constant_pool_insert_shared(pm_constant_pool_t *pool, const uint8_t *start, size_t length) { - return pm_constant_pool_insert(pool, start, length, false, false); + return pm_constant_pool_insert(pool, start, length, PM_CONSTANT_POOL_BUCKET_DEFAULT); } // Insert a constant into a constant pool from memory that is now owned by the @@ -238,14 +237,14 @@ pm_constant_pool_insert_shared(pm_constant_pool_t *pool, const uint8_t *start, s // resize fail. pm_constant_id_t pm_constant_pool_insert_owned(pm_constant_pool_t *pool, const uint8_t *start, size_t length) { - return pm_constant_pool_insert(pool, start, length, true, false); + return pm_constant_pool_insert(pool, start, length, PM_CONSTANT_POOL_BUCKET_OWNED); } // Insert a constant into a constant pool from memory that is constant. Returns // the id of the constant, or 0 if any potential calls to resize fail. pm_constant_id_t pm_constant_pool_insert_constant(pm_constant_pool_t *pool, const uint8_t *start, size_t length) { - return pm_constant_pool_insert(pool, start, length, false, true); + return pm_constant_pool_insert(pool, start, length, PM_CONSTANT_POOL_BUCKET_CONSTANT); } // Free the memory associated with a constant pool. @@ -257,7 +256,7 @@ pm_constant_pool_free(pm_constant_pool_t *pool) { pm_constant_pool_bucket_t *bucket = &pool->buckets[index]; // If an id is set on this constant, then we know we have content here. - if (bucket->id != 0 && bucket->owned) { + if (bucket->id != 0 && bucket->type == PM_CONSTANT_POOL_BUCKET_OWNED) { pm_constant_t *constant = &pool->constants[bucket->id - 1]; free((void *) constant->start); } diff --git a/prism/util/pm_constant_pool.h b/prism/util/pm_constant_pool.h index ec4ede0dc4..067a871c6a 100644 --- a/prism/util/pm_constant_pool.h +++ b/prism/util/pm_constant_pool.h @@ -39,10 +39,26 @@ size_t pm_constant_id_list_memsize(pm_constant_id_list_t *list); // Free the memory associated with a list of constant ids. void pm_constant_id_list_free(pm_constant_id_list_t *list); +// Constant pool buckets can have a couple of different types. +typedef unsigned int pm_constant_pool_bucket_type_t; + +// By default, each constant is a slice of the source. +static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_DEFAULT = 0; + +// An owned constant is one for which memory has been allocated. +static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_OWNED = 1; + +// A constant constant is known at compile time. +static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_CONSTANT = 2; + +// An anonymous constant is one that doesn't directly reference values in the +// source. This is used for things like the anonymous local variable introduced +// by for loops, rescue bodies, destructured required parameters, etc. +static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_ANONYMOUS = 3; + typedef struct { unsigned int id: 30; - bool owned: 1; - bool constant: 1; + pm_constant_pool_bucket_type_t type: 2; uint32_t hash; } pm_constant_pool_bucket_t; |