String Switch Lowering
Steven Schveighoffer
schveiguy at yahoo.com
Thu Jan 25 19:48:52 UTC 2018
On 1/25/18 1:41 PM, H. S. Teoh wrote:
> On Thu, Jan 25, 2018 at 07:21:29PM +0100, Benjamin Thaut via Digitalmars-d wrote:
>> If we think this is a good idea, should we rewrite this particular
>> string switch to use a associative array instead to avoid the overly
>> long symbol name?
> [...]
>
> I believe the original idea behind using a template for string switches
> was to allow the possibility for the implementation to be smarter about
> how to implement the switch (IIRC, string switches used to be
> implemented as a runtime function). Supposedly object.__switch could
> analyze the list of strings at compile-time and generate a perfect hash
> or something, to maximize runtime performance.
I believe that when the number of cases is small enough, the binary
search of the strings is done recursively to allow full optimization.
And these symbols should be relatively small.
But I think if the number of strings is large enough, it calls the same
runtime function as before (essentially). But here we have a problem --
the wrapper for this function is essentially this giant symbol that
generates the string array locally, and then calls the real function.
It's a huge cost just for a simple inline-able wrapper to another call.
See the code here:
https://github.com/dlang/druntime/blob/master/src/object.d#L3873
The function that does the real work is __switchSearch, which is only
templated on the char type. It's going to be very very infrequent that
the exact same switch case list is used in multiple places, meaning you
are paying a huge cost for essentially memoizing these string lists.
I think we need some way to mark a function as following a different
mangling, or maybe even just avoid the whole memoization, do everything
inline.
-Steve
More information about the Digitalmars-d
mailing list