Performance Improvements for 1.0

By Matt Matt Permalink

Optimization requires taking a step back to observe, measure, and experiment. Somehow it seems like a luxury, and features or bug fixes are always the priority. This release though, thanks to the survey and GetSatisfaction feedback you’ve been sending, speed was a primary focus.

dbradley, pvh, jacklu, kreeger and myself had a lot of fun working on performance problems that had been annoying us. Our goal was to improve basic every-day library management, and the results are a huge step forward.

Scrolling is much much smoother, searching is thousands of times faster, databases are 60% smaller, and importing is nearly twice as fast and many times more efficient. Songbird is much more pleasant to use, and can now easily handle 20,000+ track libraries. In fact, it seems to scale about as well as other popular players.

OBLIGATORY INFOPORN

This is (mostly) the output from a new performance lab that marcc and toks set up. Every night we get performance reports for the library API, allowing us to verify progress and avoid regressions. Don’t worry about the times, these are stress tests, not actual user use-cases. The lower the time, the better the performance.

Read on if you’re interested in how these improvements were made.

SCROLLING

The main library view is a XUL Tree widget, just like the bookmarks in Firefox and mail view in Thunderbird, so we were concerned that Songbird used nearly twice as much CPU and scrolled much more slowly than these applications. Simply moving the mouse in a circle over the tree would consume up to 80% CPU.

Profiling with Shark showed a lot of wasted effort reading item metadata and firing events while painting the tree. A little work to adjust how the selection is maintained and refactor the caching code significantly improved library scrolling.

Next was a paging hiccup every 1000 items when scrolling large libraries. Logging and timing queries showed that secondary sorting was to blame (first sort by artist, then by album, then by disc number and track number). Getting sorted media item IDs for 1000 rows could require hundreds of queries, each with a cost proportional to the library size.

Since secondary sorting is not user configurable we now pre-compute another sort value for each property. For example, when album metadata for a track changes, the secondary sort value for the artist property is rebuilt and indexed, making sorting by artist, album, disc, track number essentially free.

SEARCH + FILTERING

In 0.7 we moved to SQLite Full Text Search (FTS3), and found that in most cases it was extremely fast. Unfortunately though search time grew exponentially with the number of match results, and searching for “a” in large library could hang for up to 30 seconds. Lame.

Half of the problem was the secondary sorting issue that had been slowing down scrolling. Each query had to include the search and filter constraints, so a slight cost increase would be multiplied by 50 to 100x. Removing this multiplier helped a great deal.

The other half turned out to be the way we were joining the FTS3 table to the properties. Query optimization is mysterious, so testing and measurement are critical. Query logging, our performance test harness, and the SQLite ‘EXPLAIN QUERY PLAN’ command helped us determine that joining the FTS table, then the properties, then the media items is an order of magnitude faster than equivalent queries.

Finally, since library filter lists only show a small number of values, we now fetch all rows at once, removing the need for a separate count query of approximately the same cost.

Lessons learned:

  • Table joining order is extremely important. As a rule, join the most restricting or most expensive to join table first. Whatever you do, test a few permutations.
  • Joining with virtual and temporary tables prevents the use of an index for both joining and sorting. ‘EXPLAIN QUERY PLAN’ shows that an index can still help with a join, but sorting has to be done the hard way.
  • Occasionally the query optimizer will make bad decisions, and forcing it to use a specific index can help quite a bit. Placing the unary plus operator (+) in front of a column name indicates that an index should not be used.

Recommended links:

ADDING TRACKS

Importing large libraries was slow, and required an obscene amount of memory. With 1.0 it still isn’t ideal, but it’s fast, and the memory usage is 10% of what it was in 0.7. Some of the improvement came from the database changes described below, but significant progress was made simply by avoiding unnecessary work.

The first pass removed unnecessary code paths. 0.7 computed an MD5 hash for every track, doubling IO costs, and would frequently loop over and copy the list of tracks. This behaviour was redundant, and required a huge amount of memory when importing 50k+ tracks.

The second pass cleaned up library change listeners. Tracing with the DTrace JavaScript probes and profiling with Venkman revealed that the smart playlist system and album art scanner ignored batching hints. Batch notification was added after the initial listener API was created, and for backwards compatibility it is opt-in, making it easy to forget that a single JavaScript listener called 50,000 times can hang the UI. The listeners were fixed, but we need an API that is easier to use correctly.

DATABASE ENGINE

Experimenting with BloatView and Instruments, we established that the large library memory problems in 0.7 were mostly caused by our database engine.

Any time Songbird operates on a large set of media items (adding, deleting, enumerating) it builds multiple queries per item, enqueues them, and then executes them. In 0.7 these queries were constructed as strings, which, multiplied by a library of 50k+ tracks, could require hundreds of megabytes.

The problem was solved by adding prepared statement support to the database engine and library code. Now a single compiled query instance can be used with a large set of parameters, and the number of media items involved is barely a factor.

Discoveries along the way:

  • NS_ProxyRelease should be treated with caution, since it can cause objects to pile up.
  • A string query or prepared statement with a “column IN (?, ?, …)” clause is much faster than an equivalent set of “column = ?” prepared statements.
  • The SQLite ‘ANALYZE’ command doesn’t need to be run all the time. Dump values from a typical database and include hardcoded results with the schema.

DATABASE SIZE

The easiest way to improve database performance is to just have less data. Seems obvious, but it’s easy to overlook.

In 0.7, a 10,000 track library required a 50MB database, of which 60% went to storing indices (according to the sqlite analyzer). Indices store a complete copy of the indexed data, and are expensive to maintain. They are also difficult to remove, as you have to be sure you wont hurt performance. Using ‘EXPLAIN QUERY PLAN’ to figure out which indices were used, and the performance test suite to avoid regressions, we dropped three indices for an overall size reduction of 40%.

We also noticed that many values were stored as UTF-16, despite never containing Unicode (since our generic property schema stores everything as strings). With a little experimentation we discovered that using UTF-8 databases did not impact performance, even for large libraries of entirely Unicode metadata.

Switching to UTF-8 gave another 40% reduction, and an average 10,000 track library in 1.0 is now 18MB.

UP NEXT

Where are the new pain points? If you’ve been using the 1.0 RC builds please let us know how things are working, and where you think we should focus. Songbird can never be “too fast” or “too lean” and we’re committed to improving performance in every release going forward.

Both comments and trackbacks are currently closed.

8 Trackbacks

  1. [...] That May Also be of interest ImprovementsSongbird Blog » Performance Improvements for 1.0Songbird Blog » Performance Improvements for 1.0Tuscany Energy Ltd. reports significant improvements in financial …Google Chrome Bookmark [...]

  2. [...] made some substantial gains this [...]

  3. By Songbird 1.0.0 - Clunk's Forums. Dec 2, 2008 3:24 pm

    [...] others need ironing out, are experimental, or are just plain missing. There’s still a lot to do. Songbird Blog Performance Improvements for 1.0 * Drastically Faster Search: Searching in Songbird is now anywhere from 10x faster for small [...]

  4. [...] so is the cover flow.Its the biggest drawback of Songbird.This is expected to be resolved in future(check for updates)Whereas amarok never has any such [...]

  5. By Songbird 1.0 a review « Whitenoise Dec 14, 2008 9:52 pm

    [...] most of them. The application starts with a 3 column library view.The indexing of songs was pretty fast. My 12G song library got indexed within 3 minutes!  Songbird1.0 uses GStreamer,a cross platform  [...]

  6. [...] 也許畢竟功能龐大,還是滿吃記憶體的。雖然 v1.0 比起舊版,能感覺出整體效能有明顯地提昇(Performance Improvements for 1.0),不過這對於沒用過舊版的人來說,沒什麼感覺吧… [...]

  7. [...] 也許畢竟功能龐大,還是滿吃記憶體的。雖然 v1.0 比起舊版,能感覺出整體效能有明顯地提昇(Performance Improvements for 1.0),不過這對於沒用過舊版的人來說,沒什麼感覺吧… [...]

  8. [...] Jusque-là, je n’avais jamais vraiment utilisé Songbird : trop lourd, interface pas très agréable. Bref, je ne lui ai pas vraiment laissé sa chance. Mais cette nouvelle version est d’un tout autre niveau. La nouvelle interface est de loin plus soignée, parfaitement intégrée à l’interface graphique MacOS et surtout, l’équipe de Songbird a énormément travaillé sur les performances. [...]

42 Comments

Subscribe
  1. mmhm Nov 26, 2008 6:13 pm Permalink

    And yet I still can’t double click a playlist file or mp3 and have it open in songbird. I have to go through the file menu. Why? Anyone?

  2. dude Nov 26, 2008 6:20 pm Permalink

    what is with the 9 mini graphs? is it supposed to be a joke? Seriously what the heck happened.

  3. matt Nov 26, 2008 6:34 pm Permalink

    mmhm: We don’t try to take over your computer just yet. You’ll have to right click on the files and tell your OS to associate them Songbird by default.

    dude: half-joke, since the scale and details are mostly irrelevant. They are performance tests, so the less time they take the better. Down is good.

  4. Manuel Nov 26, 2008 6:45 pm Permalink

    Wow! Great work!!
    Thanks for sharing this.
    I’m also looking forward for the third article of ‘Path to agility” =)

  5. atreiu Nov 26, 2008 6:51 pm Permalink

    VERY nice improvements! And it’s good to hear, that you are still looking for new ways to improve performance even more. :-) Here are my ideas:

    I myself hadn’t big issues with library performance, cause I’m under 5000 songs, but I would appreciate it, if you could speed up the UI by:
    * make Songbird start faster
    * make switching to miniplayer and back a lot faster!
    * make switching feathers faster (so the time i see glitches will become shorter.)
    * make Songbird play a song faster (first song after start needs about 1sec for me to start playing)
    (sorted by my priority ;-)

  6. Nazgulled Nov 26, 2008 7:54 pm Permalink

    Are these performance improvements supposed to be on the RC’s or should we wait for 1.0 final?

    I’m asking because this post and by the graphics, it looks like a library like mine, with less than 4000 tracks, Songbird should be quite fast, but it doesn’t feel like it…

    Most of my problems are the same as the ones atreiu described:
    - Songbird takes a lot of time to start compared to Winamp (the player I’v been using in the past years, Winamp ist not very fast but it’s faster than Songbird, for now…)
    - Songbird takes alike 2secs (minimum) to start playing the first song, this is a bit annoying…
    - The library scroll is not very smooth, it lags quite a bit and I have like about 3700 tracks.

    Regarging performance, this is what bothers me the most with no specified order.

  7. bent Nov 26, 2008 8:50 pm Permalink

    Awesome post, Matt. Congrats on all the big wins!

  8. gnardawg Nov 26, 2008 8:58 pm Permalink

    Songbird is shaping along nicely!
    Won’t even play songs in RC3 though, just crashed on my Kubuntu 8.10 box.

    Songbird really needs some video function. The old builds opened a video in another window, and only supported a few codecs.
    It would be cool if it could implement a video library the same way WMP11 and iTunes does. This would make it a great all around Multimedia Management application.
    Also some advanced features like an equalizer would be good.

  9. Vivek Nov 26, 2008 10:39 pm Permalink

    Still no answer to the huge RAM leakage which is present even in the RC’s. It is this huge leakage that kept me away from songbird 0.5-0.7. 1.0RC’s fix them to some extent but there is a lot to be desired in this field. I really don’t know when songbird will be on par with winamp or even for that matter wmp :(

  10. Anonymous Nov 26, 2008 11:09 pm Permalink

    Almost all the player that I used come with equalizer and visualizer like Winamp, foobar2000, Amarok but Songbird none.

  11. atreiu Nov 26, 2008 11:16 pm Permalink

    Oh, i forget one thing: It would be nice if you could improve the playback-performance: Currently, if i have heavy load on my computer, the playback stutters. I would prefer it, If songbird could take his part of CPU-Power to ensure clean Playback.

    @Vivek, Nazgulled: Comparing the performance of Songbird to Winamp seems like comparing OpenOffice to Word-Pad. The feature are just too different. It sure would be nice to have a better Memory-Management, but I wouldn’t expect Songbird to consume as low memory as Winamp or Foobar or something…

  12. atreiu Nov 26, 2008 11:19 pm Permalink

    @Anonymous: Join the club here: http://getsatisfaction.com/songbird/topics/eq_and_applet I hope they will speed up development of this, the more people subscribed there… ;-)

  13. Clamm Nov 27, 2008 12:45 am Permalink

    @matt

    “[...]We don’t try to take over your computer just yet. You’ll have to right click on the files and tell your OS to associate them Songbird by default.[...]”

    Thats not the problem… If you associated a filetype to Songbird by default Songbird opens up after clicking on such a file BUT isnt able to play it. You can only play files you added to the library but not right away from the file system (that makes Songbird as the default player useless)

    There’s already a bug report and an entry at getsatisfaction:
    http://bugzilla.songbirdnest.com/show_bug.cgi?id=13895
    http://getsatisfaction.com/songbird/topics/how_to_get_songbird_to_play_a_song_when_clicking_on_music_file

  14. Matthew Wilcoxson Nov 27, 2008 1:54 am Permalink

    Sounds great. (I secretly enjoy coding performance improvements…!)

    So are these improvements in the current nightly build??

  15. John Smith Nov 27, 2008 1:54 am Permalink

    Regarding speed: I use Songbird 1.0 RC3 on both Windows XP and OSX 10.5 machines and have noticed that Songbird feels overall more responsive/less sluggish on the Windows machine. Like atreiu above playback-performance suffers if I have “heavy” load on my Windows machine (on the mac this has never been a problem).

    Unrelated to the speed issues: Something that has been bothering me from the very beginning is that I can’t change the title of a radio stream in the library like I can for example in iTunes. It always says “Metadata for the selected item cannot be edited”. Is there a reason for this behavior?

  16. Nazgulled Nov 27, 2008 4:40 am Permalink

    I couldn’t care less about how much memory it consumes as long as it doesn’t make my system slow when I’m listening to music on Songbird and working on several different applications. So far, I’ve noticed that Songbird uses quite a big chunck of memory compared to others, but it doesn’t bother me for now because the system is not slow.

    However, I wonder why can’t we expect Songbird to be on par with Winamp… What abou performance? Do you say we shouldn’t be expecting Songbird to be as fast as Winamp?

    How do you expect people to use A over B if A is more sluggish than B? Yes, Winamp has years of development, but Winamp is much more fully featured than Songbird right now. Why can’t a smaller application with less features be more reponsive/faster? If you say we shouldn’t expect better, what will happen when Songbird becomes a fully featured media player just like any other on the market? Will it be even slower?

  17. Mazza Nov 27, 2008 5:32 am Permalink

    Como on guys, stop stress our beloved programmers! If you prefer the Winamp functions.. well, go and use it! Songbird is not supposed to be a Winamp clone, neither a iTunes or Amarok one. It supposed to be something more. I follow the development of this program since the 0.2.x and it improved a lot. You won’t never have a browser on Winamp nor a huge list of useful plugins on iTunes.
    Songbird was born to provide a surrounding web-music experience.. and I have to admit that I love it. A lot of work has done so far but there’s space for a lot of new improvements (podcast, video, better performance and so on).. just have faith and help in any way you can!
    I love Songbird, keep on working this way!

  18. Nazgulled Nov 27, 2008 6:15 am Permalink

    Nobody is talking about features that one has and the other don’t. We are talking about performance, that’s what this post is about. If songbird was lacking any feature from Winamp that I needed, I wouldn’t be here…

    Blog post quote:
    “If you’ve been using the 1.0 RC builds please let us know how things are working(…)”

    That’s what I’m doing…

    This post is about performance issues with Songbird, for me, it takes a lot to start the player and to play the first song and the library scrolling is very sluggish. That’s my take on the performance, other than that, it’s all good.

  19. Horfic Nov 27, 2008 6:31 am Permalink

    I would love to see a export feature for the library, or a sync feature, or something like that. Because I’m using some computers with songbird and i hate to sync every single thing always manual.

    Friendly Regards
    Horfic

  20. Clamm Nov 27, 2008 7:27 am Permalink

    @ John Smith
    Editing Metadata of streams is also on getSatisfaction whishlist… To raise attention to this task go here and click the “I have this problem too” button ;-)

    http://getsatisfaction.com/songbird/topics/editing_of_metadata_for_streams?from=new_topic&utm_medium=topic_search&utm_source=topic_search_songbird

  21. DanielArgentina Nov 27, 2008 9:32 am Permalink

    awesome!! good work!

  22. LIB53 Nov 27, 2008 10:46 am Permalink

    That’s all pretty sweet, but does that means improved RAM usage too?

  23. katana346 Nov 27, 2008 10:58 am Permalink

    Video playback would be nice…. it will play my video files but only the sound. then for some reason, upon opening songbird one day, a separate “video” window opened, so, excited to see if this was the fabled video playback function, I played some of my video files… but to no avail. Sound only.. the video window simply watched in mocking contempt.

    RAM usage is still pretty high, I agree with the others. Songbird uses nearly three time as much memory as WMP. But thats alright. Still lovin’ the bird.

    I found SB around the .2 release because it was the only alternative to Itunes of my old mac, and at that time it was so buggy i would often get too frustrated and go back to itunes. Soon after things got better and it was stable enough to use as a main player. then i got a vista machine and stuck with SB cause it has alot of promise. Glad to see where the team has taken it.

  24. Cato Nov 27, 2008 2:36 pm Permalink

    Please add a video library, guys! If you get a video library and proper video playback, that will be the day i delete VLC.

    katana346: I have the same problem. I’ve never been able to play anything properly. Sometimes I get sound only, though.

  25. RomeoJava Nov 27, 2008 3:20 pm Permalink

    The performance of the Library has improved dramatically in the last couple of months which is fantastic! Therefore, strictly from a performance perspective, the two areas I feel need to be worked on most are:

    1) Songbird startup time
    2) Time to start tracks playing

    Thanks for all the hard work you have put in!

    Rj

  26. b0b Nov 28, 2008 12:05 pm Permalink

    Scrolling in a large playlists is still very slow, this is my major annoyance. For example in foobar2000 with several thousands of item it’s silk smooth, but it’s to be expected since standard win32 list widget is used.

    Also, for future performance work I would concentrate on reducing or eliminating visible redraw when switching from a component to another: it makes the app feel sluggish.

    Otherwise thanks for the detailed explanations, it was really useful as I’m myself using sqlite for development.

  27. The Flying Jaco Nov 28, 2008 8:30 pm Permalink

    I have to congrat you guys on what you’ve accomplished already. Songbird already takes up less RAM on my system than iTunes. Anyone out there have a nightly for PPC Mac? I would love to sink my teeth into that (otherwise, I’ll wait like a good little boy for the official release of 1.0).

  28. Odysseus Nov 29, 2008 6:12 am Permalink

    One feature I would like to see, is that of continually monitoring your music library for changes. I don’t like having to ‘import’ new music I’ve purchased from Amazon. I would like to see Songbird just monitor my Music folder for any changes and then add or remove them from the library.

    BTW, I’m using 1.0RC3.

    Thanks.

  29. sebastian Nov 29, 2008 7:38 am Permalink

    how about at least two more RCs before you go final? better safe than sorry. I think I speak on behalf of all your fans if I say we’d rather wait for awesome stuff than get not-quite-yet-awesome stuff right now ;-)

    keep up the great work!

  30. hachel Nov 29, 2008 1:13 pm Permalink

    With every new release I hope that this problem has been fixed: http://getsatisfaction.com/songbird/topics/downloads_fail_at_the_last_instant_with_podcast

    Unfortunately RC3 still has that problem.
    Anyone here runnign Ubuntu able to download podcasts?

  31. Sergey Nov 30, 2008 3:56 am Permalink

    I don’t care about memory usage by Songbird, but i have a laptop and Songbird RC(1-2-3) use so much processor time. More then 30% on my Celeron M 1.8

    It’s so big for me and another players (don’t Amarok 2 :) use about 10-15%

  32. matt Dec 1, 2008 11:35 am Permalink

    This is great! Thanks for your feedback and your support.

    So, critical problems in order of priority:
    1. RAM usage
    2. Start time
    3. Playback start time
    4. Playback CPU usage
    5. Further scrolling, filtering improvements

    We won’t be able to address this list for 1.0, but it should be looking much better by 1.1. MikeS and Aus are on top of the playback problems (all gstreamer-on-new-platforms pain), and I’ll be doing RAM and startup research later this week.

    @Clamm, re file associations, OH yeah, that is a huge bug. That should have been working since 0.5-ish. Thanks.

    @John Smith, re stream metadata, that’s actually an explicit policy. At some point we decided that you shouldn’t be able to edit metadata for read only/remote tracks, since there was no way to save the changes outside of the database. Am not sure I agree with this though, and would support adding a preference to allow editing everything in the library.

    @Odysseus: Kreeger is working exactly that feature for the next release. See http://wiki.songbirdnest.com/Releases/Hendrix/Watch_Folders. It seems to get cut half way through every release though, so will see.

    @LIB53, re RAM usage, for this release we focused on speed and large library support. We did improve memory usage quite a bit, but only in specific areas such as adding tracks to the library.

  33. dan Dec 1, 2008 2:10 pm Permalink

    Thanks for the updates, matt; I’m looking forward to the next Songbird version, especially for the playback start time fix. Other than that, I’m very satisfied with Songbird, and appreciate what you guys are doing.

    Thanks once again.

  34. deOmega Dec 1, 2008 6:49 pm Permalink

    I keep forgetting to mention..
    I just did a fresh songbird install, and I am back to a problem I have had for what seems like forever.

    Right after adding my library to SOngbird on first run, there are songs that have no track time next to them. Until recently, all i would have to do is start playing each track and the time (song’s length) would show up… but now, not even by playing them does the time come up. SO a lot of songs in my library right now, have no indication of how long the song plays………..though that information does show up by play indication slider.

  35. Bantam Dec 2, 2008 1:39 am Permalink

    I’m having the same problem as deOmaga. I too did a fresh install and I have several songs that have no track times next to them, however the info shows up the the play indication slider. As anxious as I am to see a 1.0 release, I’d be more than happy to see a 0.8 and a 0.9 first in order to see these sorts of things ironed out first.

  36. deOmega Dec 2, 2008 6:24 am Permalink

    oh, and another thing… Seeing that I just did a fresh install…seems like all the missing album artwork i have right now, have been replaced with Sly and the Family Stone Greatest hits.

    I have ONE song by them, but hundreds of album art by them for some reason..

    I appreciate the hard work that you guys are putting forth, but there really seems to be a lot of fundamental bugs introduced with this RC3,

  37. matt Dec 2, 2008 9:12 am Permalink

    @deOmega: Do you have a lot of tracks together in one folder? The album art finder looks for “Album.jpg” and that sort of thing beside tracks, so if you dump a whole bunch of albums in one directory, along with a single art file, you will get the same art for all the tracks.

    @Bantam: Meeee tooooo. We’re committed at this point though, and have accepted that we might as well just get 1.0 over with.

    As for the length bug, can you please comment and attach a file to this bug
    http://bugzilla.songbirdnest.com/show_bug.cgi?id=13254

    Thanks.

  38. deOmega Dec 2, 2008 5:08 pm Permalink

    @mat
    Thank you for your feedback, and I must say that now I have a thorough understanding of what the problem is all about, I hope it is possible for you guys to implement that 100 limit per folder… sooner, than later.., seeing that all my songs are in a single folder. many thanks…

  39. Djrocks Dec 3, 2008 4:36 pm Permalink

    I notice a huge improvement on the Linux version, it now runs smooth as water. I love it.

  40. Jordan Dec 4, 2008 1:19 pm Permalink

    I’m so happy performance is a priority. The idea of iTunes+firefox in a single sprawling executable often worries me….

  41. Ju2999 Dec 5, 2008 5:25 am Permalink

    Songbird 1.0 is so unbelievable slow on my iMac core2duo 2,8. Please optimize this software, especially the gui speed. I thought iTunes is slow, now Songbird is even slower!

  42. Suopey Dec 8, 2008 2:23 pm Permalink

    I don’t know if this was mentioned because I’m lazy, but could you guys focus on a toolbar mini player for when I’m working on another document

    If anyone thinks they can make an add on for a min toolbar player please do