Paul Robertson's words, punctuated

Thoughts on development, user-centered design, code, etc. by Paul Robertson

AS3 Concurrency/Workers use cases, best practices, and link roundup

Leif Wells responded to my presentation slides on ActionScript 3 Workers/Concurrency with a great question:

Is there a discussion somewhere on practical use cases for AS3 Workers? Best Practices? (Leif Wells on Twitter)

Use cases

I talked some about practical use cases in my presentation. The summary version is this: “any time you find yourself wishing you could make your code run asynchronously, consider putting it in a worker.” In other words, any time you’ve got code that’s doing some sort of work that takes so long that it causes pauses or stuttering in your user interface, that’s a good candidate for a Worker. In particular if the code is something you’d rather do “in the background.” Here are some concrete examples:

  • text/data processing (e.g. the example I gave in the slides of loading an xml file and looping over all the nodes to create an object graph)
  • visual/image/audio data processing (e.g. any image processing algorithm that isn’t already using PixelBender) – some examples are in the links below
  • Physics engine running in the background

Best practices

As for “best practices” that’s basically what I talked about in the section called “caveats” in slides 69-71. (These are also suggested and/or explicitly called out in the documentation.) The main suggestions I would make are:

  • Under normal circumstances, you shouldn’t use more than one or two background workers at most. Each worker is a full-blown instance of the ActionScript VM/runtime (without a visible stage). In essence you’re increasing memory use and (simultaneous, but distributed) processor use to trade off for not blocking your UI thread. Consider carefully if the cure is worse than the disease. (Remember that a single worker can do multiple operations – just not simultaneously.)
  • If you’re really done with a worker, terminate it to and the garbage collector will reclaim/free its memory.
  • Create a separate swf for your workers, rather than using the “same swf for main and worker” pattern. Many of the examples I link to below were created before Flash Builder 4.7 was released. Because of that, those examples tend to use the architecture of having a single swf file serve as both the main swf and the background worker SWF. Personally I strongly recommend against that just on general architectural principles. Before it was a matter of convenience – it’s a lot easier to test code if you only have to recompile one swf. In Flash Builder 4.7 they they added some nifty built-in features for creating and managing workers in projects, that’s no longer necessary because the IDE does the work for you =)

Links and resources

Leif’s question also made me realize that I’ve shared some links on Twitter about Workers, but it would be nice to compile them into a single post. So here is my collection of recommended links related to AS3 Workers:

Adobe documentation

  • Using workers for concurrency from the ActionScript 3.0 Developer’s Guide. In some ways this is a long-form version of my presentation. (Not surprising since I wrote both of them. =) he section on Understanding workers and concurrency has some of the same background as my presentation, and talks about best practices a bit. The other sections are more practical/hands on.
  • Worker class in the ActionScript Language Reference. I knew that most developers would go here first to learn about using workers, so I tried to give the class description enough content to help someone get started and find the important info they need.

Examples/Tutorials

Slides and code: Concurrent programming with ActionScript workers

Several weeks ago, I gave a presentation to the D-Flex users’ group about the new ActionScript workers features in Flash Player 11.4/AIR 3.4. Although I’m a few weeks behind on this, I wanted to share my slides and code.

Update Nov. 7 2012: On recommendation from Leif Wells, I realized it would be useful to share some links and tips on use cases and best practices for AS3 Workers.

A lot of the presentation involved talking through examples in the slides, but I also showed a “full” code example at the end.

If you’re interested you can view and download the example from GitHub. It’s a simple app that just counts from 0 to 100,000,000 in a loop. The counting happens behind the scenes – it’s not rendered on the screen – but it does have a progress bar and progress status text field that are updated as the counting happens:

Near the start of the presentation I showed a version of the app that does things the “wrong” way (on GitHub this is called the “NonWorkerVersion”). Since the code just counts in a loop, the code blocks in the loop and the UI freezes. (Depending on how busy your machine is and how high the number is, it can trigger a script timeout error.) Of course, the progress bar and the progress status text field don’t update either since they run in the same thread as the loop that’s counting.

This is the loop code, from the main class:

Counting in a loop (non-worker version) NonWorkerDemo.as on GitHub
1
2
3
4
5
6
7
8
9
10
while (i < targetValue)
{
    i++;
    // only send progress messages every so often
    // to avoid flooding the message channel
    if (i % oneHalfPercent == 0)
    {
        handleProgressMessage(i / onePercent);
    }
}

Toward the end of the presentation I showed the version that does things the right way.

First, the UI thread spawns a worker and sets up the main thread-to-worker communication, then it starts running:

Starting up the Worker WorkerDemo.as on GitHub
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bgWorker = WorkerDomain.current.createWorker(Workers.BackgroundWorker);

bgWorkerCommandChannel = Worker.current.createMessageChannel(bgWorker);
bgWorker.setSharedProperty("incomingCommandChannel", bgWorkerCommandChannel);

progressChannel = bgWorker.createMessageChannel(Worker.current);
progressChannel.addEventListener(Event.CHANNEL_MESSAGE, handleProgressMessage)
bgWorker.setSharedProperty("progressChannel", progressChannel);

resultChannel = bgWorker.createMessageChannel(Worker.current);
resultChannel.addEventListener(Event.CHANNEL_MESSAGE, handleResultMessage);
bgWorker.setSharedProperty("resultChannel", resultChannel);

bgWorker.addEventListener(Event.WORKER_STATE, handleBGWorkerStateChange);
bgWorker.start();

The worker contains the loop that counts. The worker is still just using a loop (and hence is running in a continuous thread without stopping the entire time). However, it dispatches events to the main thread/worker and it updates the display. This is the code for the worker’s counting loop (from “BackgroundWorker.as”):

Counting in a loop (Worker version) BackgroundWorker.as on GitHub
1
2
3
4
5
6
7
8
9
10
while (i < targetValue)
{
    i++;
    // only send progress messages every so often
    // to avoid flooding the message channel
    if (i % oneHalfPercent == 0)
    {
        progressChannel.send(i / onePercent);
    }
}

Even though this is just a simple example, it demonstrates how working with workers requires a bit of mind-bending and changing the way we think about coding in ActionScript. I realized as I started using workers that I’m so used to ActionScript’s single-threaded event-driven execution model that I make some assumptions sometimes about code execution. Once I started splitting my code into workers, I had to change my way of thinking.

On one hand, I realized that with the single-threaded model I subconciously made assumptions about certain things already having been processed or finished executing before other blocks of code were run. However, those assumptions aren’t necessarily true when the code is running in parallel, so I had to be more careful in planning my code to accomodate different states of completion at various milestones.

On the other hand, I was able to be more procedural and less event-driven (within my worker code) and just write all the code within a single method that was mostly a loop. This also requires a different way of thinking about the code. I’m so used to splitting code up into lots of event handlers that it’s weird to think that I can just run code in a loop and dispatch events, and know that they will actually be caught and run in the other thread even though the loop/method I’m in hasn’t returned yet.

Once the Worker apis are complete with the Mutex class and the Condition class, this will allow the potential for even more linear/procedural code. It will be possible to pause a thread’s execution, so you could conceivably write an entire worker within a single loop that never exits and just pauses execution until it’s needed. I know that’s normal in some other programming disciplines, but for an ActionScript programmer it’s a bit mind-bending.

…and, I’m back!

I’ve been silent on this blog for a long time. There are several reasons for this:

  • Technical (the admin part of my Wordpress installation wasn’t working)
  • Content (there weren’t any new Flash Builder releases so I didn’t have any more Flash Builder-related things to write)
  • Career (I’ve been transitioning to doing a lot more Interaction Design work, so I’ve been doing more learning than having new thoughts to share)
  • Time (I’ve been busy with work projects, and some of my outside-of-work volunteer activities have gotten much busier, and my kids are getting older and needing more time – so overall my available time for writing has been much more limited)

In any case, I didn’t mean for this post to be an excuse for my silence. In fact, the title of the post isn’t actually meant to refer to me being “back” to writing on my blog. (Although I do plan to be doing more of that.)

Two years and a couple of weeks ago, I wrote a nostalgia-filled post about leaving my job at Adobe, moving to Texas, and starting a new job as an AIR application developer. Working for Dedo Interactive has been a very rewarding experience. I’ve gotten to work with some great people, on some of the most interesting projects being done right now (in my opinion of course). My employers have been great about working with me to find opportunities to work on projects that match my interests.

In spite of all that, for a time I’ve been feeling that I’ve achieved the goals I had for myself when I joined Dedo Interactive, and so I’ve been looking for a chance to move in some different directions. Fortunately for me, an opportunity opened up at a company that I know is a great employer, and I was selected for that opportunity.

As of yesterday, I’m now officially an Adobe employee once again. My new job is in the developer relations group, specifically in the team that is in charge of developer-related content such as the Adobe Developer Connection. (I’m in a sibling team to Michelle Yaiser and Brian Rinaldi’s team, and a close cousin to the evangelist’s teams.)

My specific area of focus is on the developer documentation and other instructional content (e.g. code samples, example apps, etc.) for ActionScript. Part of my job is to be one of the people who create that content (write documentation and create code examples). Another part (this is where the “developer relations” comes in) is to work with and in the ActionScript developer community to get and keep a deep understanding of how developers work, then use that understanding to design what content we present and how we present it. The end goal is for Adobe to provide the best and most useful instructional resources for developers.

In part this is similar to what I did with Adobe before. However, since this job is now part of Developer Relations, there is more freedom and flexibility to make significant improvements, and more support for working with real developers in the community, which are some of the things that I really wanted to do but were more difficult to do in the past.

I’m excited for the possibilities that this position offers. I’m excited to be working for Adobe again – they really are a great employer. Most of all I’m excited that I get to continue to work with ActionScript and the great ActionScript developer community, and that it’s an official part of my employment.

Flash Builder 4.5’s “Flash Builder” templates

I’ve written several articles about Flash Builder 4.5’s code templates feature. However, I got a question by email today that made me realize another aspect of templates that’s potentially confusing.

Specifically, I was asked about the “Flash Builder” template category, and how to access those templates (since they don’t show up in the code hint list).

Unlike the other template types (ActionScript, MXML, and CSS), the Flash Builder templates aren’t templates you invoke directly via the code hint list. Instead, those templates give you a way to customize the code that Flash Builder generates when it creates methods, event handlers, and properties. Flash Builder uses these templates when you use Quick Assist to create a method or event handler, when you use the Generate Getter/Setter command (either from Quick Assist or from the menu), when you use the Override/Implement Methods menu item, or when you create a method override using the override code hint.

For example, by default several of these items have a comment in them that looks something like this:

// TODO Auto-generated method stub

I tolerated this for a while, but last week while preparing my Flash Builder coding productivity presentation I decided I was finally tired of deleting those comments every time, so I edited those templates to remove the comment.

Flash Builder 4.5 tip: easy method override

Here’s another trick I use quite often in Flash Builder 4.5. To be honest, this feature may have been in Flash Builder 4 and I just didn’t notice it, but I discovered it with Flash Builder 4.5 (and I’ve enhanced it with templates, which are definitely a FB 4.5 feature =).

If you want to override an inherited method (or get/set accessor):

  1. Type

    override public function<space>

    or

    override protected function<space>
  2. Flash Builder pops up a menu showing all the methods that can be overridden. This is also handy just to see what methods are available.

  3. Choose a method name from the code hint list.
  4. Flash Builder creates the method signature complete with parameters and a super.methodname() call.

However, being me, I like to save keystrokes as much as I can. I created a couple of templates to reduce the typing involved in this process:

  • opf” contains just the text override public function (including a trailing space)
  • oprf” contains just the text override protected function (including a trailing space)

Using these templates I just type “opf<space>” (or “oprf<space>”) and the code hint list appears. Then I type the first few characters of the name I want and I’m in business.

As a side note, if you want to customize the code that Flash Builder inserts when it generates the override method, it’s the template named “override method” in the Flash Builder templates category.

Coding Productivity Features in Flash Builder 4.5 recording, notes, etc.

Yesterday I had the privilege of giving a session as part of Adobe Developer Week 2011, on the topic of “Coding Productivity Features in Flash Builder 4.5.” It’s pretty obvious from my recent posts that this is a topic that is important to me =).

You can now view the session recording (requires Adobe ID).

Many of the features I talked about are the same ones I’ve discussed in recent posts about Flash Builder 4.5, so you’ll find more information in those articles, although the presentation takes a much more hands-on “watch how I really work in the real world” approach.

In addition to the posts, I talk about my Flash Builder templates several times in the presentation. I updated them a few times in the last week in preparation for the session, and the latest version is now available on my GitHub account:

Paul Robertson’s Flash Builder templates on GitHub

Finally, if you’re interested in Flash Builder productivity, you’ll want to check out these articles and links for additional information, especially useful keyboard shortcuts:

Notes from D-Flex “AIR SQLite for the real world”

Last week I presented to D-Flex (the Dallas Flex Users’ Group) on the topic of AIR SQLite for the real world. This is the description of the presentation:

Almost any real application needs to store data, and usually that means using a relational database. In the real world, building an application that uses a database is a complicated, even messy business. Complicated and messy mean slow and inefficient. This presentation is about the libraries, tools, techniques and workflow for developing AIR SQLite applications (learned through lots of trial and error) that help me work faster and better.

I won’t bother posting my slides because for the most part they were reused from my 360|Flex presentation ”AIR SQLite Optimization” (with some updating–mostly trimming content out).

I actually really enjoyed the presentation–I was comfortable enough with the material that I was pretty relaxed. That means I ended up sharing a lot of tips and bits of experience that aren’t reflected in the slides. Hopefully the recording will be available soon; when it is I’ll add a link here and I do encourage you to take some time to watch it if you want to learn some of the lessons I’ve learned about working with SQLite in AIR.

A big part of my presentation was describing (and showing) various libraries and tools that I’ve found useful in my AIR SQLite development. Rather than make everyone write them down, I promised to post them here (I’ve also included a couple that I didn’t mention but that are useful):

Flash Builder 4.5 Tip: Using “selection” templates

I’ve already written about how to use the code templates feature in Flash Builder 4.5. One less-obvious aspect of templates is that some templates behave differently if you have some characters or lines selected.

For example, suppose you have one or more lines of code you’ve entered, and now you realize you want them to be wrapped in an if statement.

First select the line(s) you want to wrap:

A line of code selected in the editor.

Next hit Ctrl+Space to trigger the code hint menu, then again to switch to the template code hints:

Open the template code hints.

Choose the template name with the mouse, by using the arrow keys, or by typing the name of the template. (Sometimes it looks like the characters you’re typing replace the selected code, but don’t worry–your selection will reappear once you choose the template.)

Type to choose the template.

Accept the code hint, and the template appears with your selection in its proper place:

The template is entered in the editor.

If you want to create your own templates that support selections, there are two template variables you can use. Use the ${line_selection} variable for one or more lines of code. For example, I created this template to wrap any lines of code in curly braces:

{
    ${line_selection}
}

This also works with MXML. For example, here is a template I created to wrap a block selection in an MXML tag:

<${tag_name}>
    ${line_selection}
</${tag_name}>

You can use the ${word_selection} variable in the same way if you want a template for surround a selection that is only part of a line of code. However, the ${word_selection} variable currently isn’t as useful as it could be, because templates with the ${word_selection} variable only show up in the code hints at the first character of a line.

Flash Builder 4.5 tip: Use code templates

One great feature in Flash Builder 4.5 is the Code Templates feature. I wrote about these a bit when the public beta came out last fall, but I wanted to share more about them.

The templates (also known as “snippets”) feature allows you to define chunks of code that you use repeatedly. The templates show up as code hints, so to access a template just hit Ctrl+Space (or another key that you’ve defined to trigger code hints) and you will see them in the list.

Templates are listed in the code hint menu.

Hit Ctrl+Space again to filter the code hints so that only template code hints show up.

Templates are listed by themselves if you hit Ctrl+space again.

Only the code hints that apply to the context where the cursor is positioned show up in the list–for example, if the cursor is inside a method, you won’t see the template for creating a function, but you will see the template for a for loop (“fori”).

Accept the code hint and the template is inserted:

Select the template and it is inserted in the code.

One of the best things about templates is they really are “templated” as their name suggests. You can define variables in templates, and they show up as outlined areas in the inserted code (as shown in the image above). (See my post on creating templates and using template variables for more on template variables.)

You can hit TAB to jump through the different variable positions. Usually there is also a tab location at the end of the template (sometimes it’s specified as a location within the template) where you can go when you’ve finished building it out.

Flash Builder 4.5 tip: Supercharge your code hints and code completion

I was a C# programmer for a while and I have to admit, I was spoiled by Visual Studio. Some people don’t like lots of code assistance. I’m not one of those people. Since I moved to mostly ActionScript coding I’ve been hunting for an editor that comes close to the features Visual Studio has, specifically with regards to code hints and code completion.

FlashDevelop is based (conceptually) on Visual Studio (it’s actually based on SharpDevelop, which is modeled after the .NET world) so not surprisingly FlashDevelop has great code hint features. Unfortunately, since I moved to a Mac I haven’t been able to use FlashDevelop. (I’ve tried, but it’s just way too slow to run it in a VM.)

With that in mind, for the Flash Builder 4.5 release I filed several feature requests to hopefully get code hinting and code completion on par with FlashDevelop. Sadly it’s not all the way there yet, but happily they accepted and implemented some of my feature requests (and others are in the “under consideration” bin).

Here’s what’s available today, and at the end I’ll talk about what I hope comes in the future.

Accept code hints with space and other characters

Problem 1: In Flash Builder 4, when you have a code hint and you get to the item you want, you have to hit Enter to accept it. When I’m trying to code quickly, I feel like I’m hitting Enter every third or fourth character. Ugh.

But not anymore! New in Flash Builder 4.5, you can specify a set of characters that serve as keys to “accept” code hints. When a code hint is showing and you hit one of those keys, the code hint is entered and the character is entered as well. So (for example) you can type a few characters from a variable name and hit “.” and the variable name (and the period) are added to the code:

Flash Builder 4.5 Code Completion with custom characters from Paul Robertson on Vimeo.

However, this feature is turned off by default, so you have to turn it on to make it work.

Go to Flash Builder Preferences > Flash Builder > Editors > ActionScript Code. In the bottom of the preferences pane you’ll see a section titled “Code completion”:

Code completion preferences pane

I believe the trigger keys are empty by default, but you can see the set of keys I specify in the screenshot. It’s pretty much every character that is legal in ActionScript but isn’t legal as part of an identifier name. If you want to copy and paste them, here they are:

{}[]().,:;+-*/%&|^!~=<>?#

Note that I also check the box to have the space bar accept a code hint.

I used to include the single and double quote characters but I found that they triggered more false positives than anything, and I couldn’t think of a situation where I’d be typing a variable or method name and the next character would be a quote character (without a space or operator in between). So I removed them, and I’m happy with that choice.

Make code hints appear automatically

Problem 2: You don’t have to hit Enter to accept code hints, but you still have to hit Ctrl+Space every fourth character to open the code hint menu in the first place.

Once again, this is now a problem of the past. Flash Builder 4.5 lets you specify a set of characters that automatically make the code hint menu appear when you type them.

Flash Builder 4.5 Code Assist triggers from Paul Robertson on Vimeo.

In Flash Builder 4.5 Preferences, choose Flash Builder > Editors:

Code assist preferences pane

In the middle of the panel there is a section titled “Code assist.” First of all, to enable this feature you need to check the box “Enable auto-activation of code and template proposals.”

I like my code hints to appear quickly, so as you can see I change the “activate after” time to 10 milliseconds.

Next you need to check the “Use additional custom triggers” checkbox. This is what turns on the magic. By default the set of lowercase letters is in the box. Because I prefix private members with an underscore (_) I add that character to the set as you can see in the screenshot.

As a side note, this set of characters is almost the exact opposite of the characters I specified to accept code hints–in this case it’s all the characters (except “$” and numbers) that are legal in identifiers.

And now for the bad news

Unfortunately, these two settings are great on their own but when you combine them you run into a bit of trouble.

Specifically, since ActionScript keywords like var, new, const, function etc. contain letters, any time you start typing them they will trigger the code hint menu. That’s okay in general, most of the keywords they are unique and once you type enough characters there is no matching code hint so the menu closes.

Unfortunately, some of the keywords do have matching items in code hints. For example, the keyword function is spelled the same as the Function class, so when you type the word “function” the “Function” entry in the code hint is selected. Then, when you get to the end of the word and hit <space>, it’s a trigger to accept the suggestion so “function” gets replaced with “Function.” Ugh.

Function/function is one I run into a lot. Another one I hit a lot is new triggering the NewObjectSampler class. NewObjectSampler? What’s that? Its a class in the flash.sampler package that I’ve never once used, but since it’s a built-in class it’s always available to code hints. (Maybe this is just Flash Builder’s way of telling me that I need to use more dependency injection and factory patterns instead of using new. =)

Depending on what libraries you have imported, other keywords can trigger code hints as well. This is especially troublesome when you’re using libraries that are built around reflection (since they tend to have classes that are named for language features). I run into problems when using FlexUnit and Mockolate, for example.

So, are these new code assist and code completion features useless? Not at all; they’re still very cool, and hopefully in the next release they’ll be perfected. In the mean time there are a few workarounds, although I admit that none of them are ideal for me:

  • Use auto-trigger or auto-accept but not both: If your code hints aren’t auto-triggered, keywords won’t trigger the code hint menu. But you’ll be back to typing Ctrl+Space a lot. If you only accept code hints with Enter, when you finish typing “function” and type space the code hint will be ignored instead of accepted. But then you’re back to typing Enter all the time.
  • Only auto-trigger with underscore (_): If you are like me and you use the underscore (_) character as a prefix for member variables (or something like that) then you actually use underscore a lot. I’ve tried having the auto-trigger behavior turned on but only using underscore to trigger the behavior. This works pretty well, but I still end up having to use Ctrl+Enter more than I’d like.
  • Use Esc to cancel code hints: This is part of the compromise I’ve settled on. Whenever I type “function” I hit Esc at the end of the word, which closes the code hint menu. I can then type space without auto-accepting the code hint.
  • Use templates: I also do this, though it’s somewhat awkward because it requires you to change your typing habits. Here’s an example: create a template named “fn” (short for “function”). In the body of the template enter “function ” (note the space after the word function). Now when you type “fn<space>” the word “function” (followed by a space) is entered into the editor, assuming you don’t have a class or something else that starts with “fn.”

    (Why do you need the space in the template? Unlike other code hints, when you accept a template code hint with a character, that character isn’t entered into the editor. That makes sense because most templates have variables, so if the character you typed to accept the code hint was entered (as it is with other code hints) you would end up with the character in a variable position or something weird like that.)

    You can also create a set of templates with abbreviated codes for different types of functions, variables, etc. This is actually handy to do anyway. For example, I have templates named “prv” for private var [name]:[Type] and “pf” for public function [name]([arguments]):[Type] {}.

    However, overall this solution is hard to get used to because you have to remember to type “fn” or some other key combination instead of “function” (and “nw” instead of “new,” etc.) or “prv” and “prf” etc. Unfortunately I have too many years of muscle memory going against me to make this a completely workable solution.

Moving forward

These are unfortunate issues, but the Flash Builder team is aware of them and they’ve told me that they’re anxious to improve things for the next version, which is a good thing.

If you’re interested in having great(er) code hints in Flash Builder, consider voting for these feature requests so we can “encourage” the Flash Builder team to move forward more quickly on these issues: