tozz

tozz

I’m currently seeing some issued with our admin interface, where people on Intel CPU’s being shown the “the tab has crashed” kind of dialog when using a LiveView form. The background is that we use the Ecto sort and delete params, interactions need to be processed, otherwise I’d just change it to not have any interactive features.

The issue is not on the receiving end, that’s significantly much faster, but when LiveView does its processing before dispatching to the server. This screenshot is on a M3 Pro.

I don’t know why dispatchClickAway is wrapping all this, since there’s no such event on the page :slight_smile:
I tried digging deeper but as you know with JS, it’s usually a very long list of anonymous functions with minified names.

Looking at Node count it varies between 80-800K. Yes that’s 10x, for some reason LiveView or Chrome, unclear which, don’t clean up the tree after navigating/getting updates. It seems to be reliant on a GC to run.

The form is quite straight forward.

Schema (120 entries), 
|---- SubSchema (can vary, but between 0 and 10 items)
|---- SecondSubSchema (0-3 items)

So at most one level deep, but you can add and remove them dynamically. Processing times on the server side are fast enough that it doesn’t matter, less than 20ms, even in dev-mode.

Curious as to if people have some good ideas on how to debug this.

Showing Posts 1 to 10

chrismccord

chrismccord

Creator of Phoenix

We’d need to know a lot more. 80k DOM nodes is extreme, 800k is likely to be a showstopper in general, but I haven’t measured. facebook.com timeline for example is on the order of 4-5k DOM nodes. What is your DOM node count on dead render/first mount?

tozz

tozz OP

I dug into this some more, my numbers are from the Chrome Performance Monitor, and it seems that is doing some weird counting, or it keeps some internal state that isn’t represented in the actual tree.
Doing a simple document.querySelectorAll('*'); returns 19k nodes. Looking at just input it’s 4600 (actually exact) and of those it’s 2450 that are hidden (inputs_for generated with the exception of the needed _sort / _delete ones for Ecto)

Looking at execution time pushInput is the one function taking up time, below is a screenshot of dynamically adding three new “SubSchemas” to the form on a M1.

Happy to supply any other data you might need, and thank you for all the hard work in the ecosystem, using Phoenix and LiveView has been a bliss in general :slight_smile:

tozz

tozz OP

@chrismccord I found the “problem” (at least one that is really really tangible).

In serializeForm there’s a section that really destroys performance.

// view.js
let elements = Array.from(form.elements);
for (let [key, val] of formData.entries()) {
...
  let inputs = elements.filter(input => input.name === key)
  let isUnused = !inputs.some(input => (DOM.private(input, PHX_HAS_FOCUSED) || DOM.private(input, PHX_HAS_SUBMITTED)))
  let hidden = inputs.every(input => input.type === "hidden")
...
}

That first filter is very costly, in my form that’s a total of 22036236 iterations (I added a counter :smiley:). And I’d argue this isn’t even a big form, given the context of building an admin interface.

abuislam

abuislam

I’ve run into similar issues with JavaScript timeouts before—optimizing event handling and reducing unnecessary re-renders helped in my case. Are you using debounce or throttle for frequent updates? Hope you find a fix soon!

tcoopman

tcoopman

How is that 22 milion iterations?
The square root of that is something in the order of 4900, so you have form.elements and formData.entries in the order of 4-5K?

That sounds like a big form in my book?

But if this is the bottleneck for you, you might try to optimize the code yourself and see if you can improve the performance and submit a PR?

tozz

tozz OP

As mentioned in the original post, yes it’s 4600 inputs, more than half of them are inputs_for generated and _sort/_delete hidden fields. The form itself is not very large, it’s for building a Questionnaire, each Questionnaire have questions, questions have answer options. Then again, “large” is relative and I might just have a different background. It could very well be that I’m not supposed to use LiveView for this, but it would be truly marvelous if I could.

But if this is the bottleneck for you, you might try to optimize the code yourself and see if you can improve the performance and submit a PR?

Exactly what I’ve been doing, but there’s a lot of moving parts here.
Storing the lookup on input.name saves 4m iterations, but it’s still way too high, but a free optimization (unless it breaks somewhere else of course).

tcoopman

tcoopman

I think if you want really improve the performance, then I think you need to look at changing the algorithm. For example by breaking the O(n^2) algorithm by introducing an other datastructure. For example by parsing the form elements in a map with name as a key.

But you’d have to do some benchmark to see if that has a decent effect.

Schultzer

Schultzer

If you have a lot of text input, then it is highly advisable to disable default attributes like spellcheck spellcheck HTML global attribute - HTML | MDN and autocomplete <form> HTML form element - HTML | MDN

Schultzer

Schultzer

Phoenix does a lot in JS, especially with watching forms and helping with focus and etc. It does a bit to much for my taste and I think it could benefit from doing less on the front end and more on the backend and just leverage messages passing to reconcile states, I believe the cost in increase chatter will be worth it when messages are small.

tozz

tozz OP

It would be interesting with a server-first forms solution, it would of course come with its own tradeoffs (like not being able to recover, or at least making it much harder).

Removing this inner loop makes everything work blazing fast, and from what I can tell the only purpose for it is to control what field has been used to trigger showing of errors, which of course is a big UX thing.

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Stef-Verniers
Hello I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New
csmr
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley: Mutonex gamedesign was finish...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews