Optimality of Message-Passing Architectures for Sparse Graphs