Yes I agree, writing a specific functionality in the engine does allow for more optimal performance but the tradeoff is that it's more brittle and more complicated to write and maintain.
My personal approach has been to do the first principles and avoid writing use-case specific features in the engine as much as possible but provide more fundamental functionality that the game then combines to create the desired effect.
That being said I've still had to do some concessions and move stuff like simple constant velocity game object integration to the engine side simply because the perf difference between native code doing that vs Lua code are just too massive.
Some typical ways to divide this though is
Some org has "engine" team and "game" team. Engine team implements features in the engine itself and game team implements game specific features using the embedded scripting language.
Engine features are game agnostic and game features are specific to games and are scripted / built-on top of the engine features.
But sometimes things get muddled as general purpose engines are used in games that require specific features in the engine itself. Typically the decision is then made by a) what kind of feature and how does it fit in the architecture of the engine b) what are the performance requirements c) who is willing to do the work and maintain it
For instance, I find you can get massive mileage by declaring “(almost) everything is instanced”, then you batch it, forget it, move on. There are similar shortcuts to be made with physics and so on.
The close to ideal engine, in terms of approaches to problems, was LittleBigPlanet. That was a small team that were really good, but crucially they had the right way to frame the problems so as to constrain the emergent complexity of the result.