Slight optimization by avoiding to query for hidden state unnecessarily.

Also removing static_cast during the loop speeds it up just a bit.

Change-Id: I4644046c91da71f4b7305e9063a853d03c5921e0