# HG changeset patch # User Paul Boddie # Date 1548547708 -3600 # Node ID 9c9bd4fccad4e817b657077c5ff06a67d93c5aba # Parent c77216b79bebf428513d0a51b3754ad88e6d29ab Experiment with preallocation of floating point instances. diff -r c77216b79beb -r 9c9bd4fccad4 templates/native/float.c --- a/templates/native/float.c Sat Jan 26 21:38:12 2019 +0100 +++ b/templates/native/float.c Sun Jan 27 01:08:28 2019 +0100 @@ -28,6 +28,69 @@ #include "progtypes.h" #include "main.h" +/* A table of preallocated float instances. */ + +struct float_table +{ + struct float_table *next; + char data[]; +}; + +static struct float_table *next_float = 0; + +/* Preallocate some float instances. */ + +static void preallocate_floats(int num) +{ + struct float_table *latest; + int i; + + for (i = 0; i < num; i++) + { + /* Allocate a table entry. */ + + latest = (struct float_table *) + __ALLOCATE(1, sizeof(struct float_table *) + + __INSTANCESIZE(__builtins___float_float)); + + /* Reference the last entry from the new entry. */ + + latest->next = next_float; + next_float = latest; + } +} + +static __attr new_float(double n) +{ + struct float_table *this_float; + __attr attr; + + if (!next_float) + preallocate_floats(1000); + + /* Reference the next preallocated entry. */ + + this_float = next_float; + + /* Initialise the embedded instance. */ + + __init((__ref) &this_float->data, + &__INSTANCETABLE(__builtins___float_float), + &__builtins___float_float); + + /* Populate the float with the value. */ + + attr = __ATTRVALUE(&this_float->data); + __set_trailing_data(attr, __builtins___float_float, n); + + /* Make the next entry available and detach it from this one. */ + + next_float = this_float->next; + this_float->next = 0; + + return attr; +} + /* Conversion of trailing data to a double-precision floating point number. */ static double __TOFLOAT(__attr attr) @@ -70,7 +133,7 @@ /* self and other interpreted as float */ double i = __TOFLOAT(self); double j = __TOFLOAT(other); - return __new_float(i + j); + return new_float(i + j); } __attr __fn_native_float_float_sub(__attr __self, __attr self, __attr other) @@ -78,7 +141,7 @@ /* self and other interpreted as float */ double i = __TOFLOAT(self); double j = __TOFLOAT(other); - return __new_float(i - j); + return new_float(i - j); } __attr __fn_native_float_float_mul(__attr __self, __attr self, __attr other) @@ -86,7 +149,7 @@ /* self and other interpreted as float */ double i = __TOFLOAT(self); double j = __TOFLOAT(other); - return __new_float(i * j); + return new_float(i * j); } __attr __fn_native_float_float_div(__attr __self, __attr self, __attr other) @@ -94,7 +157,7 @@ /* self and other interpreted as float */ double i = __TOFLOAT(self); double j = __TOFLOAT(other); - return __new_float(i / j); + return new_float(i / j); } __attr __fn_native_float_float_mod(__attr __self, __attr self, __attr other) @@ -102,14 +165,14 @@ /* self and other interpreted as float */ double i = __TOFLOAT(self); double j = __TOFLOAT(other); - return __new_float(fmod(i, j)); + return new_float(fmod(i, j)); } __attr __fn_native_float_float_neg(__attr __self, __attr self) { /* self interpreted as float */ double i = __TOFLOAT(self); - return __new_float(-i); + return new_float(-i); } __attr __fn_native_float_float_pow(__attr __self, __attr self, __attr other) @@ -128,7 +191,7 @@ __raise_overflow_error(); /* Return the result. */ - return __new_float(result); + return new_float(result); } __attr __fn_native_float_float_le(__attr __self, __attr self, __attr other) diff -r c77216b79beb -r 9c9bd4fccad4 templates/progops.h --- a/templates/progops.h Sat Jan 26 21:38:12 2019 +0100 +++ b/templates/progops.h Sun Jan 27 01:08:28 2019 +0100 @@ -25,6 +25,7 @@ /* Generic instantiation operations, defining common members. */ +void __init(__ref obj, const __table * table, __ref cls); __attr __new(const __table *table, __ref cls, size_t size, int immutable); __attr __new_wrapper(__attr context, __attr attr);