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

PERF: Index._shallow_copy shares _cache with copies of self #36840

Merged
merged 6 commits into from
Oct 6, 2020

Conversation

topper-123
Copy link
Contributor

@topper-123 topper-123 commented Oct 3, 2020

#32640 copied the cache when copying indexes. Indexes are immutable, so some refactoring allows us to share the cache instead. This means potentially fewer expensive index ops.

>>> idx = pd.CategoricalIndex(np.arange(100_000))
>>> copied = idx.copy()
>>> copied._cache
{}
>>> idx.get_loc(99_999)
>>> copied._cache
{'_engine': <pandas._libs.index.Int32Engine at 0x1fd3a6934c8>}  # don't need to recreate _engine on copied

Performance example:

>>> idx = pd.CategoricalIndex(np.arange(100_000))
>>> %timeit idx._shallow_copy().get_loc(99_999)
4.09 ms ± 65 µs per loop  # master
7.71 µs ± 355 ns per loop  # this PR

@topper-123 topper-123 changed the title PERF: Index.equals when comparing to copies of self PERF: Index._shallow_copy shares _cache with copies of self Oct 4, 2020
@topper-123
Copy link
Contributor Author

topper-123 commented Oct 4, 2020

The issue @jreback raised means sharing ids is a bit more complicated than thought.

I've repurposed this PR to make indexes share caches, as that was planned in a follow-up, and the code for that is not that different. Sharing caches gives some performance improvements also (in different circumstances than sharing _ids, but still nice).

@topper-123
Copy link
Contributor Author

ping.

@jreback jreback added Index Related to the Index class or subclasses Performance Memory or execution speed performance labels Oct 6, 2020
@jreback jreback added this to the 1.2 milestone Oct 6, 2020
Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

lgtm

@jreback
Copy link
Contributor

jreback commented Oct 6, 2020

looks good @topper-123
@jbrockmendel if you can just give another set of eyes here and merge when ready.

@jbrockmendel jbrockmendel merged commit b58ee57 into pandas-dev:master Oct 6, 2020
@jbrockmendel
Copy link
Member

thanks @topper-123

jbrockmendel pushed a commit to jbrockmendel/pandas that referenced this pull request Oct 13, 2020
…ev#36840)

* PERF: Index.equals when comparing to copies of self

* refactor _shallow_copy, add GH number

* PERF: share _cache, don't share _id

* rename tests

* fix memory usage test

Co-authored-by: Jeff Reback <[email protected]>
kesmit13 pushed a commit to kesmit13/pandas that referenced this pull request Nov 2, 2020
…ev#36840)

* PERF: Index.equals when comparing to copies of self

* refactor _shallow_copy, add GH number

* PERF: share _cache, don't share _id

* rename tests

* fix memory usage test

Co-authored-by: Jeff Reback <[email protected]>
@topper-123 topper-123 deleted the index_id branch November 28, 2020 18:25
@simonjayhawkins simonjayhawkins mentioned this pull request May 27, 2022
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Index Related to the Index class or subclasses Performance Memory or execution speed performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants