Checkout pr_PersonEntityRepresent itself - it is a nested represent method itself: pr_pentity is a super-entity and hence needs to load the instance records in order to represent the pe_ids. It implements lookup_rows() in order to avoid unnecessary lookups.

But your pr_RoleRepresent calls it row-by-row, so this lookup_rows() function is always called with a single ID instead of all pe_ids.

To overcome this, you need to implement a lookup_rows method in pr_RoleRepresent which performs the pr_entity lookup for /all/ roles at once, and then remove the additional call to pr_pentity_represent in represent_row().

Apart from that: you're not dealing with just 2 tables here - since pr_pentity is a super-entity, you have to deal with >10 different tables here, so optimizing this requires some more insight. I wonder whether it is worth it - so I do need to understand the case.