Skip to content

Commit db79871

Browse files
authored
optimize: Cache Clib4Resource pointer to eliminate repeated OpenResource() calls in malloc (#439)
- Add __clib4_resource field to struct _clib4 for caching the global resource pointer - Initialize cache once in stdlib_memory_init instead of calling OpenResource() on every allocation - Update __get_wmem_allocator, __memory_lock, and __memory_unlock to use cached pointer - Eliminates 3 OpenResource() calls per malloc/free (hot path optimization) - Maintains global allocator semantics and thread-safety through shared resource semaphore
1 parent 4c1322c commit db79871

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

library/dos.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,14 @@ struct _clib4 {
603603
*/
604604
struct iob *__sf[3]; /* per-process stdin/stdout/stderr iob pointers */
605605
struct _glue *__sglue; /* per-process root glue node for FILE slots */
606+
607+
/*
608+
* Cached pointer to the global Clib4Resource.
609+
* Initialized once in stdlib_memory_init to avoid repeated OpenResource() calls
610+
* in malloc/free hot path. The resource itself is shared across all processes,
611+
* but each process caches its own pointer for fast access.
612+
*/
613+
struct Clib4Resource *__clib4_resource;
606614
};
607615

608616
#ifndef __getClib4

library/stdlib/malloc.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818

1919
wmem_allocator_t *
2020
__get_wmem_allocator(struct _clib4 *__clib4) {
21-
(void) __clib4;
22-
23-
struct Clib4Resource *res = (APTR) OpenResource(RESOURCE_NAME);
21+
/* Use cached resource pointer - initialized in stdlib_memory_init */
22+
struct Clib4Resource *res = __clib4->__clib4_resource;
2423
if (res == NULL)
2524
return NULL;
2625

@@ -71,7 +70,8 @@ __malloc_aligned_r(struct _clib4 *__clib4, size_t size, int32_t alignment) {
7170
}
7271

7372
void __memory_lock(struct _clib4 *__clib4) {
74-
struct Clib4Resource *res = (APTR) OpenResource(RESOURCE_NAME);
73+
/* Use cached resource pointer - initialized in stdlib_memory_init */
74+
struct Clib4Resource *res = __clib4->__clib4_resource;
7575

7676
if(__clib4->memory_mutex)
7777
MutexObtain(__clib4->memory_mutex);
@@ -81,7 +81,8 @@ void __memory_lock(struct _clib4 *__clib4) {
8181
}
8282

8383
void __memory_unlock(struct _clib4 *__clib4) {
84-
struct Clib4Resource *res = (APTR) OpenResource(RESOURCE_NAME);
84+
/* Use cached resource pointer - initialized in stdlib_memory_init */
85+
struct Clib4Resource *res = __clib4->__clib4_resource;
8586

8687
if (res != NULL)
8788
ReleaseSemaphore(&res->semaphore);
@@ -99,6 +100,9 @@ STDLIB_DESTRUCTOR(stdlib_memory_exit) {
99100
__clib4->memory_mutex = NULL;
100101
}
101102

103+
/* Clear cached resource pointer */
104+
__clib4->__clib4_resource = NULL;
105+
102106
LEAVE();
103107
}
104108

@@ -121,6 +125,9 @@ STDLIB_CONSTRUCTOR(stdlib_memory_init) {
121125
goto out;
122126
}
123127

128+
/* Cache the global resource pointer to avoid repeated OpenResource() calls */
129+
__clib4->__clib4_resource = res;
130+
124131
ObtainSemaphore(&res->semaphore);
125132
if (res->__wmem_allocator == NULL) {
126133
res->__wmem_allocator = wmem_allocator_new(__clib4->__wof_mem_allocator_type);

0 commit comments

Comments
 (0)