- Issue created by @lubwn
- π«π·France mably
Wondering what could be the impact on performance on huge dataset...
Could it be problematic or not?
- π«π·France mably
@lubwn ChatGPT suggest a better approach, on a performance point of view. Would it still work for your use case?
Better Approach: Call the Hook Once
Instead of calling the hook on each row, call it once before the loop, and then apply the returned logic within the loop.
π§ Step-by-step
Step 1: Let hook implementations return callables or config
In other modules:
function my_module_my_custom_hook_prepare() { return function ($row) { // Do something with $row // e.g. modify, collect stats, tag, etc. }; }
Step 2: Invoke the hook once and collect callables
$callbacks = \Drupal::moduleHandler()->invokeAll('my_custom_hook_prepare');
This will return an array of functions (closures or callables) from all modules implementing hook_my_custom_hook_prepare().
Step 3: Use those callables inside the loop
foreach ($view->result as $row) { foreach ($callbacks as $callback) { $callback($row); } }
β Why This Is Better
π Before
- For n rows and m modules, you have O(n Γ m) hook resolution calls.
- Even for rows where no logic is needed, you're still scanning all modules.
β‘ After
- Hook resolution (invokeAll()) happens only once.
- For n rows, you directly apply the relevant logic.
- Much easier to cache or optimize later.
- Merge request !50Issue #3370352 by mably, lubwn: Add ability to alter rows in xls export β (Open) created by mably
- π«π·France mably
This feature can now be tested in the new Excel Serialization Extras β sandbox module.
- π«π·France mably
Actually, the Drupal hook cache is pretty performant, so not sure the ChatGPT optimisation suggestion is really necessary.