PERF: Performance Regression GroupBy apply
- [x] I have checked that this issue has not already been reported.

- [x] I have confirmed this bug exists on the latest version of pandas.

- [ ] (optional) I have confirmed this bug exists on the master branch of pandas.

---

#### Code Sample, a copy-pastable example

```python

import pandas as pd
import numpy as np

ROWS=[1000, 2000, 4000, 8000, 16000, 32000]

for rows in ROWS:
    print("rows", rows)
    df = pd.DataFrame({"a": np.random.randint(0, rows, size=rows).astype(str), "b": range(0, rows)})
    %timeit df.groupby("a").apply(lambda x: x.head(1))
    
for rows in ROWS:
    print("rows", rows)
    df = pd.DataFrame({"a": np.random.randint(0, rows, size=rows).astype(str), "b": range(0, rows)})
    %timeit df.groupby("a").apply(lambda x: x.max())
```

#### Problem description

As a result of comparing 1.2.2 with 0.25.3, 1.2.2 is much slower. In particular, the head processing time is proportional to the number of lines in 0.25.3, but in 1.2.2, when the number of lines doubles, the processing time triples.

```
pandas 1.2.2

In [1]: for rows in ROWS:
   ...:     print("rows", rows)
   ...:     df = pd.DataFrame({"a": np.random.randint(0, rows, size=rows).astype(str), "b": range(0, rows)})
   ...:     %timeit df.groupby("a").apply(lambda x: x.head(1))
   ...:
rows 1000
187 ms ± 1.14 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
rows 2000
421 ms ± 2.86 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
rows 4000
1.06 s ± 13 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
rows 8000
3.01 s ± 59.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
rows 16000
9.49 s ± 81.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
rows 32000
32.3 s ± 214 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [2]: for rows in ROWS:
   ...:     print("rows", rows)
   ...:     df = pd.DataFrame({"a": np.random.randint(0, rows, size=rows).astype(str), "b": range(0, rows)})
   ...:     %timeit df.groupby("a").apply(lambda x: x.max())
   ...:
rows 1000
314 ms ± 2.68 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
rows 2000
599 ms ± 2.48 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
rows 4000
1.23 s ± 17.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
rows 8000
2.49 s ± 18.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
rows 16000
4.94 s ± 22.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
rows 32000
9.81 s ± 50.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
```

```
pandas 0.25.3

In [9]: for rows in ROWS:
   ...:     print("rows", rows)
   ...:     df = pd.DataFrame({"a": np.random.randint(0, rows, size=rows).astype(str), "b": range(0, rows)})
   ...:     %timeit df.groupby("a").apply(lambda x: x.head(1))
   ...:
rows 1000
103 ms ± 142 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
rows 2000
212 ms ± 1.23 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
rows 4000
414 ms ± 1.88 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
rows 8000
837 ms ± 2.61 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
rows 16000
1.69 s ± 2.62 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
rows 32000
3.41 s ± 36.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [10]: for rows in ROWS:
    ...:     print("rows", rows)
    ...:     df = pd.DataFrame({"a": np.random.randint(0, rows, size=rows).astype(str), "b": range(0, rows)})
    ...:     %timeit df.groupby("a").apply(lambda x: x.max())
    ...:
rows 1000
118 ms ± 827 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
rows 2000
235 ms ± 652 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
rows 4000
473 ms ± 1.19 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
rows 8000
945 ms ± 9.25 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
rows 16000
1.87 s ± 10.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
rows 32000
3.74 s ± 19.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
```

#### Output of ``pd.show_versions()``

<details>

```
INSTALLED VERSIONS
------------------
commit           : 7d32926db8f7541c356066dcadabf854487738de
python           : 3.8.8.final.0
python-bits      : 64
OS               : Linux
OS-release       : 5.10.16-arch1-1
Version          : #1 SMP PREEMPT Sat, 13 Feb 2021 20:50:18 +0000
machine          : x86_64
processor        :
byteorder        : little
LC_ALL           : None
LANG             : C.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.2.2
numpy            : 1.20.1
pytz             : 2021.1
dateutil         : 2.8.1
pip              : 21.0.1
setuptools       : 53.0.0
Cython           : None
pytest           : None
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : None
IPython          : 7.20.0
pandas_datareader: None
bs4              : None
bottleneck       : None
fsspec           : None
fastparquet      : None
gcsfs            : None
matplotlib       : None
numexpr          : None
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pyxlsb           : None
s3fs             : None
scipy            : None
sqlalchemy       : None
tables           : None
tabulate         : None
xarray           : None
xlrd             : None
xlwt             : None
numba            : None
```

</details>