For my programming language, I have built an engine that goes after the parser and before the compiler. This engine analyzes, validates, and simplifies the AST. During the analasys, it determines as much information as possible about each and every variable. So, for each variable, I have a tree representing every possible value the variable may have at any given point in the program (or just minimum/maximum values (for numbers) and possible elements (for strings) if there is such a pattern).
Because of this, I have the capability to determine the most memory efficient type that stores only the minimum information required to reconstruct the value. For example, in the following code:
import random
possible = ['a', 'c', 'n', '2', '8', '1', 'o']
string = ""
for i in range(10):
string += random.choice(possible)
print(string)
(My language does not actually look like Python, this is just an example).
My system would determine that string
can only ever have a maximum of 10 characters, and those characters will always be one of ['a', 'c', 'n', '2', '8', '1', 'o']
.
This compels it to store string
in a way such that 3 bits are allocated to every character (this is the minimum to represent every possible character). It will also note the maximum number of characters (10) and so choose to allocate 10 * 3 = 30 bits (usually 32 because of alignment).
This makes it really memory efficient, but it's not too good for speed because it has to be processed on storing and reading to change it to that type. I was wondering if there was a way to determine the fastest data type for a particular scenario on a specific platform. I'm new to this, so I'm not sure how to determine which one is best optimized for the platform. Some platforms natively support IEEE 754, some don't. Some have their own data types they want programmers to use. How can I know which will run the fastest?
If it helps, I plan to compile to LLVM IR, though I don't have the compiler done yet. The language is written in (an interpreter version of) itself, if that's relevant.