FX Experience Has Gone Read-Only

I've been maintaining FX Experience for a really long time now, and I love hearing from people who enjoy my weekly links roundup. One thing I've noticed recently is that maintaining two sites (FX Experience and JonathanGiles.net) takes more time than ideal, and splits the audience up. Therefore, FX Experience will become read-only for new blog posts, but weekly posts will continue to be published on JonathanGiles.net. If you follow @FXExperience on Twitter, I suggest you also follow @JonathanGiles. This is not the end - just a consolidation of my online presence to make my life a little easier!

tl;dr: Follow me on Twitter and check for the latest news on JonathanGiles.net.

Sequences Performance Tip

Sure, there are plenty of tips discussing how to use sequences in JavaFX, but the one I wanted to cover quickly today is regarding the fact that sequences are immutable. That is, once you create them, their content can’t change.

You lie!‘ I hear you all shout, running off to your IDE’s to show me the following fully legal and compiler-friendly code:

[jfx]
var seq = [ 1, 2, 3 ];
insert 4 into seq;
insert 5 into seq;
delete 1 from seq;

println(seq);
[/jfx]

If you run this code, the output will be [ 2, 3, 4, 5 ], as expected. It’s understandable if you think the sequence is being updated in-place – it certainly looks that way from where we’re all standing – except that under the hood it isn’t, and there are performance implications we need to be clear on. To get straight to the point, the insert and delete instructions create a brand new sequence instance, but fortunately they also ensure that the seq variable always refers to the new instance, quietly leaving the old instance to get garbage collected.

Of course, as I just noted, from a developers perspective we can treat seq as a reference to a sequence instance and be oblivious to these ‘behind-the-scenes’ sequence shenanigans. There is nothing wrong with this, but it’s important to know that creating sequences isn’t free, and certainly minimising the amount of sequences we create is a smart idea.

To be slightly more technical, I should note that there are some exceptions to this ‘sequences are immutable’ rule when dealing with non-shared sequences of specific types, but I don’t want to muddy the point of this blog, which is….

Use the expression language features of JavaFX to reduce the number of sequence instances you need to create.

For example, you might have code that looks like the following:

[jfx]
var seq:Integer[];

for (i in [1..MAX]) {
insert i*i into seq;
}
[/jfx]

I hope, with this blog post firmly at the forefront of your mind, you can now see why perhaps this isn’t such a great idea: we’re creating MAX sequence instances, one for each iteration of the for loop – ouch. Importantly, this example isn’t far-fetched and made to prove a point: I just found a very similar example in my code the other day (which I promptly fixed up), and even found a few blog posts on this site and other sites that use this approach. What you actually should do is something like the following:

[jfx]
var seq:Integer[];

seq = for (i in [1..MAX]) {
i*i
}
[/jfx]

In this case rather than create MAX instances of the sequence, we’re creating only one – the result of the for expression. This is considerably more efficient, and can lead to huge performance gains, depending on what you’re doing. In my case it made the difference between my code being usable and being jittery. Obviously in my case this was a very critical for loop, and it may be similar in your use cases also.

I hope that this helps you eek out a little more performance from your apps. Now, go forth and optimise your sequences ๐Ÿ™‚

JavaFX links of the week, July 5

Wow, July already. Time sure is flying these days. As always, please feel free to flick me an email or a tweet to let me know of any news you want included. Let’s get on with the news!

That’s it for another week folks. Catch you in a weeks time.

JavaFX links of the week, June 28

A fairly quiet week in the JavaFX world this week, but nevermind, I’m sure it’s just because the majority of you are too busy working on your blog masterpieces for next week ๐Ÿ™‚ Regardless, there are some very useful links in this weeks post, so have at it, and as always please do flick me an email (or twitter message) with any link you think is worthy of being included.

That’s all folks. Thanks to everyone who took the time to post about what they’re working on, and for those of you who didn’t, I’m looking forward to your blogging masterpieces next week ๐Ÿ™‚

Custom Cell Caching

Custom Cell Caching

Ok, I know we’ve been going on about custom cells / cell factories a bit recently, but I wanted to do one more post about a very useful topic: caching within cell content.

These days ‘Hello World’ has been replaced by building a Twitter client, so I’ve decided to frame this topic in terms of building a Twitter client. Because I don’t actually care about the whole web service side of thing, I’ve neglected to implement the whole ‘real data’ / web services aspect of it. If you want to see an actual running implementation with real data, have a look at William Antรดnio’s Twitter client, which is using this ListCell implementation.

(more…)

Three Useful JavaFX Links

Recently a few useful documents turned up on the web related to JavaFX, so I thought I’d briefly provide the links to them in case you weren’t aware of their existence.

Firstly, the JavaFX team updated the JavaFX 1.3 API documentation. There is a bunch of new content for much of the API, and improved package documentation (which you can see by clicking the arrow on the main page relevant to the package you’re interested in). In particular, the javafx.animation, javafx.scene, javafx.scene.control and javafx.scene.chart packages have a bunch of useful information.

Secondly, with the huge improvements to CSS support in JavaFX 1.3, we’ve published a comprehensive CSS guide. This is a huge resource to people interested in styling JavaFX user interfaces, as it lists all available css properties for each control.

Finally, for those interested in ensuring your coding style is following convention, the best resource is Tor Norbye’s JavaFX coding conventions document. It’s not the definitive style guide for JavaFX, but it is a great starting point. I think there is only one difference between his conventions and what we use internally, and he notes this in the page.

There we go – a few very useful resources that you may be interested in perusing in a spare moment.