I can invert the logic there to handle the cases which require a protection change first (or alternatively assert them together with the comment).
Am still a bit hesitant to not querying first, at least the performance measurements seem to indicate a noticeable difference between WriteProcessMemory and NtWriteVirtualMemory on Windows in the “normal” case (this could be as well due to Windows unconditionally flushing the instruction cache though).
It would also remove the optimization of knowing the protection beforehand to skip the flush in non-executable regions.