Tag Archives: tonino

Mac, cheap SSDs, TRIM, and Safari

OK, here’s the deal: I’ve got a Mac, I’ve got an SSD. My SSD’s firmware supports the TRIM command, which means that on operating system’s that support said command, deleting files actually causes files to be deleted, rather than marked as deleted. The benefit of this is that next time something has to be written to that spot, it can just be written, rather than deleted and written. Mac OS X does not support this command. Furthermore, my SSD’s firmware isn’t the brightest of things: it doesn’t intelligently know where to put new files… at least not when the drive is 75% full. It also doesn’t have a feature called Garbage Collection, which essentially performs something similar to TRIM when the drive isn’t in [much] use, without the OS knowing a thing.




To summarize, I have a cheaper SSD and it’s not the brightest, best thing on the block. Had I known about all these problems before I bought it, I would have sprung for a more expensive one, with all the bells and whistles that I need. But here’s the thing: it’s still really fast for reading files. Given that I have a MacBook Air, the alternatives to SSDs are extremely slow 1.8″ hard drives, so while I’ll eventually get a better SSD, I’m not going back to mechanical hard drives. Even when I change my computer, to whatever I chose, my SSD is coming with me or I’m getting a new one. Applications literally launch instantly, it’s unbelievable. I can’t go back to a spinning platter!




Now, all these things out of the way, you’re probably wondering what the problem is. The problem is that the biggest limitation of the drive is when writing dozens of little files to disk, because with the deletion of a tiny file and re-creation of the same file, the system slows down to a crawl. In mast applications, this isn’t a problem at all. When writing code, it’s a tiny bit of a problem (on small programs, anyway) but not the biggest problem in the world.




Where the SSD utterly fails is




A- write performance degradation over time


and


B- applications that are very generous with small cache files




The second I will get into first, because it is actually very specifically talking about one app in particular: Safari.








B – Solutions:




Safari (4, 5… whatever!) is a little cache monster. I have studied its behaviour and even with caches turned off in the ‘Develop‘ menu (which you can get in Safari > Preferences > Advanced > Show Develop menu in menu bar) it slows down after enough time has passed.




Safari is quite fast on other computers, but on my SSD (and other cheap SSDs) the constant writing of little files makes it unusable over time. It stores many things: bookmarks, history, cookies, extensions, web forms and passwords, but most importantly: webpage previews (screenshots of every site you visit!), and your cache. These two items are what are constantly being written to disk , and the way Safari does it mocks us cSSD (cheap SSD, you heard it here first!) users: it creates a tiny file, presumably reads the tiny file, then deletes the tiny file. Over, and over, and over again.




B – Solution 1: other browsers




Use Chrome, or Firefox, both of which manage cache more intelligently. How to do this seamlessly? Install X-marks for every browser you wish to use, enabling your bookmarks to sync between all your browsers, and in Firefox install the Add-on “Keychain Services Integration” to allow it to use the system keychain for passwords. And there you go: all your passwords and bookmarks available system-wide!
The caveat is that the Safari bookmarks are in their own folder. Where most browsers have 2 bookmark locations: a bookmarks menu and a bookmarks bar, Safari has three: an additional ‘Bookmarks’ location. What I have done is within the bookmarks bar, created a folder called ‘BOOKMARKS’ and put everything in Safari ‘Bookmarks’ in that folder… here’s a screenshot:

Move BOOKMARKS elsewhere

So now, I can use any browser I want at anytime. Oh, and my Bookmarks still sync with MobileMe, so if I add a bookmark in Chrome, it shows up automatically on my iPhone within minutes. This is a great tip for anyone, really. It gives you more options, and is quite transparent. I have done this, even though there is a second solution to help me out…

B – Solution 2: stop using cache + webpage previews

The two culprits that slow Safari down are, as I mentioned, webpage previews, and cache.
Shall we remove them altogether? Safari won’t know the difference, and you probably won’t either. But you will notice a huge performance increase with a cSSD. First quit Safari, then run the following shell commands by launching ‘Terminal‘ in your Applications > Utilities folder:

rm -f ~/Library/Safari/WebpageIcons.db
rm -f ~/Library/Safari/WebpageIcons.db-local
rm -f ~/Library/Caches/com.apple.Safari/Cache.db
rm -f ~/Library/Caches/com.apple.Safari/Cache.db-local
rm -fr ~/Library/Caches/com.apple.Safari/Webpage\ Previews




touch ~/Library/Safari/WebpageIcons.db
touch ~/Library/Safari/WebpageIcons.db-local
touch ~/Library/Caches/com.apple.Safari/Cache.db
touch ~/Library/Caches/com.apple.Safari/Cache.db-local
mkdir ~/Library/Caches/com.apple.Safari/Webpage\ Previews 

The first set of commands deletes all the files, and the second set re-creates them. the .db files are the actual database files in which things ore saved, the -local files are the scratch files that Safari writes things to before writing them to the database files (we have to consider these because otherwise the disk will still be written to for no reason once the caches are disabled), and the folder ‘Webpage Previews’ is deleted, with all the snapshots that Safari takes (you’d think it’s a photographer if you looked in that folder before deleting stuff…).




Next, navigate to your Home folder > Library > Safari, and you’ll see WebpageIcons.db and WebpageIcons.db-local. Right click on each, goto Get Info and check Locked. A little lock should appear below the icons for these files. You may close the Get Info window. Do the same for the files Cache.dbCache.db-local, and the folder Webpage Previews in your Home folder > Library > Caches > com.apple.Safari

Locking files: commanding with an iron fist!




…and there you have it! Safari is fast again! If there’s anything I missed, I’ll eventually stumble upon it and post it here, but so far it seems that I can BREATH! Safari will still need to write to disk to update your history, top sites, bookmarks, and other settings, but it doesn’t do this constantly and so it shouldn’t be a problem. Alternatively, I have found that creating a RAM disk, and placing both folders Caches > com.apple.Safari and Library > Safari in it, then creating symlinks and placing those where the original folders should be. The speed increase is technically faster, but in practice there’s not much difference. Certainly nothing insanely noticeable. The problem with a RAM disk is that it needs to be unmounted eventually, and so if the full folders are there they need to be saved elsewhere first, and if something crashes you could lose your bookmarks. You could also put only the files we played with above in there, but then you take up a lot RAM needlessly, though there are some speed benefits from loading things from cache. Overall, the above is a great way to muck with as little as possible while using Safari (my favourite browser!) with a cSSD.








If that’s the only slowdown you notice with your cSSD, you can stop here. Otherwise, there are additional methods I have found that work to ‘TRIM’ your SSD, without installing Windows and running a TRIM utility for your drive. These solutions cost money, but may be worth it if you feel like your SSD isn’t what it was when you first got it.







A -Tonino Trim – Solutions for TRIMming your SSD in Mac OS X:

It’s a two or three part solution, really. If you really want all your speed back (remember, cSSDs degrade over time) then you’re going to want to find a way to empty your SSD completely. Otherwise, you can trim most of the free space on the SSD with an additional application.

Wipe Drive method for -tonino trim:

Step 1: Clone your SSD. If the SSD is internal to your Mac, get a USB drive (or firewire, if you have any still around) that is at least as big as your SSD and using an application like SuperDuper! (free) clone it to the external drive. If it’s external, find some way of saving all the data that’s on it somewhere else.

Step 2: Boot from a different drive and erase your SSD by deleting and re-creating the partitions. If internal, then you can boot from the clone you made to do this, or you can boot from a Mac OS X Install DVD. Basically, format the SSD now that all your data is safely backed up elsewhere.

Step 3: Buy DiskTester ($40) and run the Recondition SSD command on your drive according to these instructions.

Step 4: Put whatever was on your SSD before you -tonino trimmed it back on. If you had cloned it, re-clone back to the SSD, for instance.

On-the-Fly method for -tonino trim:

Step 1: Buy iDefrag ($30).

Step 2: Either by making a bootable DVD, or by booting into iDefrag’s special mode, or by booting from another install (like the clone you may have made in step 1 of the wipe drive method) run the ‘Compact’ algorithm on the drive. This will compact all the data on the drive together, so that your have one large chuck of free space. This is necessary for DiskTester in step 4 because it requires a large, un-used chuck of free space to recondition your SSD properly. 

Step 4: buy DiskTester ($40) and run the Recondition SSD command on your drive (from Mac OS X) according to these instructions.


…and that’s it. You’ve restored your SSD its original speed. If you followed the On-the-Fly method, then you sped up the unused part of the drive. Otherwise, the whole thing is back to normal. Note that it will degrade over time again, but at least now you can restore it. This was all possible because of the Mac Performance Guide’s article here.

If you have doubts or questions, leave a comment. What I can say is that I do this every few weeks and between a speedier Safari and regular maintenance, my Mac is quite fast. It takes some work, but it’s completely possible to keep the speeds of your SSD stable over time. That said, don’t buy a cheap SSD. Buy one with garbage collection built-in, like the ones from RunCore or OWC which don’t degrade over time. I should also note that while I have followed some guides to tweak my SSD and have found the noatime tweak works, but not as dramatically as the Safari one that I describe here. The reason is that Safari is pretty much always open on my Mac, and even though Chrome is a close second in my opinion (and manages cache better), it’s still slower and I prefer Safari for its lower CPU usage. 

Hopefully this helps someone out there!