Skip to content

Potential performance issue: linear template parameter lookup in reliesOnTemplateParameters #22846

@arnavsharma990

Description

@arnavsharma990

Hi,

While exploring template instantiation logic in DMD, I noticed a potential performance bottleneck in templatesem.d, specifically in the reliesOnTemplateParameters function.

Issue

In multiple visitor cases (e.g., TypeIdentifier), the code performs a linear scan over all template parameters:

foreach (tp; tparams)
{
    if (tp.ident.equals(t.ident))
        return true;
}

This check appears to be executed frequently during template deduction and semantic analysis, and is also used recursively when traversing complex types.

Why it might matter

  • This introduces O(n) lookup per check, where n = number of template parameters
  • For templates with many parameters or deeply nested types, this can multiply quickly
  • The function is called repeatedly during template matching and deduction

Possible direction

One possible improvement could be:

  • Precomputing a hash-based lookup (e.g., identifier → parameter set)
  • Or using a more efficient structure to avoid repeated linear scans

Question

Is this something that has already been considered or optimized elsewhere?

I’d be happy to explore this further and try benchmarking potential improvements if this direction makes sense.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions