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

CArray.make with initial value is extremely slow #597

Open
Lupus opened this issue May 23, 2019 · 1 comment
Open

CArray.make with initial value is extremely slow #597

Lupus opened this issue May 23, 2019 · 1 comment

Comments

@Lupus
Copy link

Lupus commented May 23, 2019

Looks like initialization of each byte goes through C stub call. Initialization of 40MB buffer took about a second on a server with modern intel CPU. Can this be achieved by calling memset? At least zero-filling probably needs some optimized specialization...

@yallop
Copy link
Owner

yallop commented May 23, 2019

Indeed, this is a naive implementation, and it should be possible to do considerably better. Several possible improvements, some more work than others:

  • Eliminate allocations from the loop (which currently creates a new fat pointer for each element in unsafe_set)
  • Eliminate dispatch/branching from the loop (which currently inspects the type representation once per element in Ctypes_memory.stubs and then again in ctypes_write)
  • Double-check that the ctypes_write function doesn't allocate, and mark it noalloc if so
  • Eliminate the C-call-per-element by moving the loop into C
  • (Perhaps) add specialized implementations for commonly-used types. For char we could use memset

At least zero-filling probably needs some optimized specialization...

That would be useful. As it happens, ctypes uses calloc to allocate C arrays, so they're already guaranteed to be zero-filled when no initializer is supplied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants