Skip to content

Commit

Permalink
gh-126195: Use pthread_jit_write_protect_np on macOS
Browse files Browse the repository at this point in the history
Replace mprotect with pthread_jit_write_protect_np
on MacOS Apple Silicon.
Improve JIT performance by ~1.4% on this platform.
  • Loading branch information
diegorusso committed Nov 8, 2024
1 parent b19d12f commit 71cef5e
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion Python/jit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 71cef5e

Please sign in to comment.