In #3060 it was mentioned:
The last part might be covered by #3455, not sure though.
Consistently smooth interactions without lagging. No issues with selecting and dragging blocks.

develop_Do not alter or remove anything below. The following sections will be managed by moderators only._
Two optimizations are recommended for getting smoother snap line display:
setSnapLines by 10 ms.horizontalSnaps in horizontalEdgeSnaps and horizontalCenterSnaps in with-snap-target.js. And consequently use these separately for calculations when finding optimal snap lines in getBestSnapLines.Verify smooth drag without lagging:
Verify proper snap lines between element and page:
Verify proper snap lines between two elements:
Also having some mega lag issues. Created a video that shows the lags.
https://youtu.be/msGZkNMrU3Y
Starting looking into this (Implementation Brief).
I'm looking into this and when I disable Snapping then the lagging does not happen. So it still seems to me as if this is related to Snapping.
@barklund You mentioned that you had seen the lagging for a while already, already pre-snapping. Do you happen to remember if you saw this with dragging or somewhere else as well?
@barklund You mentioned that you had seen the lagging for a while already, already pre-snapping. Do you happen to remember if you saw this with dragging or somewhere else as well?
I don't remember the circumstances, sorry.
Looks like setSnapLines is the thing that makes it lag.
Q: Perhaps we could detect if the cursor is moving slowly or has stopped, and only then do the snapping thing? It's likely that the cursor (user) would slow down when trying to position, or WDYT?
It might make the user experience better as well, right now there are so many lines moving around that it's quite confusing (to me at least).
Thoughts?
Additionally, I'm trying to think of a reasonable way to call setSnapLines less frequently, perhaps if the cursor has moved only 1px meanwhile then we shouldn't call setSnapLines again? This could bring inconsistencies though.
Not sure what could be a reasonable logic for setting the state less frequently. Perhaps even do it every X ms instead?
Any ideas/thoughts are welcome!
snapLines bleeds through to the div element, maybe that's the problem. None of the withSnapLines components actually use this prop, so it can be removed safely from the HoC (and even from the context provider). Maybe that helps?
If think that here were no problems with this early on, when the components actually used this property?
snapLinesbleeds through to the div element, maybe that's the problem.
Also reported in #3462. If it's not needed, removing sounds good to me.
@barklund If I understood correctly then you meant removing this, is that correct?
https://github.com/ampproject/amp-wp/blob/a9b50d973e089290ae5ed5795717f277051ac076/assets/src/stories-editor/components/contexts/snapping/index.js#L28
If that's correct, then I tried removing this, it doesn't seem to make any change, still lagging.
It seems like just calling setSnapLines, even if I try with always setting it to [] instead of the actual snap lines, makes the dragging lag.
Q: Is it generally a "normal" use-case to use useState hundreds of times per second?
I have updated the PR at #3484 so that the fix suggested by Miina.
As noted, this change doesn't improve performance noticeably. But it is still a valid change, as does fix the notice error.
Q: Is it generally a "normal" use-case to use
useStatehundreds of times per second?
I would say no. Calling the store should be seen as slow / unreliable, depending the size of the store. Is there anyway to limit the number of calls to store? Either with batching or a simple timeout?
Is there anyway to limit the number of calls to store? Either with batching or a simple timeout?
Also had a similar and some additional ideas, in this and this comment.
Would like to know first if calling the useState hundreds of times per second is the issue here or if there might be something else :)
@barklund Since you probably have more experience with hooks, then .. Based on your experience, do you think that just calling useState that often might cause the lag, or is it a common use-case?
In the initial implementation I made, there was the same updating of the local state inside the context on every mouse move and it had no noticeable lag.
Updating an internal state locally in a component is cheap. Updating the global registry is expensive, but I don't think it's doing that.
Have you tried using the built-in profiler to hunt for the problematic functions?
Have you tried using the built-in profiler to hunt for the problematic functions?
No, I haven't, I've so far hunted the problematic functions manually and detected that setSnapLines makes it lag.
I've tested the following:
setSnapLines with the actual value (and removing getBestSnapLines at all, setting it to [] -- still lagging.setSnapLines with [] and additionally _not using the set value for anything_ -- still laggingsetSnapLines at all but doing everything else (getBestSnapLines, etc.) -- lagging is gone!So, from that, I concluded that setSnapLines is the culprit.
Not sure what else (in the plugin) might influence this.
Have you tried using the built-in profiler to hunt for the problematic functions?
Do you mean the Chrome Profiler for example? _Will check now_
Do you mean the Chrome Profiler for example? Will check now
I think he meant the one in React dev tools
Tried the built-in (not React) profile and looks like it's the setSnapLines (dispatchAction) if I debugged correctly.

I figured it out. Debounce helped a lot, but I also recommend making less potential snap lines as described above. Please review @swissspidy (you probably understand the current code the best).
Debounce setSnapLines by 10 ms.
Why 10ms? Just interested how you came to that number. Why not 15 or 20?
IB looks good to me. The 10ms question is valid though, but I assume you got to that by trial and error. We can play with it and tweak it as necessary when doing the actual implementation. Important is that we're doing the debouncing in the first place.
Moved to the execution board now 👍
It needs an estimation first.
🤦♂ I got too excited here I guess
Does this really need estimations? The work is basically done.
Yep, everything needs an estimation. Also, the Implementation Brief has more than just adding debounce -- it's also for snapLines.
If it would be basically done then it could have XS as the estimation, however, the estimation should also include reducing the amount of snaplines.
Does this really need estimations? The work is basically done.
The second part about splitting into edge and center snap lines is not done, only described. It's only a few hours though, but require updating helper functions and unit tests for said functions, so XS or even S is probably appropriate.
Added an estimation and sending it to the execution board now :)
Debounce setSnapLines by 10 ms.
Why 10ms? Just interested how you came to that number. Why not 15 or 20?
Trial-and-error in deed. 5ms was still a bit laggy when moving quickly around. 20ms felt too delayed. But feel free to play around.
Trial-and-error in deed. 5ms was still a bit laggy when moving quickly around. 20ms felt too delayed. But feel free to play around.
Fair enough. I trust you picked the best value. Just wondered.
See #2992 for general snapping screencast.
still lagging
video - https://drive.google.com/file/d/1Nlj9UzKYckZNIKoqRIsj4zbhXbPKTglE/view
win10/chrome - Version 77.0.3865.120 (Official Build) (64-bit)
@csossi Could you test this again just in case? Not seeing the issue, tried both locally and in the test environment.
If you're still seeing the issue, could you please link the Story you were testing with as well?
Thank you!
Also interesting would be if the lagging continues when holding down the alt/option key while dragging.
Verified in QA
Most helpful comment
I figured it out. Debounce helped a lot, but I also recommend making less potential snap lines as described above. Please review @swissspidy (you probably understand the current code the best).