Skip to content

MAINT: Ensure _argmaxima1d doesn't keep unneeded memory.#8499

Merged
larsoner merged 2 commits intoscipy:masterfrom
jaimefrio:maxima_resize
Feb 28, 2018
Merged

MAINT: Ensure _argmaxima1d doesn't keep unneeded memory.#8499
larsoner merged 2 commits intoscipy:masterfrom
jaimefrio:maxima_resize

Conversation

@jaimefrio
Copy link
Copy Markdown
Member

Call .resize on the return array, rather than taking a view, to prevent memory being unnecessarily held by the array.

Copy link
Copy Markdown
Member

@larsoner larsoner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise LGTM.

@lagru are you happy with these changes, too?

# Preallocate, there can't be more maxima than half the size of `x`
cdef np.ndarray[np.int64_t, ndim=1] maxima
maxima = np.empty(x.shape[0] // 2, dtype=np.int64)
maxima = np.empty(x.shape[0] // 2, dtype=np.intp)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why prefer intp (platform-specific?) over int64 here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the numpy equivalent of Py_ssize_t, and is the standard type of any index returning function in numpy: will not waste memory on 32 bit systems, and will work without overflow risk when 128 bit systems take over!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, shouldn't the declaration in the line above have the same dtype np.intp_t? Maybe the build failures are related to that?

ValueError: Buffer dtype mismatch, expected 'int64_t' but got 'int'

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes indeed, have submitted a new commit fixing that.

Copy link
Copy Markdown
Contributor

@lagru lagru left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, looks like a clear improvement to me.

@larsoner
Copy link
Copy Markdown
Member

I pushed a PEP8 fix to make Travis happy, will merge once I see the CIs come back green unless someone else beats me to it.

@jaimefrio
Copy link
Copy Markdown
Member Author

That last commit seems to make all the CIs happy.

@larsoner larsoner merged commit 8aea03b into scipy:master Feb 28, 2018
@larsoner
Copy link
Copy Markdown
Member

Thanks @jaimefrio

@jaimefrio jaimefrio deleted the maxima_resize branch February 28, 2018 21:34
@jaimefrio
Copy link
Copy Markdown
Member Author

My pleasure!

adbugger pushed a commit to adbugger/scipy that referenced this pull request Mar 11, 2018
* MAINT: Ensure _argmaxima1d doesn't keep unneeded memory.

* Fix maxima index array type mismatch.
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.

3 participants