1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-28 07:03:00 +08:00

perf(array): optimize array remove / erase function performance (#6544)

Signed-off-by: zhangjipeng <zhangjipeng@xiaomi.com>
Co-authored-by: zhangjipeng <zhangjipeng@xiaomi.com>
This commit is contained in:
Zhang Ji Peng 2024-07-26 17:58:21 +08:00 committed by GitHub
parent a2fb2808ba
commit 34742a8db6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 4 deletions

View File

@ -67,6 +67,13 @@ void lv_array_copy(lv_array_t * target, const lv_array_t * source)
target->size = source->size;
}
void lv_array_shrink(lv_array_t * array)
{
if(array->size <= array->capacity / LV_ARRAY_DEFAULT_SHRINK_RATIO) {
lv_array_resize(array, array->size);
}
}
lv_result_t lv_array_remove(lv_array_t * array, uint32_t index)
{
if(index >= array->size) {
@ -75,7 +82,8 @@ lv_result_t lv_array_remove(lv_array_t * array, uint32_t index)
/*Shortcut*/
if(index == array->size - 1) {
lv_array_resize(array, array->size - 1);
array->size--;
lv_array_shrink(array);
return LV_RESULT_OK;
}
@ -83,7 +91,8 @@ lv_result_t lv_array_remove(lv_array_t * array, uint32_t index)
uint8_t * remaining = start + array->element_size;
uint32_t remaining_size = (array->size - index - 1) * array->element_size;
lv_memmove(start, remaining, remaining_size);
lv_array_resize(array, array->size - 1);
array->size--;
lv_array_shrink(array);
return LV_RESULT_OK;
}
@ -99,7 +108,8 @@ lv_result_t lv_array_erase(lv_array_t * array, uint32_t start, uint32_t end)
/*Shortcut*/
if(end == array->size) {
lv_array_resize(array, start);
array->size = start;
lv_array_shrink(array);
return LV_RESULT_OK;
}
@ -107,7 +117,8 @@ lv_result_t lv_array_erase(lv_array_t * array, uint32_t start, uint32_t end)
uint8_t * remaining = start_p + (end - start) * array->element_size;
uint32_t remaining_size = (array->size - end) * array->element_size;
lv_memcpy(start_p, remaining, remaining_size);
lv_array_resize(array, array->size - (end - start));
array->size -= (end - start);
lv_array_shrink(array);
return LV_RESULT_OK;
}

View File

@ -23,6 +23,10 @@ extern "C" {
#define LV_ARRAY_DEFAULT_CAPACITY 4
#endif
#ifndef LV_ARRAY_DEFAULT_SHRINK_RATIO
#define LV_ARRAY_DEFAULT_SHRINK_RATIO 2
#endif
/**********************
* TYPEDEFS
**********************/
@ -118,6 +122,12 @@ static inline void lv_array_clear(lv_array_t * array)
array->size = 0;
}
/**
* Shrink the memory capacity of array if necessary.
* @param array pointer to an `lv_array_t` variable
*/
void lv_array_shrink(lv_array_t * array);
/**
* Remove the element at the specified position in the array.
* @param array pointer to an `lv_array_t` variable