F
Filament4mo ago
Noxo

The huge form is too slow

I have a form that has 200+ fields. When saving I get the timeout error Maximum execution time of 30 seconds exceeded Any ideas on how to speed this up? The form is generated automatically (not static).
Tabs::make()
->hiddenOn('create')
->tabs(function ($record) {
ray('tabs');

$processors = once(fn () => $record->processors()->with('template')->get());

return $processors
->map(
fn ($processor) => Tab::make($processor->name)
->statePath('fields.' . $processor->template_id)
->schema(
$processor->template->fields
->map(fn ($field) => FieldBuilderService::build(FieldData::from($field)))
->toArray()
)
->columns(2)
)
->toArray();
}),
Tabs::make()
->hiddenOn('create')
->tabs(function ($record) {
ray('tabs');

$processors = once(fn () => $record->processors()->with('template')->get());

return $processors
->map(
fn ($processor) => Tab::make($processor->name)
->statePath('fields.' . $processor->template_id)
->schema(
$processor->template->fields
->map(fn ($field) => FieldBuilderService::build(FieldData::from($field)))
->toArray()
)
->columns(2)
)
->toArray();
}),
No description
16 Replies
Noxo
NoxoOP4mo ago
but saving the form without data happens pretty quickly
No description
No description
Dennis Koch
Dennis Koch4mo ago
I'd remove some stuff to see which parts take long to save Impossible to debug a 200 field form otherwise
Noxo
NoxoOP4mo ago
but even when I get a timeout, the form still doesn't save because I delete all the fields. so the problem can't be in the database queries. it looks like it's because of the fields validation. it looks like for each field the entire form is built from scratch (you can see it on the video).
No description
Dennis Koch
Dennis Koch4mo ago
Maybe some Select fields that load a lot of data
Noxo
NoxoOP4mo ago
there are no select fields. only text, checkbox and radio. I will try to delete parts to find out what the problem is
Dennis Koch
Dennis Koch4mo ago
it looks like for each field the entire form is built from scratch (you can see it on the video).
I don't think it's for every field, but it's definitely called multiple times. You shouldn't have logn operations in the form definition
Noxo
NoxoOP4mo ago
I found the problem. It's because I pass the function in which the tabs are built. When I removed the function, the saving speed became instant. But now the problem is that I need to somehow get the $record variable 😤
Noxo
NoxoOP4mo ago
it seems that the problem is not in the closure, but in FieldData::from (Spatie\LaravelData). when I remove it, everything works fast. but why then does it work quickly without closure which I showed in the video.. im completely confused 🫤
Noxo
NoxoOP4mo ago
No description
awcodes
awcodes4mo ago
What is the DTO doing under the hood?
Noxo
NoxoOP4mo ago
standard DTO, nothing custom 🤷‍♂️
No description
awcodes
awcodes4mo ago
Very weird. I can’t imagine why that would be the bottleneck.
Noxo
NoxoOP4mo ago
from method slows down code
No description
awcodes
awcodes4mo ago
Don’t know enough about the spatie package, sorry. I can’t imagine what it’s doing to the field. Are the fields in the map just an array or are they instances of a class.
Noxo
NoxoOP4mo ago
yes these are instances (eloquent). I couldn't figure out what the problem was. The solution is this: I created a custom page for the resource and added all the fields there. This way I was able to do it without using closure. now it works fast there.
tg
tg4mo ago
@Noxo Which version of spatie/laravel-data do you use? ::from usually triggers validation for all fields, which might slow things down considerably. Regular new instantiation should skip validation and be orders of magnitude faster as you observed. If you use v4, you may want to try out the Dto class: https://spatie.be/docs/laravel-data/v4/as-a-data-transfer-object/creating-a-data-object#content-dto-classes I guess you can skip validation when you are building the form from existing database-driven field definitions, as these have been validated at creation/update.
Want results from more Discord servers?
Add your server