SlideShare uma empresa Scribd logo
1 de 32
Greenthreading in Flex
Presented by Huyen Tue Dao
About me
Fell in love with
programming in a C++ class
in high school.

Flex developer since 2006.

Second time presenting,
first time at 360|Flex.

Weapons of choice in L4D2:
grenade launcher/katana
“…PATENTED APPROACH OF SHOOT FIRST, SHOOT LATER,   I AM THE STIG…OKAY, NOT REALLY BUT I DID PASS MY
SHOOT SOME MORE AND THEN WHEN EVERYBODY'S DEAD                        DRIVER’S TEST ON THE FIRST GO
TRY TO ASK A QUESTION OR TWO.”
Outline
The Problem

Some Solutions

Greenthreading

Comparison

Credit Where Credit Is Due

Conclusion

Questions

                    GREENTHREADING IN FLEX
The Problem
Flash Player well suited to certain tasks: animation.

Seems to be able to do several things at once:
concurrency.

However, it can get hung up if one of these things
requires lots of processing.

Does not take complex code to hang it up either.

Let me show you…

                   GREENTHREADING IN FLEX
The Problem
The problem revolves around Flash platform’s
event-driven, frame-base architecture.

Flash movies/SWFs: content divided into frames
played according to a timeline.




                 GREENTHREADING IN FLEX
FLASH SWF
                        1        2    3    4      5         6

         TIMELINE


                                     FLEX SWF
                                      1    2
                                                 FRAME 2 REPEATEDLY CALLED

                      TIMELINE

                            FRAME 1         FRAME 2
                  BOOTSTRAPPING/            APPLICATION CODE
                      PRELOADER

WHAT THE SWF KIND OF LOOKS LIKE
ARTIST (NOT-SO-MUCH) RENDERING
The Problem
Flash platform is event-driven: most everything is
triggered by an event.

Events collected into the Event Queue.

FP handles one event at time.

To get to the next frame? An event is triggered.



                  GREENTHREADING IN FLEX
1   2      3       4   5     6

         TIMELINE

    EVENTS DISPATCHED IN FRAME 1




   EVENT QUEUE




               KEYBOARD EVENT       MOUSE EVENTS
                                                           *FRAME EVENT


THE EVENT QUEUE
ARTIST (NOT-SO-MUCH) RENDERING
The Problem
SWF plays at a certain number of frames per second
(FPS). The time per frame is 1/FPS.

Default FPS is 24. Therefore, a frame needs to
execute every ~42 milliseconds.

Application FPS is not a minimum, it’s a maximum.

If code running in a frame takes longer than allotted
time, FP cannot process next frame event “on time.”

Hello, non-responsive window/beach ball of death.

                   GREENTHREADING IN FLEX
The Problem
Another way to look at it, using Ted Patrick’s Elastic Mental Racetrack:




             IMAGE FROM SEAN CHRISTMANN’S UPDATED ‘ELASTIC RACETRACK’ FOR FLASH 9 AND AVM2



The racetrack extends to accommodate whatever code it is asked to execute.

No built-in way to defer execution till later.

If FP starts executing long-running code, code execution half will just keep
elongating.

FP gets stuck running on the first half of the track.

                                     GREENTHREADING IN FLEX
Some Solutions
General idea: break up long-running code into
smaller batches/jobs.

Trigger batches at different times, different frames.

Gives FP a chance to catch up.

Allow other events to be processed (including the
important screen-updating events).



                   GREENTHREADING IN FLEX
Some Solutions
Several ways to break up the process:

  callLater( ): function called using this will be queued and
  run either at handler for RENDER event or at the next
  ENTER_FRAME event

     UIComponent method

     “Some features, like effects, can cause queued
     functions to be delayed until the feature completes…”

  ENTER_FRAME: run a batch explicitly at each frame

  Timers: run a batch every χ milliseconds

                     GREENTHREADING IN FLEX
Some Solutions
AND NOW FOR A LITTLE CODE…
// To break up an algorithm via callLater()
// 1. Write a method that performs the main work of algorithm and takes as
// parameters the current iteration and the total iterations to execute.
// 2. In method heck for stopping condition and if it has not been reached,
// use callLater to schedule a new call to method.
...
<!-- Some object that is being displayed -->
<mx:UIComponent id=”obj”/>
...
var numIterations : int = 0;
var totalIterations : int = 100000;

private function runLoop(... args) : void
{
  // Do work of iteration here.
  numIterations++;
  if(numIterations < totalIterations)
  {
     obj.callLater( runLoop, /* args to pass to next method call */ );
  }
}

                                 GREENTHREADING IN FLEX
Some Solutions
AND NOW FOR A LITTLE CODE…

//   To break up an algorithm via a ENTER_FRAME event.
//   1. Initialize progress variables.
//   2. Add a listener to the application for the ENTER_FRAME event the does
//     main work of the algorithm.
//   3. In handler check for stopping condition and remove the listener if it
//     has been reached.

var numIterations : int = 0;
var totalIterations : int = 100000;
Application.application.addEventListener(Event.ENTER_FRAME, runLoop);

private function runLoop(event : Event) : void
{
   // Do work of iteration here.
     numIterations++;
     if(numIterations >= totalIterations)
     {
        Application.application.removeEventListener(Event.ENTER_FRAME,runLoop);
     }
}


                                   GREENTHREADING IN FLEX
Some Solutions
AND NOW FOR A LITTLE CODE…
//   To break up an algorithm via a Timer
//   1. Initialize progress variables.
//   2. Initialize a Timer with a delay. Here delay ≈≈ time/frame.
//   3. Add listener for TimerEvent.TIMER that does main work of algorithm.
//   4. In handler check for stopping condition and call stop() on Timer if it
//     has been reached.

var numIterations : int = 0;
var totalIterations : int = 100000;
var timer : Timer = new Timer(1000 / Application.application.stage.frameRate);
timer.addEventListener(TimerEvent.TIMER, runLoop);
timer.start();

private function runLoop(event : TimerEvent) : void
{
   // Do work of iteration here.
     numIterations++;
     if(numIterations >= totalIterations)
     {
        timer.stop();
     }
     // Else next iteration will execute next TimerEvent.TIMER event.
}
                                    GREENTHREADING IN FLEX
Some Solutions
The good:

 Event Queue can process other events between
 batches.

 Application remains responsive.




                GREENTHREADING IN FLEX
Some Solutions
The not-so-good:

  Code becomes little more complex.

  Can take much longer to finish the job:

     Spread out over time.

     More overhead.

  Fine tune amount of work done and how often you do it.

     Can be time-consuming for developer.

     Might need to re-tune if code or data changes.


                       GREENTHREADING IN FLEX
Greenthreading
Multi-threading on FP:

   Developers can’t access threading FP uses:

      Networking

      Watchers on your code for timeouts

   True threading available via PixelBender:

      Designed for media processing and to take advantage of ability of
      shaders, filters, and blenders to run multi-threaded.

      Works great if you can find a way to convert your problem space to
      PIxelBender’s.


                            GREENTHREADING IN FLEX
Greenthreading
So what is a “green thread?”

   Want to give developer more control over when code gets executed.

   Can emulate threads in developer code, i.e., green threads.

   Similar to how other platforms provide threading even for single
   processor systems.

General idea:

   Do as much work as possible in frame.

   Leave enough time for other events to process, for screen to
   update.

                          GREENTHREADING IN FLEX
Greenthreading
How does it work?

  Specify some amount of time to leave for other events:
  EPSILON

  Break long-running code into basic blocks that will run
  repeatedly.

  Each time greenthread finishes with a block, check how
  much total time used so far in frame.

  If the total time used >= TIME_PER_FRAME - EPSILON,
  stop running for now.

                      GREENTHREADING IN FLEX
EXECUTED BEFORE         EPSILON
              GREENTHREAD STARTS
                                   2

          AFTER 1 ITERATIONS


                                   2
          AFTER 2 ITERATIONS


                                   2
          AFTER X ITERATIONS

                                                   EXECUTED AFTER
                                                 GREENTHREAD PAUSES
                                   2
     WAITING FOR NEXT FRAME



GREENTHREADING
ARTIST (NOT-SO-MUCH) RENDERING
Greenthreading
Encapsulate all this as a class: GreenThread by
Charlie Hubbard.

http://code.google.com/p/greenthreads/

Hubbard’s class also provides statistics, progress
monitoring via events.

To use: convert your code block/algorithm/job etc.
to a GreenThread.


                  GREENTHREADING IN FLEX
Greenthreading
AND NOW FOR A LITTLE CODE…
public class MyGreenthreadAlgorithm extends GreenThread
{
  // Called when start() called on a GreenThread
  override public function initialize() : void
  {
    // Initialize variables needed.
    progress = 0;
    maximum = 100;
  }

    override public function run() : Boolean
    {
      // Do work of one iteration here.
      progress++;
      // Return true if done, false otherwise.
    }
}
                           GREENTHREADING IN FLEX
ALGORITHM
                                                                                                  1 ITERATION


 DIVIDE + CONQUER                    ENTER_FRAME                TIMER

                                     1        2             1           2
                                                                                                  LEFTOVER
                                                                                                    TIME



       INCREASE ITERATIONS
          RUN PER FRAME



        ENTER_FRAME              TIMER                                           GREENTHREADING    EPSILON


       1         2           1           2                                       1          2       KEY
                                                   SPECIFY EPSILON: SET AMOUNT
                                                    OF TIME LEFT IN FRAME FOR
                                                   PROCESSING OTHER EVENTS.
                                                    FILL UP REMAINING FRAME
                                                      TIME WITH ITERATIONS




GREENTHREADING VS OTHER SOLUTIONS
ARTIST (NOT-SO-MUCH) RENDERING, NOT DRAWN TO SCALE
Timer             Timer
                    Original                                       GreenThread
                               (1 loop/frame)   (20k loop/frame)


   Runtime         58 sec*      ~11 hrs -          62 sec           85 sec

 Slow Down          None*        V. Large           Small          Moderate

 Responsive                                        NOT
                      NO           YES                                YES
     ?                                            REALLY


                               *If the application doesn’t crash that is.

DEMO COMPARISON
RUNTIME AND RESPONSIVENESS
Greenthreading
The not-so-good:

 Still have overhead.

   More code complexity

   Runtime increase

 Still may need to make adjustments to time left
 over to rest of events.


                   GREENTHREADING IN FLEX
Greenthreading
The good:

 Application responsiveness

 Greenthread determines how much work to do
 itself given bounds.

 Greenthread encapsulates management of time/
 work. Makes some things neater.



                GREENTHREADING IN FLEX
Greenthreading
But what about multi-threading?

  Important to maintain EPSILON time to handle rest of the
  execution and rendering of the frame.

  Queue up Greenthreads as they are instantiated.

  Time allocated to each Greenthread: (time for frame -
  EPSILON) / # active Greenthreads.

  When Greenthreads finish executing, they are removed from
  queue.

  Implemented in Hubbard’s library: just create as many
  GreenThreads as needed.

                       GREENTHREADING IN FLEX
Credit Where Credit Is
Due
Doug Knudsen: “AIR, CSVs, and Mean Greenies
oh my!” @ cubicleman.com

Charlie Hubbard, “Actionscript and Concurrency”
@ wrongnotes.blogspot.com

Drew Cummins, “Green Threads” @
blog.generalrelativity.org

Jesse Warden, “Parsing & Rendering Lots of Data
in Flash Player” @ jessewarden.com

                 GREENTHREADING IN FLEX
Conclusion
Applications that have long-running code can hang because Event
Queue stalls.

To keep application responsive:

  Break work down into small jobs

  Trigger jobs at different time, give Event Queue a chance to
  process other events.

Greenthreading: fit as much work into frame as possible, leave
time for other events.

Costs of greenthreading: more complex code, little longer runtime.


                        GREENTHREADING IN FLEX
Questions?


    THANKS FOR COMING!


                                 HUYEN TUE DAO
                              DAOTUEH@GMAIL.COM
                       QUEENCODEMONKEY @ TWITTER
                        WWW.QUEENCODEMONKEY.COM
        GREENTHREADING IN FLEX
Links
Greenthreading

   http://www.cubicleman.com/2009/03/08/air-csvs-and-mean-greenies-oh-my/

   http://wrongnotes.blogspot.com/2009/02/concurrency-and-actionscript-part-
   i-of.html

   http://blog.generalrelativity.org/actionscript-30/green-threads/

   http://jessewarden.com/2009/02/parsing-rendering-lots-of-data-in-flash-
   player.html

Frame execution model

   http://www.craftymind.com/2008/04/18/updated-elastic-racetrack-for-flash-9-
   and-avm2/

   http://www.onflex.org/ted/2005/07/flash-player-mental-model-elastic.php


                              GREENTHREADING IN FLEX

Mais conteúdo relacionado

Mais procurados

Mais procurados (19)

Ch7 Process Synchronization galvin
Ch7 Process Synchronization galvinCh7 Process Synchronization galvin
Ch7 Process Synchronization galvin
 
Operating systems question bank
Operating systems question bankOperating systems question bank
Operating systems question bank
 
Transactional Memory
Transactional MemoryTransactional Memory
Transactional Memory
 
Operating System 5
Operating System 5Operating System 5
Operating System 5
 
CPU Scheduling
CPU SchedulingCPU Scheduling
CPU Scheduling
 
Deadlocks
DeadlocksDeadlocks
Deadlocks
 
Reviewer cpu scheduling
Reviewer cpu schedulingReviewer cpu scheduling
Reviewer cpu scheduling
 
Module-related pages
Module-related pagesModule-related pages
Module-related pages
 
Cpu Scheduling Galvin
Cpu Scheduling GalvinCpu Scheduling Galvin
Cpu Scheduling Galvin
 
Final Exam OS fall 2012-2013 with answers
Final Exam OS fall 2012-2013 with answersFinal Exam OS fall 2012-2013 with answers
Final Exam OS fall 2012-2013 with answers
 
OperatingSystemChp3
OperatingSystemChp3OperatingSystemChp3
OperatingSystemChp3
 
Chapter4
Chapter4Chapter4
Chapter4
 
PART-3 : Mastering RTOS FreeRTOS and STM32Fx with Debugging
PART-3 : Mastering RTOS FreeRTOS and STM32Fx with DebuggingPART-3 : Mastering RTOS FreeRTOS and STM32Fx with Debugging
PART-3 : Mastering RTOS FreeRTOS and STM32Fx with Debugging
 
Chapter05 new
Chapter05 newChapter05 new
Chapter05 new
 
Scheduling Algorithms
Scheduling AlgorithmsScheduling Algorithms
Scheduling Algorithms
 
Synchronization linux
Synchronization linuxSynchronization linux
Synchronization linux
 
ITFT_Semaphores and bounded buffer
ITFT_Semaphores and bounded bufferITFT_Semaphores and bounded buffer
ITFT_Semaphores and bounded buffer
 
SCP
SCPSCP
SCP
 
Hard versus Soft real time system
Hard versus Soft real time systemHard versus Soft real time system
Hard versus Soft real time system
 

Semelhante a 360|Flex Greenthreading In Flex

Python multithreading
Python multithreadingPython multithreading
Python multithreadingJanu Jahnavi
 
Python multithreading
Python multithreadingPython multithreading
Python multithreadingJanu Jahnavi
 
Deep into your applications, performance & profiling
Deep into your applications, performance & profilingDeep into your applications, performance & profiling
Deep into your applications, performance & profilingFabien Arcellier
 
BEAM (Erlang VM) as a Soft Real-time Platform
BEAM (Erlang VM) as a Soft Real-time PlatformBEAM (Erlang VM) as a Soft Real-time Platform
BEAM (Erlang VM) as a Soft Real-time PlatformHamidreza Soleimani
 
Microsoft kafka load imbalance
Microsoft   kafka load imbalanceMicrosoft   kafka load imbalance
Microsoft kafka load imbalanceNitin Kumar
 
Os2 2
Os2 2Os2 2
Os2 2issbp
 
Linux Security and How Web Browser Sandboxes Really Work (NDC Oslo 2017)
Linux Security  and How Web Browser Sandboxes Really Work (NDC Oslo 2017)Linux Security  and How Web Browser Sandboxes Really Work (NDC Oslo 2017)
Linux Security and How Web Browser Sandboxes Really Work (NDC Oslo 2017)Patricia Aas
 
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)Tim Bunce
 
Let's Talk Locks!
Let's Talk Locks!Let's Talk Locks!
Let's Talk Locks!C4Media
 
04 threads-pbl-2-slots
04 threads-pbl-2-slots04 threads-pbl-2-slots
04 threads-pbl-2-slotsmha4
 
04 threads-pbl-2-slots
04 threads-pbl-2-slots04 threads-pbl-2-slots
04 threads-pbl-2-slotsmha4
 
HARD COPY REPORT CDAC
HARD COPY REPORT CDACHARD COPY REPORT CDAC
HARD COPY REPORT CDACSarthak Dubey
 
Import golang; struct microservice
Import golang; struct microserviceImport golang; struct microservice
Import golang; struct microserviceGiulio De Donato
 
The Beam Vision for Portability: "Write once run anywhere"
The Beam Vision for Portability: "Write once run anywhere"The Beam Vision for Portability: "Write once run anywhere"
The Beam Vision for Portability: "Write once run anywhere"Knoldus Inc.
 
DDD Framework for Java: JdonFramework
DDD Framework for Java: JdonFrameworkDDD Framework for Java: JdonFramework
DDD Framework for Java: JdonFrameworkbanq jdon
 
Developing a Reverberation Plugin with the aim of a Genetic Algorithm
Developing a Reverberation Plugin with the aim of a Genetic AlgorithmDeveloping a Reverberation Plugin with the aim of a Genetic Algorithm
Developing a Reverberation Plugin with the aim of a Genetic AlgorithmLorenzo Monni
 
Devel::NYTProf 2009-07 (OUTDATED, see 201008)
Devel::NYTProf 2009-07 (OUTDATED, see 201008)Devel::NYTProf 2009-07 (OUTDATED, see 201008)
Devel::NYTProf 2009-07 (OUTDATED, see 201008)Tim Bunce
 

Semelhante a 360|Flex Greenthreading In Flex (20)

Python multithreading
Python multithreadingPython multithreading
Python multithreading
 
Python multithreading
Python multithreadingPython multithreading
Python multithreading
 
Deep into your applications, performance & profiling
Deep into your applications, performance & profilingDeep into your applications, performance & profiling
Deep into your applications, performance & profiling
 
BEAM (Erlang VM) as a Soft Real-time Platform
BEAM (Erlang VM) as a Soft Real-time PlatformBEAM (Erlang VM) as a Soft Real-time Platform
BEAM (Erlang VM) as a Soft Real-time Platform
 
Microsoft kafka load imbalance
Microsoft   kafka load imbalanceMicrosoft   kafka load imbalance
Microsoft kafka load imbalance
 
Os2 2
Os2 2Os2 2
Os2 2
 
Linux Security and How Web Browser Sandboxes Really Work (NDC Oslo 2017)
Linux Security  and How Web Browser Sandboxes Really Work (NDC Oslo 2017)Linux Security  and How Web Browser Sandboxes Really Work (NDC Oslo 2017)
Linux Security and How Web Browser Sandboxes Really Work (NDC Oslo 2017)
 
Java multi thread programming on cmp system
Java multi thread programming on cmp systemJava multi thread programming on cmp system
Java multi thread programming on cmp system
 
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
 
Terraforming
Terraforming Terraforming
Terraforming
 
Let's Talk Locks!
Let's Talk Locks!Let's Talk Locks!
Let's Talk Locks!
 
04 threads-pbl-2-slots
04 threads-pbl-2-slots04 threads-pbl-2-slots
04 threads-pbl-2-slots
 
04 threads-pbl-2-slots
04 threads-pbl-2-slots04 threads-pbl-2-slots
04 threads-pbl-2-slots
 
HARD COPY REPORT CDAC
HARD COPY REPORT CDACHARD COPY REPORT CDAC
HARD COPY REPORT CDAC
 
05 Transactions
05 Transactions05 Transactions
05 Transactions
 
Import golang; struct microservice
Import golang; struct microserviceImport golang; struct microservice
Import golang; struct microservice
 
The Beam Vision for Portability: "Write once run anywhere"
The Beam Vision for Portability: "Write once run anywhere"The Beam Vision for Portability: "Write once run anywhere"
The Beam Vision for Portability: "Write once run anywhere"
 
DDD Framework for Java: JdonFramework
DDD Framework for Java: JdonFrameworkDDD Framework for Java: JdonFramework
DDD Framework for Java: JdonFramework
 
Developing a Reverberation Plugin with the aim of a Genetic Algorithm
Developing a Reverberation Plugin with the aim of a Genetic AlgorithmDeveloping a Reverberation Plugin with the aim of a Genetic Algorithm
Developing a Reverberation Plugin with the aim of a Genetic Algorithm
 
Devel::NYTProf 2009-07 (OUTDATED, see 201008)
Devel::NYTProf 2009-07 (OUTDATED, see 201008)Devel::NYTProf 2009-07 (OUTDATED, see 201008)
Devel::NYTProf 2009-07 (OUTDATED, see 201008)
 

Último

Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 

Último (20)

Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 

360|Flex Greenthreading In Flex

  • 2. About me Fell in love with programming in a C++ class in high school. Flex developer since 2006. Second time presenting, first time at 360|Flex. Weapons of choice in L4D2: grenade launcher/katana “…PATENTED APPROACH OF SHOOT FIRST, SHOOT LATER, I AM THE STIG…OKAY, NOT REALLY BUT I DID PASS MY SHOOT SOME MORE AND THEN WHEN EVERYBODY'S DEAD DRIVER’S TEST ON THE FIRST GO TRY TO ASK A QUESTION OR TWO.”
  • 3. Outline The Problem Some Solutions Greenthreading Comparison Credit Where Credit Is Due Conclusion Questions GREENTHREADING IN FLEX
  • 4. The Problem Flash Player well suited to certain tasks: animation. Seems to be able to do several things at once: concurrency. However, it can get hung up if one of these things requires lots of processing. Does not take complex code to hang it up either. Let me show you… GREENTHREADING IN FLEX
  • 5. The Problem The problem revolves around Flash platform’s event-driven, frame-base architecture. Flash movies/SWFs: content divided into frames played according to a timeline. GREENTHREADING IN FLEX
  • 6. FLASH SWF 1 2 3 4 5 6 TIMELINE FLEX SWF 1 2 FRAME 2 REPEATEDLY CALLED TIMELINE FRAME 1 FRAME 2 BOOTSTRAPPING/ APPLICATION CODE PRELOADER WHAT THE SWF KIND OF LOOKS LIKE ARTIST (NOT-SO-MUCH) RENDERING
  • 7. The Problem Flash platform is event-driven: most everything is triggered by an event. Events collected into the Event Queue. FP handles one event at time. To get to the next frame? An event is triggered. GREENTHREADING IN FLEX
  • 8. 1 2 3 4 5 6 TIMELINE EVENTS DISPATCHED IN FRAME 1 EVENT QUEUE KEYBOARD EVENT MOUSE EVENTS *FRAME EVENT THE EVENT QUEUE ARTIST (NOT-SO-MUCH) RENDERING
  • 9. The Problem SWF plays at a certain number of frames per second (FPS). The time per frame is 1/FPS. Default FPS is 24. Therefore, a frame needs to execute every ~42 milliseconds. Application FPS is not a minimum, it’s a maximum. If code running in a frame takes longer than allotted time, FP cannot process next frame event “on time.” Hello, non-responsive window/beach ball of death. GREENTHREADING IN FLEX
  • 10. The Problem Another way to look at it, using Ted Patrick’s Elastic Mental Racetrack: IMAGE FROM SEAN CHRISTMANN’S UPDATED ‘ELASTIC RACETRACK’ FOR FLASH 9 AND AVM2 The racetrack extends to accommodate whatever code it is asked to execute. No built-in way to defer execution till later. If FP starts executing long-running code, code execution half will just keep elongating. FP gets stuck running on the first half of the track. GREENTHREADING IN FLEX
  • 11. Some Solutions General idea: break up long-running code into smaller batches/jobs. Trigger batches at different times, different frames. Gives FP a chance to catch up. Allow other events to be processed (including the important screen-updating events). GREENTHREADING IN FLEX
  • 12. Some Solutions Several ways to break up the process: callLater( ): function called using this will be queued and run either at handler for RENDER event or at the next ENTER_FRAME event UIComponent method “Some features, like effects, can cause queued functions to be delayed until the feature completes…” ENTER_FRAME: run a batch explicitly at each frame Timers: run a batch every χ milliseconds GREENTHREADING IN FLEX
  • 13. Some Solutions AND NOW FOR A LITTLE CODE… // To break up an algorithm via callLater() // 1. Write a method that performs the main work of algorithm and takes as // parameters the current iteration and the total iterations to execute. // 2. In method heck for stopping condition and if it has not been reached, // use callLater to schedule a new call to method. ... <!-- Some object that is being displayed --> <mx:UIComponent id=”obj”/> ... var numIterations : int = 0; var totalIterations : int = 100000; private function runLoop(... args) : void { // Do work of iteration here. numIterations++; if(numIterations < totalIterations) { obj.callLater( runLoop, /* args to pass to next method call */ ); } } GREENTHREADING IN FLEX
  • 14. Some Solutions AND NOW FOR A LITTLE CODE… // To break up an algorithm via a ENTER_FRAME event. // 1. Initialize progress variables. // 2. Add a listener to the application for the ENTER_FRAME event the does // main work of the algorithm. // 3. In handler check for stopping condition and remove the listener if it // has been reached. var numIterations : int = 0; var totalIterations : int = 100000; Application.application.addEventListener(Event.ENTER_FRAME, runLoop); private function runLoop(event : Event) : void { // Do work of iteration here. numIterations++; if(numIterations >= totalIterations) { Application.application.removeEventListener(Event.ENTER_FRAME,runLoop); } } GREENTHREADING IN FLEX
  • 15. Some Solutions AND NOW FOR A LITTLE CODE… // To break up an algorithm via a Timer // 1. Initialize progress variables. // 2. Initialize a Timer with a delay. Here delay ≈≈ time/frame. // 3. Add listener for TimerEvent.TIMER that does main work of algorithm. // 4. In handler check for stopping condition and call stop() on Timer if it // has been reached. var numIterations : int = 0; var totalIterations : int = 100000; var timer : Timer = new Timer(1000 / Application.application.stage.frameRate); timer.addEventListener(TimerEvent.TIMER, runLoop); timer.start(); private function runLoop(event : TimerEvent) : void { // Do work of iteration here. numIterations++; if(numIterations >= totalIterations) { timer.stop(); } // Else next iteration will execute next TimerEvent.TIMER event. } GREENTHREADING IN FLEX
  • 16. Some Solutions The good: Event Queue can process other events between batches. Application remains responsive. GREENTHREADING IN FLEX
  • 17. Some Solutions The not-so-good: Code becomes little more complex. Can take much longer to finish the job: Spread out over time. More overhead. Fine tune amount of work done and how often you do it. Can be time-consuming for developer. Might need to re-tune if code or data changes. GREENTHREADING IN FLEX
  • 18. Greenthreading Multi-threading on FP: Developers can’t access threading FP uses: Networking Watchers on your code for timeouts True threading available via PixelBender: Designed for media processing and to take advantage of ability of shaders, filters, and blenders to run multi-threaded. Works great if you can find a way to convert your problem space to PIxelBender’s. GREENTHREADING IN FLEX
  • 19. Greenthreading So what is a “green thread?” Want to give developer more control over when code gets executed. Can emulate threads in developer code, i.e., green threads. Similar to how other platforms provide threading even for single processor systems. General idea: Do as much work as possible in frame. Leave enough time for other events to process, for screen to update. GREENTHREADING IN FLEX
  • 20. Greenthreading How does it work? Specify some amount of time to leave for other events: EPSILON Break long-running code into basic blocks that will run repeatedly. Each time greenthread finishes with a block, check how much total time used so far in frame. If the total time used >= TIME_PER_FRAME - EPSILON, stop running for now. GREENTHREADING IN FLEX
  • 21. EXECUTED BEFORE EPSILON GREENTHREAD STARTS 2 AFTER 1 ITERATIONS 2 AFTER 2 ITERATIONS 2 AFTER X ITERATIONS EXECUTED AFTER GREENTHREAD PAUSES 2 WAITING FOR NEXT FRAME GREENTHREADING ARTIST (NOT-SO-MUCH) RENDERING
  • 22. Greenthreading Encapsulate all this as a class: GreenThread by Charlie Hubbard. http://code.google.com/p/greenthreads/ Hubbard’s class also provides statistics, progress monitoring via events. To use: convert your code block/algorithm/job etc. to a GreenThread. GREENTHREADING IN FLEX
  • 23. Greenthreading AND NOW FOR A LITTLE CODE… public class MyGreenthreadAlgorithm extends GreenThread { // Called when start() called on a GreenThread override public function initialize() : void { // Initialize variables needed. progress = 0; maximum = 100; } override public function run() : Boolean { // Do work of one iteration here. progress++; // Return true if done, false otherwise. } } GREENTHREADING IN FLEX
  • 24. ALGORITHM 1 ITERATION DIVIDE + CONQUER ENTER_FRAME TIMER 1 2 1 2 LEFTOVER TIME INCREASE ITERATIONS RUN PER FRAME ENTER_FRAME TIMER GREENTHREADING EPSILON 1 2 1 2 1 2 KEY SPECIFY EPSILON: SET AMOUNT OF TIME LEFT IN FRAME FOR PROCESSING OTHER EVENTS. FILL UP REMAINING FRAME TIME WITH ITERATIONS GREENTHREADING VS OTHER SOLUTIONS ARTIST (NOT-SO-MUCH) RENDERING, NOT DRAWN TO SCALE
  • 25. Timer Timer Original GreenThread (1 loop/frame) (20k loop/frame) Runtime 58 sec* ~11 hrs - 62 sec 85 sec Slow Down None* V. Large Small Moderate Responsive NOT NO YES YES ? REALLY *If the application doesn’t crash that is. DEMO COMPARISON RUNTIME AND RESPONSIVENESS
  • 26. Greenthreading The not-so-good: Still have overhead. More code complexity Runtime increase Still may need to make adjustments to time left over to rest of events. GREENTHREADING IN FLEX
  • 27. Greenthreading The good: Application responsiveness Greenthread determines how much work to do itself given bounds. Greenthread encapsulates management of time/ work. Makes some things neater. GREENTHREADING IN FLEX
  • 28. Greenthreading But what about multi-threading? Important to maintain EPSILON time to handle rest of the execution and rendering of the frame. Queue up Greenthreads as they are instantiated. Time allocated to each Greenthread: (time for frame - EPSILON) / # active Greenthreads. When Greenthreads finish executing, they are removed from queue. Implemented in Hubbard’s library: just create as many GreenThreads as needed. GREENTHREADING IN FLEX
  • 29. Credit Where Credit Is Due Doug Knudsen: “AIR, CSVs, and Mean Greenies oh my!” @ cubicleman.com Charlie Hubbard, “Actionscript and Concurrency” @ wrongnotes.blogspot.com Drew Cummins, “Green Threads” @ blog.generalrelativity.org Jesse Warden, “Parsing & Rendering Lots of Data in Flash Player” @ jessewarden.com GREENTHREADING IN FLEX
  • 30. Conclusion Applications that have long-running code can hang because Event Queue stalls. To keep application responsive: Break work down into small jobs Trigger jobs at different time, give Event Queue a chance to process other events. Greenthreading: fit as much work into frame as possible, leave time for other events. Costs of greenthreading: more complex code, little longer runtime. GREENTHREADING IN FLEX
  • 31. Questions? THANKS FOR COMING! HUYEN TUE DAO DAOTUEH@GMAIL.COM QUEENCODEMONKEY @ TWITTER WWW.QUEENCODEMONKEY.COM GREENTHREADING IN FLEX
  • 32. Links Greenthreading http://www.cubicleman.com/2009/03/08/air-csvs-and-mean-greenies-oh-my/ http://wrongnotes.blogspot.com/2009/02/concurrency-and-actionscript-part- i-of.html http://blog.generalrelativity.org/actionscript-30/green-threads/ http://jessewarden.com/2009/02/parsing-rendering-lots-of-data-in-flash- player.html Frame execution model http://www.craftymind.com/2008/04/18/updated-elastic-racetrack-for-flash-9- and-avm2/ http://www.onflex.org/ted/2005/07/flash-player-mental-model-elastic.php GREENTHREADING IN FLEX

Notas do Editor

  1. Frame event: especially important because where drawing of the stage occurs.
  2. Bring up simple parser application code and the parsing loop and reiterate that because application keeps processing loop, application becomes unresponsive, hangs, beach ball of death.
  3. Bring up simple parser application code and the parsing loop and reiterate that because application keeps processing loop, application becomes unresponsive, hangs, beach ball of death.
  4. Now go to application code and show parser example using callLater
  5. Now go to application code and show parser example using a Timer event.
  6. So what we have here is a trade-off between a quicker-running application that&amp;#x2019;s unresponsive and a responsive application that takes way too long to run.
  7. So what we have here is a trade-off between a quicker-running application that&amp;#x2019;s unresponsive and a responsive application that takes way too long to run.
  8. Frame event: especially important because where drawing of the stage occurs.
  9. Now go to main demo application and show green
  10. Links to these blog posts are included at the end of my slides.