We are checking inputs again and again. Caching the result is difficult because inputs can be checked with a different set of flags. Using a cache key that is computed from the input and the set of flag should be enough.