Revert "Revert "Cache matrix""

This reverts commit 91baf25fccc208262e83a65c31594e58f0cfa2aa.