From 3e090bf0a0e5833bc1e42e28d6a0f94c694573e3 Mon Sep 17 00:00:00 2001 From: Diego Russo Date: Tue, 23 Apr 2024 15:01:13 +0100 Subject: [PATCH] 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. --- Python/jit.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Python/jit.c b/Python/jit.c index 135daeb1b1da80..a111f800a62f4c 100644 --- a/Python/jit.c +++ b/Python/jit.c @@ -56,9 +56,16 @@ jit_alloc(size_t size) int flags = MEM_COMMIT | MEM_RESERVE; unsigned char *memory = VirtualAlloc(NULL, size, flags, PAGE_READWRITE); int failed = memory == NULL; +#elif defined(__APPLE__) && defined(__aarch64__) + int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_JIT; + int prot = PROT_READ | PROT_WRITE | PROT_EXEC; + unsigned char *memory = mmap(NULL, size, prot, flags, -1, 0); + int failed = memory == MAP_FAILED; + pthread_jit_write_protect_np(0); #else int flags = MAP_ANONYMOUS | MAP_PRIVATE; - unsigned char *memory = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0); + int prot = PROT_READ | PROT_WRITE; + unsigned char *memory = mmap(NULL, size, prot, flags, -1, 0); int failed = memory == MAP_FAILED; #endif if (failed) { @@ -101,6 +108,10 @@ mark_executable(unsigned char *memory, size_t size) } int old; int failed = !VirtualProtect(memory, size, PAGE_EXECUTE_READ, &old); +#elif defined(__APPLE__) && defined(__aarch64__) + int failed = 0; + __builtin___clear_cache((char *)memory, (char *)memory + size); + pthread_jit_write_protect_np(1); #else __builtin___clear_cache((char *)memory, (char *)memory + size); int failed = mprotect(memory, size, PROT_EXEC | PROT_READ);