Skip to content

Hashmap._Bucket.toString is very expensive to compile #17

@zorael

Description

@zorael

EndeavourOS/Arch x86_64, dmd 2.107-beta.1, ldc 1.36.0.

I'm profiling the compilation process of my project which uses requests, which in turn uses cachetools.

In an empty app.d that only imports requests, HashMap._Bucket.toString() takes 100 milliseconds to compile, or about one third of the whole compilation process (excluding compiler start-up). It is not a template so it is always compiled.

struct _Bucket {
hash_t hash;
StoredKeyType key;
StoredValueType value;
string toString() const {
import std.format;
return "%s, hash: %0x,key: %s, value: %s".format([
EMPTY_HASH: "free",
DELETED_HASH: "deleted",
ALLOCATED_HASH: "allocated"
][cast(long)(hash & TYPE_MASK)], hash, key, value);
}
}

We can't easily make std.format.format cheaper, but can we at least make toString a template? I have no use for it myself and I'd like to avoid the 100ms.

        struct _Bucket {
            hash_t hash;
            StoredKeyType key;
            StoredValueType value;
            string toString()() const {  // <-- added ()
                import std.format;

                return "%s, hash: %0x,key: %s, value: %s".format([
                        EMPTY_HASH: "free",
                        DELETED_HASH: "deleted",
                        ALLOCATED_HASH: "allocated"
                ][cast(long)(hash & TYPE_MASK)], hash, key, value);
            }
        }

The example empty file:

import requests;

void main() {}

Profile viewed in tracy:

Screenshot_20240118_130309

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions