Skip to content

Commit

Permalink
⚡ Optimize formio data access
Browse files Browse the repository at this point in the history
In FormioData, glom is used to perform a deep set/get on
the data, which is required for keys with a '.' as they
indicate a nested data structure.

Unfortunately, glom is slow, as already previously shown
by the patch for GH-4744. For forms with lots of form
variables (300+), creating a FormioData instance while
using `glom.assign` in __setitem__ is an expensive task.
Especially for forms which also contain lots of logic rules (50+),
this can hurt the performance of a check-logic call, as multiple
FormioData instances are created when evaluating each
logic rule.

This patch is a bandaid fix where we bypass `glom.assign` when
setting an item in FormioData.__setitem__. If the key does
not contain a '.', which means it is a top-level key, we
set the value on self.data directly. Otherwise, we use
`glom.assign` for a deep assignment of the value.

It is labeled as a bandaid fix, because the underlying problem
of creating lots of FormioData instances is still present.
This is not addressed here as it must be possible to
backport this patch easily. Investigating and refactoring
the complete data structures will take more time.

Backport-Of: #5118

(cherry picked from commit 47bf93a)
  • Loading branch information
viktorvanwijk committed Feb 26, 2025
1 parent ddc7ebf commit 04af67a
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/openforms/formio/datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ def __getitem__(self, key: str):
raise KeyError(f"Key '{key}' is not present in the data") from exc

def __setitem__(self, key: str, value: JSONValue):
assign(self.data, key, value, missing=dict)
if "." not in key:
self.data[key] = value
else:
assign(self.data, key, value, missing=dict)
self._keys.add(key)

def __contains__(self, key: object) -> bool:
Expand Down

0 comments on commit 04af67a

Please sign in to comment.