Optimizations cherry-picked from SIGMOD branches

Thanks to @rxin for the optimizations. Other optimizations that may be useful but aren't included in this PR: 42c7f3c47623b7aaba217bfb958ba6fb182a5dbd, 6a3c44ed7a80ad23c977ce8952cd1302326a1804