Skip to content

Commit 2cd7a78

Browse files
committed
optimize allocation of small null<int>
1 parent 3c9e352 commit 2cd7a78

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

src/std/cast.c

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,30 @@ static void invalid_cast( hl_type *from, hl_type *to ) {
2828
hl_error("Can't cast %s to %s",hl_type_str(from),hl_type_str(to));
2929
}
3030

31+
32+
static vdynamic static_ints[256];
33+
static bool static_ints_init = false;
34+
35+
static vdynamic *hl_dyni32( int v ) {
36+
char b = (char)v;
37+
if( b == v ) {
38+
if( !static_ints_init ) {
39+
int i;
40+
for(i=-128;i<128;i++) {
41+
static_ints[i&0xFF].t = &hlt_i32;
42+
static_ints[i&0xFF].v.i = i;
43+
}
44+
static_ints_init = true;
45+
}
46+
return &static_ints[v&0xFF];
47+
}
48+
vdynamic *d = (vdynamic*)hl_gc_alloc_noptr(sizeof(vdynamic));
49+
d->t = &hlt_i32;
50+
d->v.i = v;
51+
return d;
52+
53+
}
54+
3155
HL_PRIM vdynamic *hl_make_dyn( void *data, hl_type *t ) {
3256
vdynamic *v;
3357
switch( t->kind ) {
@@ -44,10 +68,7 @@ HL_PRIM vdynamic *hl_make_dyn( void *data, hl_type *t ) {
4468
v->v.i = *(unsigned short*)data;
4569
return v;
4670
case HI32:
47-
v = (vdynamic*)hl_gc_alloc_noptr(sizeof(vdynamic));
48-
v->t = t;
49-
v->v.i = *(int*)data;
50-
return v;
71+
return hl_dyni32(*(int*)data);
5172
case HI64:
5273
case HGUID:
5374
v = (vdynamic*)hl_gc_alloc_noptr(sizeof(vdynamic));
@@ -529,12 +550,6 @@ static vdynamic *hl_dynf64( double v ) {
529550
return d;
530551
}
531552

532-
static vdynamic *hl_dyni32( int v ) {
533-
vdynamic *d = hl_alloc_dynamic(&hlt_i32);
534-
d->v.i = v;
535-
return d;
536-
}
537-
538553
static bool is_number( hl_type *t ) {
539554
return t->kind >= HUI8 && t->kind <= HBOOL;
540555
}

0 commit comments

Comments
 (0)