Skip to content

Support precompiling fast string -> string switches #115371

@Sergio0694

Description

@Sergio0694

Note

Somewhat related to #115323, but this is about string -> string switches specifically.

As part of the sidecar .dll for CsWinRT 3.0 (see tracking issue), we need to generate a "type hierarchy lookup", to store all runtime class names for projected base Windows Runtime classes in the entire application domain. This is needed to properly marshal runtime classes to managed: we will try to marshal the actual type, and if that's been trimmed, we'll use this lookup to walk the type hierarchy and find the most derived type that hasn't been trimmed, and create an instance of that type. This allows dynamic casts to work as expected.

This lookup is basically a giant string -> string map, which can have ~1000 names in a large project (the Windows SDK alone has ~750 pairs). We're looking for some way to have this be pre-initialized, so that we get (1) fast lookup, and (2) no initialization cost at runtime. We considered:

  • Emitting a single method with a switch. This ends up being so massive the JIT just gives up optimizing (see here)
  • Using some static readonly dictionary or frozen dictionary. This works, but is not pre-initialized.
  • Build our own frozen data structure. We could do this but I was thinking:
    • This seems like a generally useful feature, it might be nice to have a generalized solution that can benefit others too
    • You can't have ReadOnlySpan<string> properties, and I'm not sure ILC can fully pre-initialize a static readonly string[] field. Can it?

@jkotas @MichalStrehovsky thoughts on this? What I was thinking is, could we perhaps just find either some pattern we can rely on, or maybe a specific constructor pattern for eg. FrozenDictionary<string, string>, that allows ILC to fully pre-initialize such a switch, and we can just use that?

Thank you! 🙂

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions