Skip to content

Commit

Permalink
perf(api): speed up simple column accesses by avoiding dereferencing (#…
Browse files Browse the repository at this point in the history
…9156)

Avoid the full dereference computation in the simple field access case,
e.g., `t["a"]` or `t[0]`.
  • Loading branch information
cpcloud authored May 8, 2024
1 parent 1892bfd commit c770fa1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ibis/expr/types/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,12 @@ def __getitem__(self, what):
if isinstance(what, slice):
limit, offset = util.slice_to_limit_offset(what, self.count())
return self.limit(limit, offset=offset)
# skip the self.bind call for single column access with strings or ints
# because dereferencing has significant overhead
elif isinstance(what, str):
return ops.Field(self.op(), what).to_expr()
elif isinstance(what, int):
return ops.Field(self.op(), self.columns[what]).to_expr()

args = [
self.columns[arg] if isinstance(arg, int) else arg
Expand Down
15 changes: 15 additions & 0 deletions ibis/tests/benchmarks/test_benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import itertools
import os
import string
from operator import attrgetter, itemgetter

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -832,3 +833,17 @@ def test_big_expression_compile(benchmark):
t2 = clean_names(t)

assert benchmark(ibis.to_sql, t2, dialect="duckdb")


@pytest.fixture(scope="module")
def many_cols():
return ibis.table({f"x{i:d}": "int" for i in range(10000)}, name="t")


@pytest.mark.parametrize(
"getter",
[itemgetter("x0"), itemgetter(0), attrgetter("x0")],
ids=["str", "int", "attr"],
)
def test_column_access(benchmark, many_cols, getter):
benchmark(getter, many_cols)

0 comments on commit c770fa1

Please sign in to comment.