PERF: Performance regression in Groupby.apply with group_keys=True
- [ ] closes #xxxx (Replace xxxx with the GitHub issue number)
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [x] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [x] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.

I came accross this bottleneck through the changed default of ``group_keys``. Technically this is not a regression, but it's visible to all users that don't set ``group_keys`` explicitly. This change provides around a 20-30% speedup in groupby apply when you have many groups
Nice find. Thanks @phofl  > Technically this is not a regression, but it's visible to all users that don't set `group_keys` explicitly.

Just want to make sure I understand this - `group_keys=False` was much faster than `group_keys=True`, and this PR improves the `group_keys=True` case. Is that right? Yep