mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-02-11 09:25:20 +00:00
Use quicksort to sort native_symbols (#324)
This commit is contained in:
parent
93ca9d8c62
commit
cc05f8fb1c
|
@ -7,6 +7,12 @@
|
|||
#include "wasm_runtime_common.h"
|
||||
#include "bh_log.h"
|
||||
|
||||
#define ENABLE_QUICKSORT 1
|
||||
#define ENABLE_SORT_DEBUG 0
|
||||
|
||||
#if ENABLE_SORT_DEBUG != 0
|
||||
#include<sys/time.h>
|
||||
#endif
|
||||
|
||||
static NativeSymbolsList g_native_symbols_list = NULL;
|
||||
static NativeSymbolsList g_native_symbols_list_end = NULL;
|
||||
|
@ -103,6 +109,7 @@ check_symbol_signature(const WASMType *type, const char *signature)
|
|||
return true;
|
||||
}
|
||||
|
||||
#if ENABLE_QUICKSORT == 0
|
||||
static void
|
||||
sort_symbol_ptr(NativeSymbol *native_symbols, uint32 n_native_symbols)
|
||||
{
|
||||
|
@ -120,6 +127,56 @@ sort_symbol_ptr(NativeSymbol *native_symbols, uint32 n_native_symbols)
|
|||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
static void
|
||||
swap_symbol(NativeSymbol* left, NativeSymbol* right)
|
||||
{
|
||||
NativeSymbol temp = *left;
|
||||
*left = *right;
|
||||
*right = temp;
|
||||
}
|
||||
|
||||
static void
|
||||
quick_sort_symbols(NativeSymbol* native_symbols, int left, int right)
|
||||
{
|
||||
NativeSymbol base_symbol;
|
||||
int pin_left = left;
|
||||
int pin_right = right;
|
||||
|
||||
if (left >= right) {
|
||||
return;
|
||||
}
|
||||
|
||||
base_symbol = native_symbols[left];
|
||||
while (left < right) {
|
||||
while (left < right
|
||||
&& strcmp(native_symbols[right].symbol,
|
||||
base_symbol.symbol) > 0) {
|
||||
right--;
|
||||
}
|
||||
|
||||
if (left < right) {
|
||||
swap_symbol(&native_symbols[left], &native_symbols[right]);
|
||||
left++;
|
||||
}
|
||||
|
||||
while (left < right
|
||||
&& strcmp(native_symbols[left].symbol,
|
||||
base_symbol.symbol) < 0) {
|
||||
left++;
|
||||
}
|
||||
|
||||
if (left < right) {
|
||||
swap_symbol(&native_symbols[left], &native_symbols[right]);
|
||||
right--;
|
||||
}
|
||||
}
|
||||
native_symbols[left] = base_symbol;
|
||||
|
||||
quick_sort_symbols(native_symbols, pin_left, left - 1);
|
||||
quick_sort_symbols(native_symbols, left + 1, pin_right);
|
||||
}
|
||||
#endif /* end of ENABLE_QUICKSORT */
|
||||
|
||||
static void *
|
||||
lookup_symbol(NativeSymbol *native_symbols, uint32 n_native_symbols,
|
||||
|
@ -205,6 +262,11 @@ register_natives(const char *module_name,
|
|||
bool call_conv_raw)
|
||||
{
|
||||
NativeSymbolsNode *node;
|
||||
#if ENABLE_SORT_DEBUG != 0
|
||||
struct timeval start;
|
||||
struct timeval end;
|
||||
unsigned long timer;
|
||||
#endif
|
||||
|
||||
if (!(node = wasm_runtime_malloc(sizeof(NativeSymbolsNode))))
|
||||
return false;
|
||||
|
@ -223,7 +285,23 @@ register_natives(const char *module_name,
|
|||
g_native_symbols_list = g_native_symbols_list_end = node;
|
||||
}
|
||||
|
||||
#if ENABLE_SORT_DEBUG != 0
|
||||
gettimeofday(&start, NULL);
|
||||
#endif
|
||||
|
||||
#if ENABLE_QUICKSORT == 0
|
||||
sort_symbol_ptr(native_symbols, n_native_symbols);
|
||||
#else
|
||||
quick_sort_symbols(native_symbols, 0, (int)(n_native_symbols - 1));
|
||||
#endif
|
||||
|
||||
#if ENABLE_SORT_DEBUG != 0
|
||||
gettimeofday(&end, NULL);
|
||||
timer = 1000000 * (end.tv_sec - start.tv_sec)
|
||||
+ (end.tv_usec - start.tv_usec);
|
||||
LOG_ERROR("module_name: %s, nums: %d, sorted used: %ld us",
|
||||
module_name, n_native_symbols, timer);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -321,4 +399,3 @@ wasm_native_destroy()
|
|||
|
||||
g_native_symbols_list = g_native_symbols_list_end = NULL;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user