Improve API performance, part 2.

Summary:
This commit rewrites APIEncoder to optionally accept and cache a namespace
public id parameter. If we serialize a large number of objects at once,
separately fetching the namespace public id for each object is actually quite
expensive.

Example numbers, running locally on  a set of ~5000 messages and 1000 threads:
```
-----------------------------------------------------------------
                                         |    time in seconds
Query                                    ------------------------
                                         | master |  D210 |  D211
-----------------------------------------------------------------
/threads                                 |  2.73  | 0.426 | 0.210
/messages                                |  2.07  | 0.790 | 0.288
/threads?to=pika-commie@mit.edu          |  2.67  | 0.586 | 0.361
/messages?to=pika-commie@mit.edu         |  4.25  | 0.918 | 0.408
/messages?thread=b5uxk18wzxry97d7xueh7vd7|  0.206 | 0.082 | 0.056
-----------------------------------------------------------------
```

Test Plan: Unit tests and extensive manual testing.

Reviewers: spang

Reviewed By: spang

Differential Revision: https://review.inboxapp.com/D211