Lichen

Changeset

881:31053f0bb63b
2019-01-27 Paul Boddie raw files shortlog changelog graph Experiment with a statically-allocated table of preallocated instances, also marking the instances as "atomic". float-preallocation
templates/native/float.c (file)
     1.1 --- a/templates/native/float.c	Sun Jan 27 01:08:28 2019 +0100
     1.2 +++ b/templates/native/float.c	Sun Jan 27 14:11:25 2019 +0100
     1.3 @@ -30,64 +30,50 @@
     1.4  
     1.5  /* A table of preallocated float instances. */
     1.6  
     1.7 -struct float_table
     1.8 -{
     1.9 -    struct float_table *next;
    1.10 -    char data[];
    1.11 -};
    1.12 +#define NUM_PREALLOCATED 1000
    1.13  
    1.14 -static struct float_table *next_float = 0;
    1.15 +__ref preallocated_floats[NUM_PREALLOCATED];
    1.16 +
    1.17 +static int floats_left = 0;
    1.18  
    1.19  /* Preallocate some float instances. */
    1.20  
    1.21 -static void preallocate_floats(int num)
    1.22 +static void preallocate_floats()
    1.23  {
    1.24 -    struct float_table *latest;
    1.25      int i;
    1.26  
    1.27 -    for (i = 0; i < num; i++)
    1.28 +    for (i = 0; i < NUM_PREALLOCATED; i++)
    1.29      {
    1.30 -        /* Allocate a table entry. */
    1.31 +        /* Allocate a float for each table entry. */
    1.32  
    1.33 -        latest = (struct float_table *)
    1.34 -                 __ALLOCATE(1, sizeof(struct float_table *) +
    1.35 -                               __INSTANCESIZE(__builtins___float_float));
    1.36 +        preallocated_floats[i] = \
    1.37 +            __ALLOCATEIM(1, __INSTANCESIZE(__builtins___float_float));
    1.38 +    }
    1.39  
    1.40 -        /* Reference the last entry from the new entry. */
    1.41 -
    1.42 -        latest->next = next_float;
    1.43 -        next_float = latest;
    1.44 -    }
    1.45 +    floats_left = NUM_PREALLOCATED;
    1.46  }
    1.47  
    1.48  static __attr new_float(double n)
    1.49  {
    1.50 -    struct float_table *this_float;
    1.51 +    __ref obj;
    1.52      __attr attr;
    1.53  
    1.54 -    if (!next_float)
    1.55 -        preallocate_floats(1000);
    1.56 -
    1.57 -    /* Reference the next preallocated entry. */
    1.58 +    if (!floats_left)
    1.59 +        preallocate_floats();
    1.60  
    1.61 -    this_float = next_float;
    1.62 -
    1.63 -    /* Initialise the embedded instance. */
    1.64 +    floats_left--;
    1.65 +    obj = preallocated_floats[floats_left];
    1.66  
    1.67 -    __init((__ref) &this_float->data,
    1.68 -           &__INSTANCETABLE(__builtins___float_float),
    1.69 -           &__builtins___float_float);
    1.70 +    /* Initialise the instance. */
    1.71 +
    1.72 +    __init(obj, &__INSTANCETABLE(__builtins___float_float),
    1.73 +                &__builtins___float_float);
    1.74  
    1.75      /* Populate the float with the value. */
    1.76  
    1.77 -    attr = __ATTRVALUE(&this_float->data);
    1.78 +    attr = __ATTRVALUE(obj);
    1.79      __set_trailing_data(attr, __builtins___float_float, n);
    1.80  
    1.81 -    /* Make the next entry available and detach it from this one. */
    1.82 -
    1.83 -    next_float = this_float->next;
    1.84 -    this_float->next = 0;
    1.85 -
    1.86      return attr;
    1.87  }
    1.88