Improve performance on permission lookups by applying caching and optimizing the code