REF: cython cleanups and optimizations
State: closed

- [ ] closes #xxxx
- [ ] tests added / passed
- [ ] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [ ] whatsnew entry


Labels:
name: Performance
description: Memory or execution speed performance
name: Internals
description: Related to non-user accessible pandas implementation
name: Clean

Comments:
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/23382?src=pr&el=h1) Report
> Merging [#23382](https://codecov.io/gh/pandas-dev/pandas/pull/23382?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/4f717551b8e49ecd978bb4f5a985d6f6a96d86e9?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/23382/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/23382?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master   #23382   +/-   ##
=======================================
  Coverage   92.22%   92.22%           
=======================================
  Files         161      161           
  Lines       51187    51187           
=======================================
  Hits        47209    47209           
  Misses       3978     3978
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.61% <ø> (ø)` | :arrow_up: |
| #single | `42.26% <ø> (ø)` | :arrow_up: |


------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/23382?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/23382?src=pr&el=footer). Last update [4f71755...31a1319](https://codecov.io/gh/pandas-dev/pandas/pull/23382?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

Mostly changing ndarray to memoryviews where possible.  A few notation cleanups around pointer types.  Adds some extra type annotations, using py3-syntax where feasible.
@jbrockmendel : How is performance impacted, if at all, after these changes?
> How is performance impacted, if at all, after these changes?

Small but positive.  For the changes of ndarray[dtype] to dtype[:] the impact is small enough that I can't measure it, but I trust the cython folks when they say that the memoryview usage is more performant.

Adding `@cython.wraparound(False)` and `@cython.boundscheck(False)` is a clear improvement.  Easiest way to see this is in the output of `cython -a`.  A decent chunk of python-land calls are avoided.

Changing `ndarray[algos_t]` to `ndarray[algos_t, ndim=1]` is another one where I can't measure it, but it should be an improvement according to cython folks.

Changing `out.fill(1-flag_val)` to `out[:] = 1 - flag_val` (prompted by change to memoryview) replaces a python call with a C call.

Removal of `group_cummax_float64` etc doesn't make a difference.  in core.groupby there is a lookup for `getattr(libgroupby, "group_cummax")` that now succeeds, so it never gets around to doing the dtype-specific lookup.

Adding type annotations in _libs.lib for non-cdef functions I'm not sure if cython actually uses those, so that might just be for funsies.

Replacing `util.get_value_at(arr, i)` with `arr[i]` in _libs.lib should be a clear-but-tiny win, since all the former does is some not-necessary-in-these-cases validation before returning `arr[i]`.

So tiny-but-positive all around.
@jbrockmendel : Cool, just wanted to make sure we had the rationale documented 👍 
Reverted change of `foo.base` to `np.assarray(foo)`.  It risks incorrectly returning `np.array(None)` instead of raising.
updated to use `np.asarray(foo)` instead of `foo.base` and to explicitly reject `None` in all appropriate places.  I think we may also need to do a pass to add `const` to memoryview-accepting py-exposed functions.  Between these two, the added verbiage is making this look less appealing.
needs a rebase
Rebased, but pls hold off until I make another pass.
The worthwhile parts of this are in #23464.  Closing.