6 Replies Latest reply on Feb 1, 2008 7:13 AM by sinious

    How many lines of code can you get away with in one frame/function?

    sinious Most Valuable Participant
      Been getting into some complex projects lately and I really hate to use the flash timeline at all. I like to live in code but I am still stuck in AS2 (having CS3 pro since it came out) because mProjector and MDM Zinc only supported it. (Yes, just yesterday Zinc 3 came out with full AS3 support, but I need to learn it)..

      Sooo I'm finding that I can get away with running up to about 4-5 thousand lines of code before flash player starts to noticably have trouble completing a frame in time (31fps). Albeit it obviously matters what the code is actually doing but I'm wondering how many lines do you all run before you find it starts to fall behind on framerate? What is reasonable?
        • 1. Re: How many lines of code can you get away with in one frame/function?
          i_am_captivate Level 1
          If you use .as files, I've seen files upwards of 10000 lines and run no problem (yes ten thousand)... just make external .as files and use include statements in your main flash file to call them. Like I say, I've seen multiple AS files used that are long, but have had no problem running them.

          HTH
          • 2. How many lines of code can you get away with in one frame/function?
            sinious Most Valuable Participant
            I use all AS2 classes in everything I do (seperate .as files).

            There are times I need to use the timeline to actually perform code though. In mProjector I made my application an installer/app/uninstaller all in one. Part of the install proceedure is to copy upwards of 2GB worth of files. Doing this in a loop using nothing but code obviously kills flashes ability to draw. So I need to set up for banging between a couple frames, copying a file per frame loop to let flash continue to draw (progress bars, etc).

            I wish code had a way of letting flash draw while it looped. Some kind of Flush() or Refresh() type command to force a frame draw to occur.

            The 5~6k lines I'm running are a combination of a C# COM interop and flash. There's a lot of lines running in C# that're syncronous so flash is literally waiting for C# to return data before continuing. So some of the code isn't even in flash honestly, but we're only talking a few hundred lines. The majority is flash..

            edit:

            Mostly the new flash "this script is taking too long" error is annoying for RIAs. If I toss up a synchronous call to a win32 dialog box (OK/CANCEL or YES/NO or just OK) and the user isn't paying attention and takes oh, a minute to respond, flash can jump in and say the script is taking too long (while it waits for you to click something, halting the code). It really needs to be disablable. I understand why it exists, but it makes RIAs look cheesy.
            • 3. How many lines of code can you get away with in one frame/function?
              Greg Dove Level 4
              The flash player makes the frame rate lag while its executing long scripts, that's true.

              You could change the length of time for the 'script taking too long error' in the publish settings.

              But....what I prefer to do in the situation you describe is 'timeslice' the work that needs to be done over frame loops:
              One way to keep the frame rate up is to sequence your activities in a 'queue' (basically and array with objects that contain either function references or object reference/function string values and argument arrays to pass to the functions when they are called). You can process chunks of the queue in an onEnterFrame handler, and time the amount taken for each 'chunk'. Once the cumulative amount gets too high for each frame... just do no more on that cycle and wait until the next one. You can even have a progress bar showing the completion of the processing.... (whatever the work is) because you know the length of the array that you're working through.

              Beyond that... there are some coding limits to be aware of...
              Individual classes can be no more than 32k (NB Uncompressed...does not relate to the size of the swf) in compiled bytecode. Its pretty difficult to extrapolate back to how many lines of source code though
              The same is true (in my experience) for a single function on a frame script. (Let's just assume I had a good reason for having a function that big lol)

              But a frame script can be any length AFAIK.

              • 4. Re: How many lines of code can you get away with in one frame/function?
                sinious Most Valuable Participant
                Thanks GWD, good information to know. Does AS3 have the same bytecode limit?

                The project itself doesn't really have more than 5k or so lines of code in total, between 2 classes. 3.5k in one, 1.5k in another. When I say a frame hits around 5-6k lines of code, I count loops. I do tons of code over and over and over. One function is frighteningly big spanning about 500 lines of code. Though it's a large branch that could be broken into 3 functions. Only one of 3 parts of the huge function fire off, but those parts are over a hundred lines each. They generally dynamically create movieclips and configure them, but sometimes end up doing it 100 times, which is easily 10k lines of code or more.

                On the part that it fires off that much code it really doesn't matter if it bogs. I planned on certain parts of the code taking a second.

                Though this development machine is a dual quad core rig (8 cores, 24ghz total, 8gb ram, 2TB 10k drives, openGL card, yada) so I don't notice any bogging. Even on the little p4 2.53ghz old 533fsb machines with ddr400 I don't notice even a hint of lag. This is what made me wonder how many lines of code is starting to push the limit on one frame.

                I could do as you suggested, and have already, but found that using the timeline was just as appealing. Programatically I'm happy to remove some of the lengthy code from the .as files and place it elsewhere (rather than create another class for one small purpose). But I wonder what the ceiling is.

                And I've never noticed that setting in publish settings. Is it for AS2 that I can configure this script warning?
                • 5. Re: How many lines of code can you get away with in one frame/function?
                  Greg Dove Level 4
                  Looping is runtime code execution. The bytecode limit happens at compile time. I don't know if there's an error thrown now or anything by the compiler. I recall encountering the limit with frame based functions in flash 8. There was no error... they just didn't work!
                  If your code compiles and actually works... then the bytecode limit is not an issue for you at all. But its useful to know perhaps.

                  From what you describe your code doesn't sound particularly onerous. Don't think of it in human terms. Its just a machine doing the work, lol.

                  For the script time limit. Check the flash tab in your publish settings. It is there for as2. I have never used it because I've always done things the way I described above. It usually makes the CPUs run a little cooler as well ;-)



                  • 6. Re: How many lines of code can you get away with in one frame/function?
                    sinious Most Valuable Participant
                    Omg I'm such a noob sometimes.. It's sitting there right under password the whole time.. Script time limit: 15 seconds. LoLz..

                    Thanks for the tip. Sometimes you get so close to something you don't even see it.. *cranks up to 99999999*