|
|
@@ -63,6 +63,7 @@ typedef struct Ctx {
|
|
|
bool is_compiling_class;
|
|
|
c11_vector /*T=Expr* */ s_expr;
|
|
|
c11_smallmap_n2i global_names;
|
|
|
+ c11_smallmap_s2n co_consts_string_dedup_map;
|
|
|
} Ctx;
|
|
|
|
|
|
typedef struct Expr Expr;
|
|
|
@@ -1080,6 +1081,7 @@ static void Ctx__ctor(Ctx* self, CodeObject* co, FuncDecl* func, int level) {
|
|
|
self->is_compiling_class = false;
|
|
|
c11_vector__ctor(&self->s_expr, sizeof(Expr*));
|
|
|
c11_smallmap_n2i__ctor(&self->global_names);
|
|
|
+ c11_smallmap_s2n__ctor(&self->co_consts_string_dedup_map);
|
|
|
}
|
|
|
|
|
|
static void Ctx__dtor(Ctx* self) {
|
|
|
@@ -1089,6 +1091,12 @@ static void Ctx__dtor(Ctx* self) {
|
|
|
}
|
|
|
c11_vector__dtor(&self->s_expr);
|
|
|
c11_smallmap_n2i__dtor(&self->global_names);
|
|
|
+ // free the dedup map
|
|
|
+ c11__foreach(c11_smallmap_s2n_KV, &self->co_consts_string_dedup_map, p_kv) {
|
|
|
+ const char* p = p_kv->key.data;
|
|
|
+ PK_FREE((void*)p);
|
|
|
+ }
|
|
|
+ c11_smallmap_s2n__dtor(&self->co_consts_string_dedup_map);
|
|
|
}
|
|
|
|
|
|
static int Ctx__prepare_loop_divert(Ctx* self, int line, bool is_break) {
|
|
|
@@ -1198,10 +1206,28 @@ static int Ctx__add_varname(Ctx* self, py_Name name) {
|
|
|
}
|
|
|
|
|
|
static int Ctx__add_const_string(Ctx* self, c11_sv key) {
|
|
|
- py_Ref p = c11_vector__emplace(&self->co->consts);
|
|
|
- py_newstrv(p, key);
|
|
|
- int index = self->co->consts.length - 1;
|
|
|
- return index;
|
|
|
+ if(key.size > 100) {
|
|
|
+ py_Ref tmp = c11_vector__emplace(&self->co->consts);
|
|
|
+ py_newstrv(tmp, key);
|
|
|
+ int index = self->co->consts.length - 1;
|
|
|
+ return index;
|
|
|
+ }
|
|
|
+ uint16_t* val = c11_smallmap_s2n__try_get(&self->co_consts_string_dedup_map, key);
|
|
|
+ if(val) {
|
|
|
+ return *val;
|
|
|
+ } else {
|
|
|
+ py_Ref tmp = c11_vector__emplace(&self->co->consts);
|
|
|
+ py_newstrv(tmp, key);
|
|
|
+ int index = self->co->consts.length - 1;
|
|
|
+ // dedup
|
|
|
+ char* new_buf = PK_MALLOC(key.size + 1);
|
|
|
+ memcpy(new_buf, key.data, key.size);
|
|
|
+ new_buf[key.size] = 0;
|
|
|
+ c11_smallmap_s2n__set(&self->co_consts_string_dedup_map,
|
|
|
+ (c11_sv){new_buf, key.size},
|
|
|
+ index);
|
|
|
+ return index;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
static int Ctx__add_const(Ctx* self, py_Ref v) {
|