@cryptotaxi247 / netdata-1 / commits / 83788e6d1

Improve storage number unpacking by using a lookup table. (#11048)

The LUT contains precomputed values of multiplier/divisors that are used to unpack storage numbers into calculated numbers efficiently.

vkalintiris committed May 5, 2022 at 14:17 UTC 83788e6d1c316ff0a299d7eae4fe8e8b36c218ec
1 file changed +22 -15
libnetdata/storage_number/storage_number.c
+22 -15
@@ -91,15 +91,32 @@ RET_SN:
91 return r;
92 }
93
94 +// Lookup table to make storage number unpacking efficient.
95 +static calculated_number lut10x[4 * 8];
96 +
97 +__attribute__((constructor)) void initialize_lut(void) {
98 + // The lookup table is partitioned in 4 subtables based on the
99 + // values of the factor and exp bits.
100 + for (int i = 0; i < 8; i++) {
101 + // factor = 0
102 + lut10x[0 * 8 + i] = 1 / pow(10, i); // exp = 0
103 + lut10x[1 * 8 + i] = pow(10, i); // exp = 1
104 +
105 + // factor = 1
106 + lut10x[2 * 8 + i] = 1 / pow(100, i); // exp = 0
107 + lut10x[3 * 8 + i] = pow(100, i); // exp = 1
108 + }
109 +}
110 +
111 calculated_number unpack_storage_number(storage_number value) {
112 if(!value) return 0;
113
97 - int sign = 0, exp = 0;
98 - int factor = 10;
114 + int sign = 1, exp = 0;
115 + int factor = 0;
116
117 // bit 32 = 0:positive, 1:negative
118 if(unlikely(value & (1 << 31)))
102 - sign = 1;
119 + sign = -1;
120
121 // bit 31 = 0:divide, 1:multiply
122 if(unlikely(value & (1 << 30)))
@@ -107,7 +124,7 @@ calculated_number unpack_storage_number(storage_number value) {
124
125 // bit 27 SN_EXISTS_100
126 if(unlikely(value & (1 << 26)))
110 - factor = 100;
127 + factor = 1;
128
129 // bit 26 SN_EXISTS_RESET
130 // bit 25 SN_ANOMALY_BIT
@@ -122,17 +139,7 @@ calculated_number unpack_storage_number(storage_number value) {
139
140 // fprintf(stderr, "UNPACK: %08X, sign = %d, exp = %d, mul = %d, factor = %d, n = " CALCULATED_NUMBER_FORMAT "\n", value, sign, exp, mul, factor, n);
141
125 - if(exp) {
126 - for(; mul; mul--)
127 - n *= factor;
128 - }
129 - else {
130 - for( ; mul ; mul--)
131 - n /= 10;
132 - }
133 -
134 - if(sign) n = -n;
135 - return n;
142 + return sign * lut10x[(factor * 16) + (exp * 8) + mul] * n;
143 }
144
145 /*