Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

work around pointless copy in isZeroMemory #233

Merged
merged 1 commit into from
Sep 26, 2024
Merged

work around pointless copy in isZeroMemory #233

merged 1 commit into from
Sep 26, 2024

Conversation

tersec
Copy link
Contributor

@tersec tersec commented Sep 25, 2024

For example, given

import stew/objects
discard isZeroMemory(default(array[1024, byte]))

The previous code under Nim 2.0.8 with refc compiles to:

N_LIB_PRIVATE N_NIMCALL(NIM_BOOL,
                        _ZN7objects12isZeroMemoryE5arrayI10range010235uInt8E)(
    tyArray__P46RzO9bG1lpfJxQ2itJfAA x_p0) {
  NIM_BOOL result;
  {
    result = (NIM_BOOL)0;
    {
      NU8 b;
      tyArray__P46RzO9bG1lpfJxQ2itJfAA colontmp_;  /* stack copy of the array */
      NI i;
      b = (NU8)0;
      nimZeroMem((void *)colontmp_, sizeof(tyArray__P46RzO9bG1lpfJxQ2itJfAA));
      nimCopyMem((void *)colontmp_, (NIM_CONST void *)((NU8 *)(x_p0)),
                 sizeof(tyArray__P46RzO9bG1lpfJxQ2itJfAA));
      i = ((NI)0);
      {
        while (1) {
          b = colontmp_[(i)-0];
          {
            if (!!((b == ((NU8)0))))
              goto LA6_;
            result = NIM_FALSE;
            goto BeforeRet_;
          }
        LA6_:;
          {
            if (!(((NI)1023) <= ((NI)(i))))
              goto LA10_;
            goto LA2;
          }
        LA10_:;
          i += ((NI)1);
        }
      }
    LA2:;
    }
    result = NIM_TRUE;
  }
BeforeRet_:;
  popFrame();
  return result;
}

which this PR changes to codegen to

N_LIB_PRIVATE N_NIMCALL(NIM_BOOL,
                        _ZN7objects12isZeroMemoryE5arrayI10range010235uInt8E)(
    tyArray__P46RzO9bG1lpfJxQ2itJfAA x_p0) {
  NIM_BOOL result;
  NU8 *bufPtr;
  {
    result = (NIM_BOOL)0;
    bufPtr = ((NU8 *)(x_p0));
    {
      NU8 b;
      NI i;
      b = (NU8)0;
      i = ((NI)0);
      {
        while (1) {
          b = bufPtr[(i)-0];
          {
            if (!!((b == ((NU8)0))))
              goto LA6_;
            result = NIM_FALSE;
            goto BeforeRet_;
          }
        LA6_:;
          {
            if (!(((NI)1023) <= ((NI)(i))))
              goto LA10_;
            goto LA2;
          }
        LA10_:;
          i += ((NI)1);
        }
      }
    LA2:;
    }
    result = NIM_TRUE;
  }
BeforeRet_:;
  popFrame();
  return result;
}

which as intended does not trigger a copy of isZeroMemory's parameter.

initHashSet unconditionally exists in std/sets since at the latest 1.6.0: https://github.com/nim-lang/Nim/blob/v1.6.0/lib/pure/collections/sets.nim#L112-L129

default unconditionally also unconditionally exists since at the latest 1.6.0: https://github.com/nim-lang/Nim/blob/v1.6.0/lib/system.nim#L982-L998

@arnetheduck arnetheduck merged commit 1c9190a into master Sep 26, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants