exaloop / codon

A high-performance, zero-overhead, extensible Python compiler using LLVM
https://docs.exaloop.io/codon
Other
14.94k stars 510 forks source link

Suggested feature specialize typing for small lists with constant indexes #213

Open jplevyak opened 1 year ago

jplevyak commented 1 year ago
a = [1, "asdf", 2.0]
a[0] = 4
print a[0]

Unfortunately sometimes folks use small lists when they really should be using tuples, so the above code doesn't work.

However, these can be recognized and converted into tuples and given your bi-directional IR this shouldn't be too hard.

I do this in https://github.com/jplevyak/pyc by global constant propagation and specialization.

arshajii commented 1 year ago

They general way we're planning to handle cases like this is by promoting the list's type to a union, e.g. Union[int, str, float] in your case. Codon does support unions already, but doesn't yet do this automatic promotion. In certain cases we could also potentially just convert the list to a tuple as you mentioned.