Archive for April, 2007

How do I swap thy bytes? Let me count the ways

Saturday, April 28th, 2007
  1. swab

    swab(3) is a function that copies some bytes from one location to another, swapping each pair of bytes during the copy. Handy for structures.

    It has a feature that isn't mentioned in the Darwin manpage for swab: If you pass a negative size, it does not swap. I have no idea why this magic behavior was added; if you want a swab that doesn't swap bytes, just use bcopy. I shake my head at this use of a magic argument.

  2. ntohs, htons, ntohl, htonl

    These four functions swap the bytes of a 16-bit (‘s’) or 32-bit (‘l’, in ignorance of LP64) integer and return the transformed value.

    They are mainly used in network-I/O contexts, as they transform between network byte order (big-endian) and host byte order (whatever you're running). But there's nothing stopping you from using them for any other 16-bit/32-bit integral byte-swapping.

  3. OSByteOrder (Darwin)

    The Darwin kernel provides a number of handy-dandy macros for byte-swapping:

    • OSSwap{Const}?Int{16,32,64}
    • OSSwap{Host,Big,Little}To{Host,Big,Little}{Const}?Int{16,32,64}

    The {Host,Big,Little}To{Host,Big,Little} functions swap conditionally; the others always swap.

    According to the Universal Binary Programming Guidelines, it is safe to use these in applications.

  4. Core Foundation

    CF's Byte-Order Utilities provide the same facilities as OSByteOrder, with a couple of twists:

    • The implementation uses assembly language when the environment is GCC on either PowerPC or x86. This is theoretically faster than OSByteOrder's pure-C implementation. (CF falls back on pure C in all other environments.)
    • CF adds support for byte-swapping 32-bit and 64-bit floating-point numbers.
  5. Foundation

    Foundation's byte-order functions bear all the same capabilities as the CF Byte-Order Utilities. In fact, they are implemented with them.

  6. NeXT byte-order utilities

    These utilities are equivalent to the Foundation functions, except that they are implemented using the OSByteOrder utilities. They are declared in <architecture/byte_order.h>.

  7. Core Endian

    Core Endian logo that I made up.

    I think that the “Core Endian” name itself is new in Panther. Three functions in the API have a “CoreEndian” prefix, and are marked as new in Panther, whereas the others have simply “Endian”, and are marked as having existed since 10.0. This suggests to me that the entire API was branded “Core Endian” in 10.3, with the older functions subsumed by it.

    The new functions have to do with “flipper” callbacks, which you can install so that things like Apple Event Manager can DTRT with your custom data types. The older functions are plain byte-swapping utilities, just like all the other APIs described here, and exist mainly for the benefit of QuickTime users (they exist on Windows, too, through QuickTime).

Report-an-Apple-Bug Friday! 57

Friday, April 27th, 2007

This bug is NSFrameRectWithWidth uses the current color, not the stroke color. It was filed on 2007-04-27 at 16:17 PDT.

(more...)

Ahhh, no more paging!

Thursday, April 26th, 2007

Just after launch, MemMeter reported that I have 564 MiB of memory in use and 4557 MiB of memory free.

My 4 GiB of memory from Other World Computing arrived today. Woo-hoo!

My Mac Pro came with 1 GiB of memory, which is the same as what I have in my Cube—but when you have four times as many processors, you need at least four times as much memory, especially for things like compiling programs. (Xcode will deploy one compiler per processor, and make can be instructed to deploy any number of tasks at a time with its -j flag.)

I waited to get more RAM because the Apple Store charged one-and-a-half kilobucks for the 4-GiB kit. Now, it's down to only $1200—woo-hoo! But OWC still beats the Apple Store by more than half: the 4 GiB I bought cost me only $500. That's still expensive by the Cube's standards, but I was getting really tired of the paging, and it certainly beats the heck out of Apple's prices.

Here's the before-and-after of Adium build times:

1 GiB of RAM—2007-04-20:

svnversion               %~/Projects/@otherpeoplesprojects/adium/trunk-clean(0)
19517
___
time xcodebuild -configuration Deployment
** BUILD SUCCEEDED **
xcodebuild -configuration Deployment
  252.57s user 232.52s system 196% cpu 4:07.33 total

5 GiB of RAM—2007-04-26:

svnversion               %~/Projects/@otherpeoplesprojects/adium/trunk-clean(0)
19517
___
time xcodebuild -configuration Deployment
** BUILD SUCCEEDED **
xcodebuild -configuration Deployment
  243.32s user 220.05s system 232% cpu 3:19.21 total

Both tests were run with no existing build folder. Prior to each xcodebuild, I ran the same xcodebuild in order to defeat any caching issues.

Now on FeedBurner

Tuesday, April 24th, 2007

I just moved the blog feed over to FeedBurner. It's a permanent redirect, so your reader should handle the move for you, but it doesn't update in Vienna. Please make the change yourself (and if you're using some other reader, make sure it updated)—otherwise, you're just bouncing off my server every half hour for no reason. Thanks.

Well, this sucks

Saturday, April 21st, 2007

Just as I had almost finished writing the blog post for Apple Bug Friday! #56, my display blinked off. “Rats—here comes a power failure”, I thought.

But the computer didn't turn off, nor did the lights.

By listening to the hard drive, I was able to establish that my computer was not only still on, but working. I even restarted (forcibly, as I had xterms running and X11 always complains when you try to quit when xterms are running)—the display still did not come on.


My display is an Apple Studio Display 17″ LCD, from 2003 (IIRC, I bought it just after they discontinued the Studio Display). Because of its age, it uses ADC, rather than DVI, to connect to the computer. My Mac Pro has DVI, so I had to buy the DVI to ADC Adapter to use the Studio Display.

I had the Studio Display long before I had the Mac Pro; I'd been using it on my G4 Cube for years. I still have the Cube, so I had a way to test whether it was the display that crapped out, or the adapter (or the video card). So I unplugged the display from the adapter, fished my Cube and its power supply out of the Mac Pro box (where it normally inhabits, with all the other peripherals that I bought for it that my Mac Pro has built-in), and set it up in the kitchen.

To quote the operator on the Zero Wing, “WE GET SIGNAL !!


So either my DVI-to-ADC adapter is borked, or my video card is borked. Later today Monday, I'll head on down to the Apple Store and find out which. Fortunately, they're both still under warranty.

Oh, and in case you're wondering what I'm typing this on: my mom's iBook G4, which I borrowed to help pass the time.

Apple Bug Friday! 56

Saturday, April 21st, 2007

This bug is iTunes does not FNNotify after moving items to the Trash. It was filed on 2007-04-13 at 19:56 PDT.

(more...)

Linkage

Friday, April 20th, 2007

Simone Manganelli, on his blog Technological Supernova, which I apparently link to a lot (just kidding!), presents a breakdown of pages that link to his blog. So, always one to jump on a fun-looking bandwagon, here's my implementation of the same thing…

(more...)

New service: Make Obj-C Accessors

Thursday, April 19th, 2007

A pair of services, actually: One to generate declarations, and the other to generate definitions. Simply select a run of instance variable declarations (ideally copied and pasted into the area where you want the methods to go), then invoke the service. Couldn't be simpler—at least until Obj-C 2.0 arrives.

Wit and wisdom from the manpages

Tuesday, April 17th, 2007

From man strftime:

BUGS

There is no conversion specification for the phase of the moon.

Feel free to share your favorite manpage jokes in the comments. (I'll do one for you: the obligatory tunefs joke.)

So you need to get a count of all asterisks in a string

Monday, April 16th, 2007

You have a string, which I'll call sample, and you need to count the number of asterisks in it. For comparison purposes:

>>> sample = ' *' * 5000000 #Just imagine that you got this from somewhere else

What do you do?

Solution A: filter

(ifilter won't work here, because you can't count it.)

>>> start = time.time(); len(filter(lambda ch: ch == '*', sample)); end = time.time()
5000000 #The correct result
>>> end - start
2.2621231079101562 #seconds

Solution B: List comprehension

(Generator comprehensions won't work here, because you can't count them.)

>>> start = time.time(); len([ch for ch in sample if ch == '*']); end = time.time()
5000000
>>> end - start
2.005012035369873

OK, so it looks like I'll be going with list com—WAIT! What's that!? It's a regular expression!

Solution C: re.findall

>>> start = time.time(); len(re.findall(r'\*', sample)); end = time.time()
5000000
>>> end - start
0.40664911270141602

…Wow.

Incidentally, I didn't find a statistically-significant speed-up in running re.compile over the expression first. Apparently, this expression isn't complex enough for that to help any.

[Added 2007-04-19] So I guess that's it then. re.findall is the winn—

What's this? New comment from Chuck…

Solution D: str.count

>>> start = time.time(); sample.count('*'); end = time.time();
5000000
>>> end - start
0.038351058959960938

A factor-of-ten improvement! Wow—thanks, Chuck!

Report-an-Apple-Bug Friday! 55

Friday, April 13th, 2007

This bug is QuickTime does not allow protected MPEG-4 video files to be opened. It was filed on 2007-04-13 at 15:51 PDT.

(more...)

ICeCoffEE and Services are your friends

Thursday, April 12th, 2007

ICeCoffEE is an APE extension by Nicholas Riley that lets you ⌘-click on a URL in any NSTextView to open that URL in your favorite browser.

But it has another feature that's far more useful than that.

ICeCoffEE is named after ICeTEe, an extension for Mac OS that also provided the ⌘-click feature. ICeTEe added it to TextEdit (that's the “TE” part; TextEdit was the text-editing control in Mac OS, and it's totally separate from TextEdit.app). But ICeCoffEE has one other feature, which I find far more useful. It's a two-part feature:

  1. It adds a Services menu to your contextual menu.
  2. And you can edit that menu.

What that means is that rather than having to mouse all the way up to the Application menu, then rifle through an enormous list of services*, you can instead simply right-click on the view that already has your focus, and find the service you want among the set of services that you actually use.

(Before you mention ServiceScrubber: That works by editing applications' Info.plists. Not a good way to go about it. I'd rather have services specifically excluded from the Services menu without having to edit any part of the applications. ICeCoffEE provides this.)

This is pretty big. The major reason why people don't use services—even in the face of ThisService, which is the greatest developer tool since Interface Builder—is having to cross half the screen and then wade through several dozen menu items. ICeCoffEE solves both of those problems, so now you have no excuse not to use the full power of services in your work.

It also lets you put the Services menu in the menu bar, but while that beats having it hidden in the Application menu, I don't think that that's as cool as having it in the contextual menu. Worse, it doesn't let you edit the menu-bar Services menu. The one in the contextual menu is a hundred times more useful.

ICeCoffEE is free and a UB, and works in any Cocoa app. You'll need Mac OS X 10.1 or later, though current versions require 10.3.9 or later.

* My Services menu has 46 menu items in it, and that's counting submenus as 1 each.

New utility: qtsetclip

Wednesday, April 11th, 2007

I've just released my latest command-line utility, qtsetclip. This is a utility that allows you to set the clipping region of a QuickTime movie to a rectangle you specify. I use it in producing the Adium screencasts, since Final Cut Express limits me to choosing among certain predefined resolutions. (Further details will appear on the Adium Blog on Saturday.)

A novel way to reduce the size of a grayscale PNG file

Sunday, April 8th, 2007

Today, I scanned in one of my old drawings: a study of five-pointed stars that I made when I was trying to figure out how to draw a proper star (this was at the time of me working on Keynote Bingo MWSF2007 Edition, and a derivative of the same star is used in TuneTagger).

The odd thing is, after I corrected the image using Preview's Black Point and Aperture controls (no relation to the photo-management program), the image weighed about two-fifths as much:

du -b Five-pointed\ star\ study* %~/Pictures(0) 1403443 Five-pointed star study-adjusted levels.png 3346498 Five-pointed star study.png

(These sizes are after pngout, but even if I re-correct the original image and save it elsewhere, it comes out 1790244 bytes long.)

Go figure.

Why Mac programmers should learn PostScript

Saturday, April 7th, 2007

I'll follow this up with a tutorial called “PostScript for Cocoa programmers”, but today brings my list of reasons why you should care in the first place.

(more...)

Report-an-Apple-Bug Friday! 54

Friday, April 6th, 2007

This bug is iTunes should use real combo boxes in its Info dialogs. It was filed on 2007-04-06 at 23:48 PDT.

(more...)

Negative Turing Test now supports deletion

Wednesday, April 4th, 2007

As of r58, you can now tell Negative Turing Test to delete spam comments instead of marking them as spam. (This is in the NTT Options pane.)

I just turned this on here. It worked fine on the test post; we'll see how well it works in real usage.

Oh, and in case you ever need to delete a comment from a WP plug-in: Use wp_set_comment_status. I thought for so long that WP had no programmatic way to delete comments—now I know that it does.