lots of performance optimizations to entity manager