3 ms·
You can just lift the objects so that they are a function of the scope so that the table of them only need to be constructed once. Python (C) does not have the
by compressedgas 6y ago
You can just lift the objects so that they are a function of the scope so that the table of them only need to be constructed once.
Python (C) does not have the tabled indirect jump that would be required to convert the table of objects into a table of offsets thereby turning the function call into a jump.
The compiler could use such a tabled indirect jump for every switch statement. Some of which could be optimized by using a different type of table rather than always using a frozendict.
- klodolph 6y ago> You can just lift the objects so that they are a function of the scope so that the table of them only need to be constructed once. So, I had very carefully worded that statement. I said you have to construct them "each time you enter that scope." Although CPython does not have a jump table opcode, it easily could, if it were a priority.
- compressedgas 6y agoI might not have understood it correctly. I thought you were thinking of: def bar(x): def X(): return 1 def Y(): return 1 def Z(): return bar(x-1) + bar(x-2) table = {0:X,0:Y} default = Z return table.get(x, default)() Whereas I was thinking of: def X(x): def X(): return 1 return X def Y(x): def Y(): return 1 return Y def Z(x): def Z(): return bar(x-1) + bar(x-2) return Z table = {0:X,1:Y} default = Z def bar(x): return table.get(x, default)(x)() Which can be reduced to a tabled indirect jump: def bar(x): jump by x via table T and default D where T = {0:X,1:Y} and D = Z X: return 1 Y: return 1 Z: return bar(x-1) + bar(x-2)