11 Replies Latest reply on Apr 19, 2017 3:52 PM by Malcolm

    Are Calculations based on Get functions cached?

    alecgregory

      Does anyone know if the results of calculations based on Get functions are cached within a script?

       

      For example, if I am using GetValue ( Get ( ScriptParameter ); 1 ) four times in a script, is it more efficient to store it in a variable and all that four times or does it make no real difference? And does the answer change if the scope is a calculation rather than a script? From this post I know Get ( ScriptParameter ) is only evaluated once per script but I don't know if this extends to calculations based on it too.

       

      Thanks,


      Alec

       

        • 1. Re: Are Calculations based on Get functions cached?
          jbante

          alecgregory wrote:

           

          Does anyone know if the results of calculations based on Get functions are cached within a script?

          I doubt it. FileMaker's calculation speed does not generally look like it memoizes results within a script. I haven't specifically tested for this exact comparison, but the results I've gotten from scripts testing the speeds of different calculations wouldn't make any sense if FileMaker was in fact memoizing calculation results within the script.

           

          That said, even if GetValue ( Get ( ScriptParameter ; n ) ) is/were just as fast as referring to a variable we previously set with that value, we should still opt for the variable every time. Using the variable makes the script more readable by saying what it's supposed to mean without imposing the extra cognitive burden of thinking about where it came from every time we see it.

           

          From that same thread you linked to, we saw that calculations do some interesting (and not thoroughly disclosed) things relevant to this. FileMaker is not likely to disclose the exact details. But if all we care about is the speed, we don't need to know how FileMaker achieved the effect to test if it's there. Have you tried building a calculation that would go a lot faster with memoization and compared its speed with a similar calculation that would not?

          1 of 1 people found this helpful
          • 2. Re: Are Calculations based on Get functions cached?
            alecgregory

            Thanks for the reply.

            I doubt it. FileMaker's calculation speed does not generally look like it memoizes results within a script. I haven't specifically tested for this exact comparison, but the results I've gotten from scripts testing the speeds of different calculations wouldn't make any sense if FileMaker was in fact memoizing calculation results within the script.

            Good to know.

            That said, even if GetValue ( Get ( ScriptParameter ; n ) ) is/were just as fast as referring to a variable we previously set with that value, we should still opt for the variable every time. Using the variable makes the script more readable by saying what it's supposed to mean without imposing the extra cognitive burden of thinking about where it came from every time we see it.

            I agree with this in general. However, I was thinking from a memory usage perspective as well as a speed perspective. That is, I thought it might use twice as much memory to make a mutable copy of GetValue ( Get ( ScriptParameter ); $i ) in a variable because both the memoized result and the variable would be stored. This could become non-trivial with very large script parameters, which are a possibility in some of the scripts I work with.

            Have you tried building a calculation that would go a lot faster with memoization and compared its speed with a similar calculation that would not?

            I have not. I'll give it a go and post back here if I come up with interesting results.

            • 3. Re: Are Calculations based on Get functions cached?
              alecgregory

              I've done some tests and they strongly suggest no memoization within a script or a calculation.

              • 4. Re: Are Calculations based on Get functions cached?
                DavidJondreau

                I can't see how. There are a lot of Get() functions that would break if it were. Like Get ( CurrentTimestamp ), Get ( RecordNumber ), Get ( FoundCount )

                 

                If I recall correctly, re-evaluating Get ( SystemPlatform ) is faster than calling a $system.platform variable that had been set earlier.

                1 of 1 people found this helpful
                • 5. Re: Are Calculations based on Get functions cached?
                  philmodjunk

                  In any case,

                   

                  Set Variable [ $DescriptiveParameterName ; value: GetValue ( Get ( ScriptParameter ) ; 1 ) ]

                   

                  Then using $DescriptiveParameterName anywhere that it's needed in your script makes for easier to read and debug scripts than constantly having to figure which value was the first value in the list.

                   

                  And there are significant limitations to parameter lists that need to be respected when you use this method of passing multiple parameter values to a script.

                  • 6. Re: Are Calculations based on Get functions cached?
                    alecgregory

                    Thanks for the additional responses. I don't use the GetValue ( Get ( ScriptParameter ); n ) method for passing parameters, I just wanted to give a simple example that would be familiar to most people. And, yes, I agree that $descriptiveParameterName is the best approach in general but wanted to make sure that I wasn't doubling the memory usage of a particular script or calculation by doing so. That's significant when working with very large parameters.

                    • 7. Re: Are Calculations based on Get functions cached?
                      Malcolm

                      If you are working with very large parameters make sure that you stay within the capacity of 1,000,000 characters.

                       

                      malcolm

                      • 8. Re: Are Calculations based on Get functions cached?
                        alecgregory

                        I believe the script parameter capacity is 10,000,000 characters using Perform Script and 1,000,000 for Perform Script On Server. I do have "Long Parameter" versions of my PSoS scripts that may reach the 1,000,000 limit. They store the parameter in a script parameter record which the server can access. To handle a script parameter of 10,000,000 characters or higher I suppose I'd have to span the parameter across multiple records or multiple field repetitions or return an error.

                        • 9. Re: Are Calculations based on Get functions cached?
                          Malcolm

                          Perform Script on Server documents the one million character limit. General information about the Perform Script On Server script step | FileMaker

                           

                          I can't locate a limit for the Perform Script parameter. Where did you find the ten million figure?

                           

                          Malcolm

                          • 10. Re: Are Calculations based on Get functions cached?
                            alecgregory

                            Somewhere on here! As I recall I tested it at the time.

                            • 11. Re: Are Calculations based on Get functions cached?
                              Malcolm

                              You can't trust everything you read here ;-)

                               

                              I've just done a quick test and was able to pass 151 million characters in a parameter. That wasn't the maximum, I simply stopped there.

                               

                              Malcolm