def get_savings(td, fds):
    gsavings = [-(s.subr_cost + 2) if s.program else 0 for s in td.GlobalSubrs]
    lsavings = [[-(s.subr_cost + 2) if s.program else 0 for s in fd.Private.Subrs] for fd in fds]
    gusages = [0 for _ in td.GlobalSubrs]
    lusages = [[0 for _ in fd.Private.Subrs] for fd in fds]
    gbias = psCharStrings.calcSubrBias(td.GlobalSubrs)
    lbias = map(lambda fd: psCharStrings.calcSubrBias(fd.Private.Subrs)
                           if hasattr(fd.Private, 'Subrs') else 0,
                fds)
    
    def count_subr(idx, is_global, fdidx=-1):
        if is_global:
            gsavings[idx + gbias] += (td.GlobalSubrs[idx + gbias].subr_saving - tokenCost(idx) - 1)
            gusages[idx + gbias] += 1
            subr = td.GlobalSubrs[idx + gbias]
        else:
            assert fdidx >= 0
            lsavings[fdidx][idx + lbias[fdidx]] += (fds[fdidx].Private.Subrs[idx + lbias[fdidx]].subr_saving - tokenCost(idx) - 1)
            lusages[fdidx][idx + lbias[fdidx]] += 1
            subr = fds[fdidx].Private.Subrs[idx + lbias[fdidx]]

        # follow called subrs:
        for before, tok in zip(subr.program, subr.program[1:]):
            if tok == 'callgsubr':
                count_subr(before, True, fdidx)
            elif tok == 'callsubr':
                count_subr(before, False, fdidx)
    
    for g in td.charset:
        cs, sel = td.CharStrings.getItemAndSelector(g)
        for before, tok in zip(cs.program, cs.program[1:]):
            if tok == 'callgsubr':
                count_subr(before, True, sel)
            elif tok == 'callsubr':
                count_subr(before, False, sel)
                
    return ((gsavings, lsavings), (gusages, lusages))
dataset/ETHPy150Open googlei18n/compreffor/tools/subr_grapher.py/get_savings/original