Skip to content

Introduced FLiftExpensive to avoid calling expansive functions again#1491

Open
Eugeny48 wants to merge 2 commits into
masterfrom
FLiftExpensive
Open

Introduced FLiftExpensive to avoid calling expansive functions again#1491
Eugeny48 wants to merge 2 commits into
masterfrom
FLiftExpensive

Conversation

@Eugeny48
Copy link
Copy Markdown
Contributor

We use Transform a lot. But sometimes Transform behaves unclear.

For example we can use transforms to create trees.

classId2learnersT = fselectLift(learnersB, \learners -> valuesToArrayTree(learners, \learner -> learner.classId);

We do not expect to recalculate this expensive tree until learnersB changes.
But it happens if we use classId2learnersT in fselect2, because fselect2 creates new function from our expansive function.
We do not want to change this behavior, because as @NikitaFil says, it may increase the number of DynamicBehaviours and memory consumption.

So we need FLiftExpensive to mark expensive functions and avoid unwanted recalculations.

Here is a test example https://github.com/area9innovation/flow9/compare/FLiftExpensive?expand=1#diff-97db1eea617660e99c205157e61ab96a911c7206daa6b1261e5149885ca19524
Output:

=== Using FLift
Creating transforms
  call fn8
Subscribing transforms from last to first
Subscribing transform 10
  call fn1
  call fn2
  call fn3
  call fn4
  call fn3
  call fn5
  call fn6
  call fn7
  call fn9
  call fn10
  Transform 10 calculated
Subscribing transform 9
  call fn9
  Transform 9 calculated
Transform 8 is already initialized
Transform 7 is already initialized
Transform 6 is already initialized
Transform 5 is already initialized
Transform 4 is already initialized
Subscribing transform 3
  call fn3
  Transform 3 calculated
Subscribing transform 2
  call fn2
  Transform 2 calculated
Transform 1 is already initialized
Getting values
[1, 1, 2, 3, 5, 14, 14, 14, 14, 28]
Done

=== Using FLiftExpensive
Creating transforms
Subscribing transforms from last to first
Subscribing transform 10
  call fn8
  call fn1
  call fn2
  call fn3
  call fn4
  call fn5
  call fn6
  call fn7
  call fn9
  call fn10
  Transform 10 calculated
Transform 9 is already initialized
Transform 8 is already initialized
Transform 7 is already initialized
Transform 6 is already initialized
Transform 5 is already initialized
Transform 4 is already initialized
Transform 3 is already initialized
Transform 2 is already initialized
Transform 1 is already initialized
Getting values
[1, 1, 2, 3, 5, 14, 14, 14, 14, 28]
Done

As you can see from the example above, an additional benefit of FLiftExpensive is that initializing a transform also initializes the influencing transforms with FLiftExpensive, even if they are behind fselect2.

@Eugeny48 Eugeny48 requested a review from NikitaFil May 16, 2025 13:36
Reworked `instantiateFn2Lazy`, now it returns `Maybe`;
Restored optimization for lazy Transforms in `fselect2`;
Test updated, lazy transforms involved;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant