Unless something rather nasty is going on, it should be possible to move the gets of source/target outside the loop, as they don't have any apparent dependency on mapping_type ?