reflex icon indicating copy to clipboard operation
reflex copied to clipboard

Track state variable and attribute usage

Open benedikt-bartscher opened this issue 1 year ago β€’ 3 comments
trafficstars

Track if rx.Vars and their attributes are used (directly or via var operations) and only include used variables in the state dict. This should result in reduced initial state payloads and network traffic for var changes, which are not really needed in the frontend.

My plan is to write a second PR based on this one, which includes "used" properties (normal python properties with @property decorator) in the serialization. This allows users to use those properties in the frontend without always computing all properties for a given class. (related to my comment here)

Co-Author: @macmoritz

benedikt-bartscher avatar Jan 21 '24 20:01 benedikt-bartscher

Will have to continue holding off on this one for the time being, because it breaks with the reflex.dev website.

Flexdown currently redefines State classes multiple times per doc file, so they lose their _is_used markers.

masenf avatar Feb 20 '24 22:02 masenf

Needs #3214 for flexdown

benedikt-bartscher avatar May 04 '24 12:05 benedikt-bartscher

Will have to continue holding off on this one for the time being, because it breaks with the reflex.dev website.

Flexdown currently redefines State classes multiple times per doc file, so they lose their _is_used markers.

@masenf I just rebased onto main and tested this locally with flexdown branch benedikt-bartscher/per-block-module, and it seems to work fine. However, reflex-web ci is still failing

benedikt-bartscher avatar May 22 '24 22:05 benedikt-bartscher

@masenf did you release a new flexdown version yet? Shall we stall this one until the new var system is merged? Did you have usage tracking in mind while designing it?

benedikt-bartscher avatar Aug 19 '24 22:08 benedikt-bartscher

@benedikt-bartscher I just cut a new version of flexdown 0.1.5a10 off the latest main

picklelo avatar Aug 19 '24 23:08 picklelo

Thank you, @picklelo. I just raised https://github.com/reflex-dev/reflex-web/pull/855

benedikt-bartscher avatar Aug 20 '24 00:08 benedikt-bartscher

Finally, reflex-web ci is passing, thanks again @picklelo and @masenf.
Now we just need to figure out how we handle this with the new var system in #3743 cc @adhami3310

benedikt-bartscher avatar Aug 20 '24 10:08 benedikt-bartscher

Converted to draft because #3743 was merged. This needs some love to work with the new Immutable Vars

benedikt-bartscher avatar Aug 26 '24 20:08 benedikt-bartscher

@masenf @adhami3310 do you have any hints/ideas on how to implement this for ImmutableVar's?

benedikt-bartscher avatar Aug 27 '24 13:08 benedikt-bartscher

ideas on how to implement this for ImmutableVar

Since immutable vars can't have such fields, it could be possible to have a state-level mapping between vars and if they are used or not, although it will be fairly tricky to implement.

I'm not sure however how this will be that strong of an optimization considering that in an ideal application (almost) all vars will be used somewhere in the code, rendering all of the operations we do to figure out if a variable is being used unnecessary. Another issue is that it would slow down compile times (even further). Is there unseen advantages I'm missing?

adhami3310 avatar Aug 27 '24 18:08 adhami3310

Thanks for your feedback. You are right, it might introduce some additional compile time. But I think it should be possible without too much overhead, and (at least for us) runtime performance is more important.

an ideal application (almost) all vars will be used somewhere in the code While this should always be the goal, there are many cases where it is not true.

Some examples:

  • Reflex internal vars like router or dynamic router vars can not be disabled and may not be needed at all.
  • Libraries may share reusable State classes or Mixins, and the user might not want to use all vars.

Another advantage of usage tracking is automatic serialization of python/sqla/... properties if they are used in the frontend.

benedikt-bartscher avatar Aug 27 '24 18:08 benedikt-bartscher

@adhami3310 I completely forgot about one of the biggest benefits. Sorry, it has been a long time since I started this PR. Reflex currently always serializes complete root-level vars. Meaning, if you have f.e. a rx.Base class with 10 fields, reflex always serializes all of them if you don't provide a custom serializer.

benedikt-bartscher avatar Aug 27 '24 20:08 benedikt-bartscher

Closing this, continue here: https://github.com/reflex-dev/reflex/pull/3845

benedikt-bartscher avatar Aug 29 '24 20:08 benedikt-bartscher