Issue #1307 - Part 4: Stop using variable-length arrays.

"USING VLA'S IS ACTIVELY STUPID! It generates much more code, and much
slower code (and more fragile code), than just using a fixed key size
would have done." -- Linus Torvalds
This commit is contained in:
wolfbeast 2019-11-30 15:42:59 +01:00 • committed by Roy Tam
commit 442f6b3252
2 changed files with 20 additions and 38 deletions

View file

@ -381,10 +381,6 @@ void *_mmap(void *addr, size_t length, int prot, int flags,
#define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var)) #define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var))
#endif #endif
#ifdef MOZ_MEMORY_WINDOWS
/* MSVC++ does not support C99 variable-length arrays. */
# define RB_NO_C99_VARARRAYS
#endif
#include "rb.h" #include "rb.h"
#ifdef MALLOC_DEBUG #ifdef MALLOC_DEBUG

View file

@ -36,7 +36,6 @@
* (Optional.) * (Optional.)
* #define SIZEOF_PTR ... * #define SIZEOF_PTR ...
* #define SIZEOF_PTR_2POW ... * #define SIZEOF_PTR_2POW ...
* #define RB_NO_C99_VARARRAYS
* *
* (Optional, see assert(3).) * (Optional, see assert(3).)
* #define NDEBUG * #define NDEBUG
@ -769,39 +768,26 @@ a_prefix##remove(a_tree_type *tree, a_type *node) { \
* effort. * effort.
*/ */
#ifdef RB_NO_C99_VARARRAYS /*
/* * Avoid using variable-length arrays.
* Avoid using variable-length arrays, at the cost of using more stack space. * Size the path arrays such that they are always large enough, even if a
* Size the path arrays such that they are always large enough, even if a * tree consumes all of memory. Since each node must contain a minimum of
* tree consumes all of memory. Since each node must contain a minimum of * two pointers, there can never be more nodes than:
* two pointers, there can never be more nodes than: *
* * 1 << ((SIZEOF_PTR<<3) - (SIZEOF_PTR_2POW+1))
* 1 << ((SIZEOF_PTR<<3) - (SIZEOF_PTR_2POW+1)) *
* * Since the depth of a tree is limited to 3*lg(#nodes), the maximum depth
* Since the depth of a tree is limited to 3*lg(#nodes), the maximum depth * is:
* is: *
* * (3 * ((SIZEOF_PTR<<3) - (SIZEOF_PTR_2POW+1)))
* (3 * ((SIZEOF_PTR<<3) - (SIZEOF_PTR_2POW+1))) *
* * This works out to a maximum depth of 87 and 180 for 32- and 64-bit
* This works out to a maximum depth of 87 and 180 for 32- and 64-bit * systems, respectively (approximatly 348 and 1440 bytes, respectively).
* systems, respectively (approximatly 348 and 1440 bytes, respectively). */
*/ #define rbp_compute_f_height(a_type, a_field, a_tree)
# define rbp_compute_f_height(a_type, a_field, a_tree) #define rbp_f_height (3 * ((SIZEOF_PTR<<3) - (SIZEOF_PTR_2POW+1)))
# define rbp_f_height (3 * ((SIZEOF_PTR<<3) - (SIZEOF_PTR_2POW+1))) #define rbp_compute_fr_height(a_type, a_field, a_tree)
# define rbp_compute_fr_height(a_type, a_field, a_tree) #define rbp_fr_height (3 * ((SIZEOF_PTR<<3) - (SIZEOF_PTR_2POW+1)))
# define rbp_fr_height (3 * ((SIZEOF_PTR<<3) - (SIZEOF_PTR_2POW+1)))
#else
# define rbp_compute_f_height(a_type, a_field, a_tree) \
/* Compute the maximum possible tree depth (3X the black height). */\
unsigned rbp_f_height; \
rbp_black_height(a_type, a_field, a_tree, rbp_f_height); \
rbp_f_height *= 3;
# define rbp_compute_fr_height(a_type, a_field, a_tree) \
/* Compute the maximum possible tree depth (3X the black height). */\
unsigned rbp_fr_height; \
rbp_black_height(a_type, a_field, a_tree, rbp_fr_height); \
rbp_fr_height *= 3;
#endif
#define rb_foreach_begin(a_type, a_field, a_tree, a_var) { \ #define rb_foreach_begin(a_type, a_field, a_tree, a_var) { \
rbp_compute_f_height(a_type, a_field, a_tree) \ rbp_compute_f_height(a_type, a_field, a_tree) \