Performance issue because of the profiler
I created a Datatable and noticed 4 database request were made. One request to get the data, one to get the pagination information and 2 two last were just the same as the first ones.
I searched where this comes from and noticed in the src\DataTable\DataTable.php class the resetPagination() method that defines the resultSet to null.
As the DataCollectorListener listens to the DataTableEvents::POST_FILTER, DataTableEvents::POST_PAGINATE and DataTableEvents::POST_SORT events. The collector then fetches the data to fill the information.
And just before that, we remove the resultSet with the resetPagination(). That causes the data to be fetched again later in the process.
Maybe the events should be triggered later in the process ? We could dispath them just before the DataTableEvents::POST_INITIALIZE for example. But I'm not sure if those event make sense if they're dispatched at the same time than the main DataTable one.
Just to test it, but this seems to solve the problem (while removing the dispatched events in paginate, filter, sort and personalize)
public function initialize(): void
{
if ($this->initialized) {
return;
}
$this->dispatch(DataTableEvents::PRE_INITIALIZE, new DataTableEvent($this));
$paginationData = $this->getInitialPaginationData();
if (null === $this->paginationData && $paginationData) {
$this->paginate($paginationData, false);
}
$sortingData = $this->getInitialSortingData();
if (null === $this->sortingData && $sortingData) {
$this->sort($sortingData, false);
}
$filtrationData = $this->getInitialFiltrationData();
if (null === $this->filtrationData && $filtrationData) {
$this->filter($filtrationData, false);
}
$personalizationData = $this->getInitialPersonalizationData();
if (null === $this->personalizationData && $personalizationData) {
$this->personalize($personalizationData, false);
}
$this->initialized = true;
if (null !== $filtrationData) {
$this->dispatch(DataTableEvents::POST_FILTER, new DataTableFiltrationEvent($this, $filtrationData));
}
if (null !== $paginationData) {
$this->dispatch(DataTableEvents::POST_PAGINATE, new DataTablePaginationEvent($this, $paginationData));
}
if (null !== $sortingData) {
$this->dispatch(DataTableEvents::POST_SORT, new DataTableSortingEvent($this, $sortingData));
}
if (null !== $personalizationData) {
$this->dispatch(DataTableEvents::POST_PERSONALIZE, new DataTablePersonalizationEvent($this, $personalizationData));
}
$this->dispatch(DataTableEvents::POST_INITIALIZE, new DataTableEvent($this));
}
Hi @Kreyu,
After some digging, I found out that the problem comes from the method resetPagination(). It's called in filter, paginate and sort.
Removing the method call and moving it in initialize() fixes the problem but I'm not quite sure this is the correct way to address the problem.
Those 3 methods (filter, paginate and sort) are also called in the HttpFoundationRequestHandler but the modification doesn't seem to have any incidence.