Duet3D Logo Duet3D
    • Tags
    • Documentation
    • Order
    • Register
    • Login

    Coding techniques to conserve memory

    Scheduled Pinned Locked Moved
    Gcode meta commands
    3
    5
    194
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • DonStaufferundefined
      DonStauffer
      last edited by DonStauffer

      I'm working with a Duet2 WiFi, so I have a shortage of memory, so I'm trying to develop some programming techniques to minimize memory use. So I have an idea and I'd like to know if it can be made to work.

      For a simple example, let's say I have a variable A which is only used to calculate another variable B. Assume the code for the value of A and B is too complex to simply replace "var.A" in a single expression for B with the expression used to calculate A.

      My idea:

      var B = 0
      
      if true
          var A = 0
          ; Complex code to get the final value for A
          ; Complex code involving A, to get the final value of B
          ; A is never needed again
      var C = 0
      var D = 0
      ; Code which uses B
      

      Would I be correct that the end of the conditional block would see the deallocation of A? At that point I'm hoping that the memory in use would be only enough for the 3 variables B, C and D (assuming no other variables are defined in the macro). That is, that this would use 75% of the memory that an equivalent macro without the conditional would. If this works, I suspect I could do something similar with a while loop, and maybe other constructs (could I just indent, without any flow control statements, like a {compound statement} in C++?)

      I should mention, that for this to be useful, it would either have to be done more than once in a macro, or it would have to be done earlier than some other variables get defined.

      If this isn't a solution, I know I can offload the code of some macros into multiple nested calls, but that suffers from 3 limitations I can think of: 1. At this time, I can't return a value; 2. There's a limit to the nesting depth, and I'm already often up against it; 3. I assume there's some performance hit for a nested call. Nonetheless, I'm already doing this sometimes, but sometimes it's not ideal.

      If my idea doesn't work, is there something else I could do?

      Thanks for your help.

      droftartsundefined 1 Reply Last reply Reply Quote 0
      • droftartsundefined
        droftarts administrators @DonStauffer
        last edited by

        @DonStauffer See https://docs.duet3d.com/en/User_manual/Reference/Gcode_meta_commands#local-variable-declaration

        The scope of a local name is the remainder of the block in which it is declared.

        I think that's what you mean? You could test by echo-ing var.A outside the loop.

        Ian

        Bed-slinger - Mini5+ WiFi/1LC | RRP Fisher v1 - D2 WiFi | Polargraph - D2 WiFi | TronXY X5S - 6HC/Roto | CNC router - 6HC | Tractus3D T1250 - D2 Eth

        DonStaufferundefined 1 Reply Last reply Reply Quote 0
        • DonStaufferundefined
          DonStauffer @droftarts
          last edited by DonStauffer

          @droftarts I assumed the inability to use the variable outside its scope, so I wouldn't try to use it. I only intended to use this if I didn't need to. Hence the comment "A is never needed again".

          My question is whether it would, in fact, make the memory of A available, say, to be used for C for example, thus reducing the maximum memory requirements of the macro.

          Assuming my 4 variables are all 4 byte integers, and disregarding any overhead, the variables in my example should at most use 12 bytes, where the same code without the conditional would reach 16 bytes usage, if I'm correct in my assumptions. My main assumption is that when A goes out of scope, its memory becomes available even though the macro is still running.

          I do recognize that memory allocation schemes probably make it a little more complicated, like if memory gets allocated a page at a time or something. But that doesn't necessarily mean that if I use this technique a fair amount, I wouldn't be able to get a program to work that would run out of memory otherwise.

          So my question is whether that assumption about memory availability is correct. at least generally.

          droftartsundefined dc42undefined 2 Replies Last reply Reply Quote 0
          • droftartsundefined
            droftarts administrators @DonStauffer
            last edited by

            @DonStauffer I see, but I don't know! One for @dc42.

            Ian

            Bed-slinger - Mini5+ WiFi/1LC | RRP Fisher v1 - D2 WiFi | Polargraph - D2 WiFi | TronXY X5S - 6HC/Roto | CNC router - 6HC | Tractus3D T1250 - D2 Eth

            1 Reply Last reply Reply Quote 0
            • dc42undefined
              dc42 administrators @DonStauffer
              last edited by dc42

              @DonStauffer yes the memory allocated for var.A will be made available for use by other variables when it goes out of scope.

              Duet WiFi hardware designer and firmware engineer
              Please do not ask me for Duet support via PM or email, use the forum
              http://www.escher3d.com, https://miscsolutions.wordpress.com

              1 Reply Last reply Reply Quote 0
              • First post
                Last post
              Unless otherwise noted, all forum content is licensed under CC-BY-SA