Sorry if I'm missing something obvious here -- but why not update jobIdToStageIds here too?  It looks like the way you're currently updating jobIdToStageIds, in jobIdToStageIdsAdd, involves walking the entire set of jobs/stages each time you add a new job, which would be nice to avoid.