Improve encoder performance

use function pointers to avoid bad branch prediction with
switch() because there is no way to know which type of objects
are most common in the input.