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 doesn't copy ._engine #28584

Closed
topper-123 opened this issue Sep 23, 2019 · 3 comments · Fixed by #32568
Closed

PERF: Index._shallow_copy doesn't copy ._engine #28584

topper-123 opened this issue Sep 23, 2019 · 3 comments · Fixed by #32568
Labels
Index Related to the Index class or subclasses Performance Memory or execution speed performance
Milestone

Comments

@topper-123
Copy link
Contributor

topper-123 commented Sep 23, 2019

>>> idx = pd.Index(np.arange(100_000))
>>> %timeit idx.get_loc(99_999)
774 ns ± 26 ns per loop
>>> %timeit idx._shallow_copy().get_loc(99_999)
3.57 ms ± 56.8 µs per loop

The same performance issue can be seen on other index types, e.g. CategoricalIndex and MultiIndex.

Problem description

The reason for the above diferences is that _shallow_copy does not copy over the ._engine attribute to the new index and the _engine is expensive to recreate.

Indexes are immutable, and likewise - to my understanding - are the ._engine attribute of indexes. The ._engine is quite expensive to create and if it has been created on the original index, I think it should be possible to reuse it on the new index, saving ther time needed to create a new and identical ._engine.

_shallow_copy is used in a few places internally in pandas, so there seems to be potential for some speedups for several pandas merhods by copying the _engine over to newly-copied indexes.

Possibly I'm missing some finer details here, e.g. don't know what the ._engine.clear_mappings is for and it seems from its name to be destructive, but overall it seems to be possible to make a change to copy this over.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 79663fb
python : 3.7.3.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None

pandas : 0.25.0.dev0+1363.g79663fb66
numpy : 1.16.4
pytz : 2019.1
dateutil : 2.8.0
pip : 19.1.1
setuptools : 41.0.1
Cython : 0.29.13
pytest : 5.0.1
hypothesis : 4.28.2
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.6.1
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : 2.6.9
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None

@TomAugspurger
Copy link
Contributor

I suspect we might want to copy over other cached attributes in Index._cache (is_unique, inferred_type, etc) if this proves safe to do.

@jbrockmendel
Copy link
Member

shallow_copy generally has new values passed to it. My intuition is that would make it impossible to share an engine. What am i missing?

@topper-123
Copy link
Contributor Author

topper-123 commented Sep 24, 2019

shallow_copy generally has new values passed to it. My intuition is that would make it impossible to share an engine. What am i missing?

I was thinking implicitly that reusing _engine would only be done when parameter values is None and the old values will be reused. If the values are reused, the _engine could be reused, if the engine is immutable, wouldn't you agree?

@topper-123 topper-123 added Index Related to the Index class or subclasses Performance Memory or execution speed performance labels Sep 24, 2019
@jreback jreback added this to the 1.1 milestone Mar 11, 2020
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 a pull request may close this issue.

4 participants