ibis-project / ibis

the portable Python dataframe library
https://ibis-project.org
Apache License 2.0
4.43k stars 544 forks source link

perf(api): speed up simple column accesses by avoiding dereferencing #9156

Closed cpcloud closed 1 month ago

cpcloud commented 1 month ago

Avoid the full dereference computation in the simple field access case, e.g., t["a"] or t[0].

cpcloud commented 1 month ago

Benchmark comparison:

-------------------------------------------------------------------------------- benchmark 'test_column_access[attr]': 2 tests ---------------------------------------------------------------------------------
Name (time in us)                               Min                Max               Mean            StdDev             Median               IQR            Outliers  OPS (Kops/s)            Rounds  Iterations
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_column_access[attr] (0001_022d094)     12.2840 (1.0)      28.0130 (1.04)     12.9317 (1.0)      0.6494 (1.03)     12.8340 (1.0)      0.2400 (1.70)      378;500       77.3295 (1.0)       12211           1
test_column_access[attr] (NOW)              12.7140 (1.04)     27.0010 (1.0)      13.1502 (1.02)     0.6293 (1.0)      13.0550 (1.02)     0.1410 (1.0)       463;863       76.0444 (0.98)      18164           1
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------- benchmark 'test_column_access[int]': 2 tests -----------------------------------------------------------------------------------
Name (time in ms)                               Min                 Max                Mean             StdDev              Median                IQR            Outliers          OPS            Rounds  Iterations
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_column_access[int] (0001_022d094)     157.3464 (>1000.0)  213.8952 (>1000.0)  180.3918 (>1000.0)  30.4235 (>1000.0)  158.6153 (>1000.0)  55.3601 (>1000.0)       2;0       5.5435 (0.00)          5           1
test_column_access[int] (NOW)                0.0321 (1.0)        0.0539 (1.0)        0.0334 (1.0)       0.0012 (1.0)        0.0330 (1.0)       0.0009 (1.0)       614;570  29,976.2958 (1.0)       10307           1
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------- benchmark 'test_column_access[str]': 2 tests -----------------------------------------------------------------------------------
Name (time in ms)                               Min                 Max                Mean             StdDev              Median                IQR            Outliers          OPS            Rounds  Iterations
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_column_access[str] (0001_022d094)     157.9610 (>1000.0)  212.6186 (>1000.0)  188.8913 (>1000.0)  28.5787 (>1000.0)  210.9883 (>1000.0)  53.4910 (>1000.0)       3;0       5.2941 (0.00)          7           1
test_column_access[str] (NOW)                0.0130 (1.0)        0.0240 (1.0)        0.0135 (1.0)       0.0006 (1.0)        0.0134 (1.0)       0.0002 (1.0)      900;1441  73,982.5499 (1.0)       33777           1
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Legend:
  Outliers: 1 Standard Deviation from Mean; 1.5 IQR (InterQuartile Range) from 1st Quartile and 3rd Quartile.
  OPS: Operations Per Second, computed as 1 / Mean
====================================================================================== 3 passed in 3.88s =======================================================================================

image

gforsyth commented 1 month ago

So... a 4900x speedup? We could use this in a disingenuous blog post about performance

kszucs commented 1 month ago

We could also speed up the dereferencing by caching the derefmap for table expressions. We can also maintain a smaller mapping by skipping items where key == value.