Thursday, October 20, 2016

Using a Router with Bushings to Make a Halloween Grave

The New Router

I had a previous router that my wife and I bought to experiment with. It was good enough to make a few holes but not really up to scratch for any real work. With a looming shelving project on the way, I stepped up and bought an industry standard Porter Cable 690 family router. What a great thing!

My wife and I like to decorate for Halloween - she has a wonderful collection of crafts and primitives that she makes and we're gearing up to sell online. Not to be left out, each year, I experiment with a new woodworking technique on a decoration for the graveyard we set up outside. Last year, I made a glow in the dark skeleton. This year, I went for a classic gravestone with a routed inscription.

The Template

I had no idea what I was doing so my basic approach was to make a template and then use the router to carve a shape against it. I sketched out a shape and then used a jigsaw and a power drill to cut it out. I tried to ballpark how big the stencil had to be, but having had no experience I just dove in. I would learn the lessons as I went.

First, I made a template.

The router and bushing

Here, I have the bushing on the router. A router, for those who don't know, is similar to a drill, except, that it is ideally heavier, so it is stable, and has a flat base with an adjustable height. In this way, you can cut shapes of exact depth into the wood. The Porter Cable 690 series router that I bought is noteworthy because there are a variety of bits and other things that work with it. One such thing is the bushing. A bushing is simply a piece that screws into the base and wraps a bit around the drill end. In this way, you can create a sandwich of a template and the target wood. The bushing keeps the router bit from drilling into the template itself. Obviously, you have to measure correctly.

The routed piece

A good router leaves the edges smooth. The bottom should be pretty smooth as well. If there's waves or other marks, it means you didn't get a consistent depth. It turns out, that's what the big handles are for. Since I was using an old piece of wood, the routed areas have a very different color. After that, I use a couple of coats of stone texture spray paint, followed by acrylic spray paint to lock it down, to give it that graveyard look. The picture here is after the first coat. Finally, a jig is used to drill some holes in the base for spikes to stick the grave in the ground.

Lessons Learned

The biggest lesson I learned in this project is that good template planning is absolutely essential. I just sketched up a skeleton head and an RIP and cut it out of an old piece of wood, which was fine to fool around with. But to make a really good show of it, what I ought to do is consider the bushing size in the design, and plan more elegantly. I can see some computer aided design - even using paint.net with brushes, would be beneficial. That way, I can visualize where the router bit will cut out, inside the bushing area.

Happy Halloween!

Monday, October 17, 2016

C+++ Compile, Part 2, Vectors vs Native Arrays in x86-64 bit Release Builds

Release Builds

We all understand that release builds make for smaller, faster code. But how? What does the compiler do differently. In C++, the difference be dramatic. Let's take a look at the exact same program in release.

What happened? Well, a couple of things. First, we notice right away that all the function calls to our little array methods are gone. Instead, all the code for our calls are baked right into the main method. This is what is called inlining. Instead of generating code for a new method and then a call for it, the compiler simply generates the code for the method in the context of the caller. There's two advantages of this. First, the advantage is that there's no function call, saving at least some stack manipulations. In particular, if the same value, say, the value of an array, is referenced across several inlined functions, the compiler will simply allocate it to a register, if it can, and then re-use that. This will put a little zoom zoom in the code.

Inlining is the secret sauce that makes C++ templates and STL workable performance-wise. A lot of what STL does is actually inlined, and from there, reduced. All those begin and end methods, ++overload methods and other tidbits in C++ that make up the implementation of iterators were simply optimized away.
We would be tempted to say that we could compare the STL implementation to our simple vector implementation, except that, it looks a lot more like, the compiler was just about smart enough to realize that the vector assignment wasn't used in our simple array methods and eliminated the calls and inlines completely.

In fact, if we look closely in this image, it almost looks like the compiler rather just blew away one of our test methods because it knew it wasn't even really being used. The only thing that remains appears to be a loop that counts to ten, but it never assigns the value to the array. Let's see if we can find out what happened. To do this, we try to back off on some of the optimizations we make in release build and see if we can see what the compiler is doing.

Optimization Options

There are several optimization switches in there, and its not immediately clear what these things also do. We experiment. As we are interested in what happened to our methods, first off, we turn off the inlining by setting "Inline Function Expansion" to Disabled (/Ob0) and then recompile.

 When we examine the compiler output, we can see that the nested layers of function calls have been restored.And, notice that all the STL methods are back in there as well. Suddenly, what was supposed to be a fairly zippy way to make performant but re-usable containers has been utterly ruined. Thus, we've learned an important lesson - that inlining is a pre-requisite to using STL.

Other things happen as well, and we'll work through the switches and what they do. But first, we're going to need to pause and actually dive into a little bit of assembly language.