Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡ [#4744] Optimize formio data access
Calling out to glom too often hurts performance, and before this patch we did so to check for key membership AND access the value for a given key, with no caching in between. This also adds that if a key doesn't exist, we'd end up in exception handling, which is notably slow in Python. This patch applies an optimization for the simple cases in a couple of ways: * we store which (nested) keys exist. This happens by setting the key in an internal set whenever a write operation to that key happens. If the FormioData is initialized with a data structure, the UserDict internals cause the __setitem__ method to be called, which ensures the keys is added to the set. * if the key doesn't contain the '.' character, we know that it must be a top-level/root key, so we can shortcut the membership test by checking for membership in 'self._keys' rather than hitting the glom code path * in __getitem__, we can also treat simple keys as a simple case and directly access self.data, rather than going through glom for this Note that this patch doesn't handle deleting keys - __delitem__ is not implement, so the key removal doesn't either. I also suspect there may be some weird cases with 'data.update(...)' variants where nested keys are changed and stale data may be left in the _keys membership, so that's why I'm erring on the side of caution and only consider root-level keys for the shortcut code path. This patch can definitely be improved upon, but it must also easily be backported and a larger refactor can be done in a separate PR. Backport-of: #4768
- Loading branch information