Skip to content

Commit 71cef5e

Browse files
committed
gh-126195: Use pthread_jit_write_protect_np on macOS
Replace mprotect with pthread_jit_write_protect_np on MacOS Apple Silicon. Improve JIT performance by ~1.4% on this platform.
1 parent b19d12f commit 71cef5e

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

Python/jit.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,16 @@ jit_alloc(size_t size)
5656
int flags = MEM_COMMIT | MEM_RESERVE;
5757
unsigned char *memory = VirtualAlloc(NULL, size, flags, PAGE_READWRITE);
5858
int failed = memory == NULL;
59+
#elif defined(__APPLE__) && defined(__aarch64__)
60+
int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_JIT;
61+
int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
62+
unsigned char *memory = mmap(NULL, size, prot, flags, -1, 0);
63+
int failed = memory == MAP_FAILED;
64+
pthread_jit_write_protect_np(0);
5965
#else
6066
int flags = MAP_ANONYMOUS | MAP_PRIVATE;
61-
unsigned char *memory = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0);
67+
int prot = PROT_READ | PROT_WRITE;
68+
unsigned char *memory = mmap(NULL, size, prot, flags, -1, 0);
6269
int failed = memory == MAP_FAILED;
6370
#endif
6471
if (failed) {
@@ -101,6 +108,10 @@ mark_executable(unsigned char *memory, size_t size)
101108
}
102109
int old;
103110
int failed = !VirtualProtect(memory, size, PAGE_EXECUTE_READ, &old);
111+
#elif defined(__APPLE__) && defined(__aarch64__)
112+
int failed = 0;
113+
__builtin___clear_cache((char *)memory, (char *)memory + size);
114+
pthread_jit_write_protect_np(1);
104115
#else
105116
__builtin___clear_cache((char *)memory, (char *)memory + size);
106117
int failed = mprotect(memory, size, PROT_EXEC | PROT_READ);

0 commit comments

Comments
 (0)