Repository: neovim/neovim
File: src/nvim/os/env.c (C)

Code before:

/// Get the process ID of the Nvim process.
///
/// @return the process ID.
int64_t os_get_pid(void)
{
#ifdef MSWIN
  return (int64_t)GetCurrentProcessId();
#else
  return (int64_t)getpid();
#endif
}

/// Gets the hostname of the current machine.
///
/// @param hostname   Buffer to store the hostname.
/// @param size       Size of `hostname`.
void os_get_hostname(char *hostname, size_t size)
{
#ifdef HAVE_SYS_UTSNAME_H
  struct utsname vutsname;

  if (uname(&vutsname) < 0) {
    *hostname = NUL;
  } else {
    xstrlcpy(hostname, vutsname.nodename, size);
  }
#elif defined(MSWIN)
  wchar_t host_utf16[MAX_COMPUTERNAME_LENGTH + 1];
  DWORD host_wsize = sizeof(host_utf16) / sizeof(host_utf16[0]);
  if (GetComputerNameW(host_utf16, &host_wsize) == 0) {
    *hostname = NUL;
    DWORD err = GetLastError();
    semsg("GetComputerNameW failed: %d", err);
    return;
  }
  host_utf16[host_wsize] = NUL;

  char *host_utf8;
  int conversion_result = utf16_to_utf8(host_utf16, -1, &host_utf8);
  if (conversion_result != 0) {
    semsg("utf16_to_utf8 failed: %d", conversion_result);
    return;
  }
  xstrlcpy(hostname, host_utf8, size);
  xfree(host_utf8);
#else
  emsg("os_get_hostname failed: missing uname()");
  *hostname = NUL;
#endif
}

Review comment (performance):
As a follow up, we may want to consider switching to the newer Quality of Service APIs. See https://developer.apple.com/documentation/apple-silicon/tuning-your-code-s-performance-for-apple-silicon:

>If you manually configure your thread’s priority using pthread_setschedparam, setpriority, or thread_set_policy, transition to APIs that set QoS classes instead.

We can also make this a bit more sophisticated by using the "User interactive" QoS class when Neovim is focused, and transition to the "Utility" or "Background" QoS class when unfocused (see https://developer.apple.com/library/archive/documentation/Performance/Conceptual/power_efficiency_guidelines_osx/PrioritizeWorkAtTheTaskLevel.html).

Code after:
  assert(len > 0);
  return xmemdupz(str, (size_t)len);
#endif
}

/// Get the process ID of the Nvim process.
///
/// @return the process ID.
int64_t os_get_pid(void)
{
#ifdef MSWIN
  return (int64_t)GetCurrentProcessId();
#else
  return (int64_t)getpid();
#endif
}

/// Signals to the OS that Nvim is an application for "interactive work"
/// which should be prioritized similar to a GUI app.
void os_hint_priority(void)
{
#ifdef __APPLE__
  // By default, processes have the TASK_UNSPECIFIED "role", which means all of its threads are
  // clamped to Default QoS. Setting the role to TASK_DEFAULT_APPLICATION removes this clamp.
  integer_t policy = TASK_DEFAULT_APPLICATION;
  task_policy_set(mach_task_self(), TASK_CATEGORY_POLICY, &policy, 1);
#endif
}

/// Gets the hostname of the current machine.
///
/// @param hostname   Buffer to store the hostname.
/// @param size       Size of `hostname`.
void os_get_hostname(char *hostname, size_t size)
{
#ifdef HAVE_SYS_UTSNAME_H
  struct utsname vutsname;

  if (uname(&vutsname) < 0) {
    *hostname = NUL;
  } else {
    xstrlcpy(hostname, vutsname.nodename, size);
  }
#elif defined(MSWIN)
  wchar_t host_utf16[MAX_COMPUTERNAME_LENGTH + 1];
  DWORD host_wsize = sizeof(host_utf16) / sizeof(host_utf16[0]);
  if (GetComputerNameW(host_utf16, &host_wsize) == 0) {
    *hostname = NUL;
    DWORD err = GetLastError();
    semsg("GetComputerNameW failed: %d", err);
    return;