Serializer: cache recent back references for shorter encoding.

And some refactorings.

R=mvstanton@chromium.org

Review URL: https://codereview.chromium.org/766893002

Cr-Commit-Position: refs/heads/master@{#25629}