Indirect table slices #207
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello,
I apologize in advance for this unsolicited PR, I am using the PR functionality to demonstrate a potential improvement to the codebase but its contents do not adhere to the contributing guidelines.
CAD1997 posted this reddit thread asking for help with some problems.
While I cannot directly help fix the problem I did notice some potential for improvement:
CharDataTable
is rather inefficient when the mapped to valueV
has more than one value. TypicallyV
then becomes for some propertyT
:&'static [T]
incurring quite a hefty overhead of 16 bytes perchar
for the slice reference as well as creating 10,000s of static slices that the compiler needs to deal with.To improve this situation you can create a second table indexed by
V
being aRange<u32>
or evenRange<u16>
. On x64 this meansV
is now 8 or 4 bytes respectively (compared to 16 bytes) and instead of 10,000s of small static slices the compiler just deals with one huge static slice.In cases where
V
ends up being&'static [&'static str]
and there are many string literal repetitions it can be further improved by substituting an enum for the&'static str
and leaving conversion from the enum to string to users of the crate. However this has impact on user facing APIs (you cannot return the slice of string slices directly any more).That is what I attempt to implement in this PR, but I didn't get very far.
I introduced a trait to replace the
CharDataTable
enum which will allow me to create dedicated structs to the different ways the tables can be stored. Now that I think about it this probably isn't too essential. I rename the old methods so I can easily find the places I need touse
the new trait.I create dedicated structs for the two variants for the
CharDataTable
enum and implement the previous trait.I selectively replace some of the
CharDataTable
instances to the new struct, to get an idea of how feasible this approach is. Hacked together by editing the generated code because I don't know how the code generation step works.I want to show how this indirect table can work. Actually updating the generated data was too daunting so I stopped there.
Anyway that's where I got after a few hours. If the compiler's problem is that literal 10,000s of small static slices are created then instead concatenating them in one big table from which they're indexed may help. This indexing approach should still be able to reduce memory overhead of all the slice references.