Skip to content

Commit 41e976d

Browse files
hujun260xiaoxiang781216
authored andcommitted
libs/libc/queue: inline queue list to improve performance
add a config CONFIG_LIBC_INLINE_QUEUE to inline the queue list test: We can use qemu for testing. compiling make distclean -j20; ./tools/configure.sh -l qemu-armv8a:nsh_smp ;make -j20 running qemu-system-aarch64 -cpu cortex-a53 -smp 4 -nographic -machine virt,virtualization=on,gic-version=3 -net none -chardev stdio,id=con,mux=on -serial chardev:con -mon chardev=con,mode=readline -kernel ./nuttx Signed-off-by: hujun5 <[email protected]>
1 parent 1e57a56 commit 41e976d

File tree

15 files changed

+251
-551
lines changed

15 files changed

+251
-551
lines changed

include/nuttx/queue.h

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@
3131
* Pre-processor Definitions
3232
****************************************************************************/
3333

34+
#ifdef CONFIG_LIBC_INLINE_QUEUE
35+
# ifndef STATIC_INLINE
36+
# define STATIC_INLINE static inline_function
37+
# endif
38+
#else
39+
# define STATIC_INLINE
40+
#endif
41+
3442
#define sq_init(q) \
3543
do \
3644
{ \
@@ -343,24 +351,241 @@ extern "C"
343351

344352
/* Add nodes to queues */
345353

354+
#ifndef CONFIG_LIBC_INLINE_QUEUE
346355
void sq_addafter(FAR sq_entry_t *prev, FAR sq_entry_t *node,
347356
FAR sq_queue_t *queue);
357+
#else
358+
STATIC_INLINE void sq_addafter(FAR sq_entry_t *prev, FAR sq_entry_t *node,
359+
FAR sq_queue_t *queue)
360+
{
361+
if (!queue->head || prev == queue->tail)
362+
{
363+
sq_addlast(node, queue);
364+
}
365+
else
366+
{
367+
node->flink = prev->flink;
368+
prev->flink = node;
369+
}
370+
}
371+
#endif
372+
373+
#ifndef CONFIG_LIBC_INLINE_QUEUE
348374
void dq_addafter(FAR dq_entry_t *prev, FAR dq_entry_t *node,
349375
FAR dq_queue_t *queue);
376+
#else
377+
STATIC_INLINE void dq_addafter(FAR dq_entry_t *prev, FAR dq_entry_t *node,
378+
FAR dq_queue_t *queue)
379+
{
380+
if (!queue->head || prev == queue->tail)
381+
{
382+
dq_addlast(node, queue);
383+
}
384+
else
385+
{
386+
FAR dq_entry_t *next = prev->flink;
387+
node->blink = prev;
388+
node->flink = next;
389+
next->blink = node;
390+
prev->flink = node;
391+
}
392+
}
393+
#endif
350394

351395
/* Remove nodes from queues */
352396

397+
#ifndef CONFIG_LIBC_INLINE_QUEUE
353398
FAR sq_entry_t *sq_remafter(FAR sq_entry_t *node, FAR sq_queue_t *queue);
399+
#else
400+
STATIC_INLINE FAR sq_entry_t *sq_remafter(FAR sq_entry_t *node,
401+
FAR sq_queue_t *queue)
402+
{
403+
FAR sq_entry_t *ret = node->flink;
404+
405+
if (queue->head && ret)
406+
{
407+
if (queue->tail == ret)
408+
{
409+
queue->tail = node;
410+
node->flink = NULL;
411+
}
412+
else
413+
{
414+
node->flink = ret->flink;
415+
}
416+
417+
ret->flink = NULL;
418+
}
419+
420+
return ret;
421+
}
422+
#endif
423+
424+
#ifndef CONFIG_LIBC_INLINE_QUEUE
354425
FAR dq_entry_t *dq_remafter(FAR dq_entry_t *node, FAR dq_queue_t *queue);
426+
#else
427+
STATIC_INLINE FAR dq_entry_t *dq_remafter(FAR dq_entry_t *node,
428+
FAR dq_queue_t *queue)
429+
{
430+
FAR dq_entry_t *ret = node->flink;
431+
432+
if (queue->head != NULL && ret != NULL)
433+
{
434+
dq_rem(ret, queue);
435+
}
436+
437+
return ret;
438+
}
439+
#endif
440+
441+
#ifndef CONFIG_LIBC_INLINE_QUEUE
355442
FAR sq_entry_t *sq_remlast(FAR sq_queue_t *queue);
443+
#else
444+
STATIC_INLINE FAR sq_entry_t *sq_remlast(FAR sq_queue_t *queue)
445+
{
446+
FAR sq_entry_t *ret = queue->tail;
447+
448+
if (ret)
449+
{
450+
if (queue->head == queue->tail)
451+
{
452+
queue->head = NULL;
453+
queue->tail = NULL;
454+
}
455+
else
456+
{
457+
FAR sq_entry_t *prev;
458+
for (prev = queue->head;
459+
prev && prev->flink != ret;
460+
prev = prev->flink);
461+
462+
if (prev)
463+
{
464+
prev->flink = NULL;
465+
queue->tail = prev;
466+
}
467+
}
468+
469+
ret->flink = NULL;
470+
}
471+
472+
return ret;
473+
}
474+
#endif
475+
476+
#ifndef CONFIG_LIBC_INLINE_QUEUE
356477
FAR dq_entry_t *dq_remlast(FAR dq_queue_t *queue);
478+
#else
479+
STATIC_INLINE FAR dq_entry_t *dq_remlast(FAR dq_queue_t *queue)
480+
{
481+
FAR dq_entry_t *ret = queue->tail;
482+
483+
if (ret)
484+
{
485+
FAR dq_entry_t *prev = ret->blink;
486+
if (!prev)
487+
{
488+
queue->head = NULL;
489+
queue->tail = NULL;
490+
}
491+
else
492+
{
493+
queue->tail = prev;
494+
prev->flink = NULL;
495+
}
496+
497+
ret->flink = NULL;
498+
ret->blink = NULL;
499+
}
500+
501+
return ret;
502+
}
503+
#endif
504+
505+
#ifndef CONFIG_LIBC_INLINE_QUEUE
357506
FAR sq_entry_t *sq_remfirst(FAR sq_queue_t *queue);
507+
#else
508+
STATIC_INLINE FAR sq_entry_t *sq_remfirst(FAR sq_queue_t *queue)
509+
{
510+
FAR sq_entry_t *ret = queue->head;
511+
512+
if (ret)
513+
{
514+
queue->head = ret->flink;
515+
if (!queue->head)
516+
{
517+
queue->tail = NULL;
518+
}
519+
520+
ret->flink = NULL;
521+
}
522+
523+
return ret;
524+
}
525+
#endif
526+
527+
#ifndef CONFIG_LIBC_INLINE_QUEUE
358528
FAR dq_entry_t *dq_remfirst(FAR dq_queue_t *queue);
529+
#else
530+
STATIC_INLINE FAR dq_entry_t *dq_remfirst(FAR dq_queue_t *queue)
531+
{
532+
FAR dq_entry_t *ret = queue->head;
533+
534+
if (ret)
535+
{
536+
FAR dq_entry_t *next = ret->flink;
537+
if (!next)
538+
{
539+
queue->head = NULL;
540+
queue->tail = NULL;
541+
}
542+
else
543+
{
544+
queue->head = next;
545+
next->blink = NULL;
546+
}
547+
548+
ret->flink = NULL;
549+
ret->blink = NULL;
550+
}
551+
552+
return ret;
553+
}
554+
#endif
359555

360556
/* Count nodes in queues */
361557

558+
#ifndef CONFIG_LIBC_INLINE_QUEUE
362559
size_t sq_count(FAR sq_queue_t *queue);
560+
#else
561+
STATIC_INLINE size_t sq_count(FAR sq_queue_t *queue)
562+
{
563+
FAR sq_entry_t *node;
564+
size_t count;
565+
566+
for (node = queue->head, count = 0;
567+
node != NULL;
568+
node = node->flink, count++);
569+
570+
return count;
571+
}
572+
#endif
573+
574+
#ifndef CONFIG_LIBC_INLINE_QUEUE
363575
size_t dq_count(FAR dq_queue_t *queue);
576+
#else
577+
STATIC_INLINE size_t dq_count(FAR dq_queue_t *queue)
578+
{
579+
FAR dq_entry_t *node;
580+
size_t count;
581+
582+
for (node = queue->head, count = 0;
583+
node != NULL;
584+
node = node->flink, count++);
585+
586+
return count;
587+
}
588+
#endif
364589

365590
#undef EXTERN
366591
#ifdef __cplusplus

libs/libc/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ source "libs/libc/stream/Kconfig"
3333
source "libs/libc/regex/Kconfig"
3434
source "libs/libc/gpsutils/Kconfig"
3535
source "libs/libc/fdt/Kconfig"
36+
source "libs/libc/queue/Kconfig"

libs/libc/queue/CMakeLists.txt

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,4 @@
1818
#
1919
# ##############################################################################
2020

21-
target_sources(
22-
c
23-
PRIVATE sq_addafter.c
24-
sq_remlast.c
25-
sq_remfirst.c
26-
sq_remafter.c
27-
sq_count.c
28-
dq_addafter.c
29-
dq_remlast.c
30-
dq_remfirst.c
31-
dq_remafter.c
32-
dq_count.c)
21+
target_sources(c PRIVATE queue.c)

libs/libc/queue/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#
2+
# For a description of the syntax of this configuration file,
3+
# see the file kconfig-language.txt in the NuttX tools repository.
4+
#
5+
6+
config LIBC_INLINE_QUEUE
7+
bool "Inline queue list to improve the performance"
8+
default !DEFAULT_SMALL
9+
---help---
10+
Inline queue list to improve the performance

libs/libc/queue/Make.defs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
# Add the queue C files to the build
2222

23-
CSRCS += sq_addafter.c sq_remlast.c sq_remfirst.c sq_remafter.c sq_count.c
24-
CSRCS += dq_addafter.c dq_remlast.c dq_remfirst.c dq_remafter.c dq_count.c
23+
CSRCS += queue.c
2524

2625
# Add the queue directory to the build
2726

libs/libc/queue/dq_addafter.c

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)