Lichen

Changeset

880:9c9bd4fccad4
2019-01-27 Paul Boddie raw files shortlog changelog graph Experiment with preallocation of floating point instances. float-preallocation
templates/native/float.c (file) templates/progops.h (file)
     1.1 --- a/templates/native/float.c	Sat Jan 26 21:38:12 2019 +0100
     1.2 +++ b/templates/native/float.c	Sun Jan 27 01:08:28 2019 +0100
     1.3 @@ -28,6 +28,69 @@
     1.4  #include "progtypes.h"
     1.5  #include "main.h"
     1.6  
     1.7 +/* A table of preallocated float instances. */
     1.8 +
     1.9 +struct float_table
    1.10 +{
    1.11 +    struct float_table *next;
    1.12 +    char data[];
    1.13 +};
    1.14 +
    1.15 +static struct float_table *next_float = 0;
    1.16 +
    1.17 +/* Preallocate some float instances. */
    1.18 +
    1.19 +static void preallocate_floats(int num)
    1.20 +{
    1.21 +    struct float_table *latest;
    1.22 +    int i;
    1.23 +
    1.24 +    for (i = 0; i < num; i++)
    1.25 +    {
    1.26 +        /* Allocate a table entry. */
    1.27 +
    1.28 +        latest = (struct float_table *)
    1.29 +                 __ALLOCATE(1, sizeof(struct float_table *) +
    1.30 +                               __INSTANCESIZE(__builtins___float_float));
    1.31 +
    1.32 +        /* Reference the last entry from the new entry. */
    1.33 +
    1.34 +        latest->next = next_float;
    1.35 +        next_float = latest;
    1.36 +    }
    1.37 +}
    1.38 +
    1.39 +static __attr new_float(double n)
    1.40 +{
    1.41 +    struct float_table *this_float;
    1.42 +    __attr attr;
    1.43 +
    1.44 +    if (!next_float)
    1.45 +        preallocate_floats(1000);
    1.46 +
    1.47 +    /* Reference the next preallocated entry. */
    1.48 +
    1.49 +    this_float = next_float;
    1.50 +
    1.51 +    /* Initialise the embedded instance. */
    1.52 +
    1.53 +    __init((__ref) &this_float->data,
    1.54 +           &__INSTANCETABLE(__builtins___float_float),
    1.55 +           &__builtins___float_float);
    1.56 +
    1.57 +    /* Populate the float with the value. */
    1.58 +
    1.59 +    attr = __ATTRVALUE(&this_float->data);
    1.60 +    __set_trailing_data(attr, __builtins___float_float, n);
    1.61 +
    1.62 +    /* Make the next entry available and detach it from this one. */
    1.63 +
    1.64 +    next_float = this_float->next;
    1.65 +    this_float->next = 0;
    1.66 +
    1.67 +    return attr;
    1.68 +}
    1.69 +
    1.70  /* Conversion of trailing data to a double-precision floating point number. */
    1.71  
    1.72  static double __TOFLOAT(__attr attr)
    1.73 @@ -70,7 +133,7 @@
    1.74      /* self and other interpreted as float */
    1.75      double i = __TOFLOAT(self);
    1.76      double j = __TOFLOAT(other);
    1.77 -    return __new_float(i + j);
    1.78 +    return new_float(i + j);
    1.79  }
    1.80  
    1.81  __attr __fn_native_float_float_sub(__attr __self, __attr self, __attr other)
    1.82 @@ -78,7 +141,7 @@
    1.83      /* self and other interpreted as float */
    1.84      double i = __TOFLOAT(self);
    1.85      double j = __TOFLOAT(other);
    1.86 -    return __new_float(i - j);
    1.87 +    return new_float(i - j);
    1.88  }
    1.89  
    1.90  __attr __fn_native_float_float_mul(__attr __self, __attr self, __attr other)
    1.91 @@ -86,7 +149,7 @@
    1.92      /* self and other interpreted as float */
    1.93      double i = __TOFLOAT(self);
    1.94      double j = __TOFLOAT(other);
    1.95 -    return __new_float(i * j);
    1.96 +    return new_float(i * j);
    1.97  }
    1.98  
    1.99  __attr __fn_native_float_float_div(__attr __self, __attr self, __attr other)
   1.100 @@ -94,7 +157,7 @@
   1.101      /* self and other interpreted as float */
   1.102      double i = __TOFLOAT(self);
   1.103      double j = __TOFLOAT(other);
   1.104 -    return __new_float(i / j);
   1.105 +    return new_float(i / j);
   1.106  }
   1.107  
   1.108  __attr __fn_native_float_float_mod(__attr __self, __attr self, __attr other)
   1.109 @@ -102,14 +165,14 @@
   1.110      /* self and other interpreted as float */
   1.111      double i = __TOFLOAT(self);
   1.112      double j = __TOFLOAT(other);
   1.113 -    return __new_float(fmod(i, j));
   1.114 +    return new_float(fmod(i, j));
   1.115  }
   1.116  
   1.117  __attr __fn_native_float_float_neg(__attr __self, __attr self)
   1.118  {
   1.119      /* self interpreted as float */
   1.120      double i = __TOFLOAT(self);
   1.121 -    return __new_float(-i);
   1.122 +    return new_float(-i);
   1.123  }
   1.124  
   1.125  __attr __fn_native_float_float_pow(__attr __self, __attr self, __attr other)
   1.126 @@ -128,7 +191,7 @@
   1.127          __raise_overflow_error();
   1.128  
   1.129      /* Return the result. */
   1.130 -    return __new_float(result);
   1.131 +    return new_float(result);
   1.132  }
   1.133  
   1.134  __attr __fn_native_float_float_le(__attr __self, __attr self, __attr other)
     2.1 --- a/templates/progops.h	Sat Jan 26 21:38:12 2019 +0100
     2.2 +++ b/templates/progops.h	Sun Jan 27 01:08:28 2019 +0100
     2.3 @@ -25,6 +25,7 @@
     2.4  
     2.5  /* Generic instantiation operations, defining common members. */
     2.6  
     2.7 +void __init(__ref obj, const __table * table, __ref cls);
     2.8  __attr __new(const __table *table, __ref cls, size_t size, int immutable);
     2.9  __attr __new_wrapper(__attr context, __attr attr);
    2.10