Skip to content

Conversation

@gorsing
Copy link
Contributor

@gorsing gorsing commented Jan 13, 2026

Description: This MR refactors the Array(T) variadic constructor and toString implementation to improve performance and code clarity:

Key Changes:

  • Optimized variadic constructor: Replaced element-by-element assignment with memcpy for POD types and direct memory operations for non-POD types, eliminating redundant constructor calls
  • Early return for empty arrays: Simplified toStringImpl to immediately return "[]" for empty arrays
  • Streamlined string building: Improved memory calculation logic in toString for better efficiency
=== DMD Array Constructor Benchmark ===

Non-POD Struct       | Elements: 100    | Old:  682 ms | New:  558 ms | SPEEDUP: 1.22x
Small int[]          | Elements: 5      | Old:  386 ms | New:  276 ms | SPEEDUP: 1.40x
Medium Point[]       | Elements: 100    | Old:  432 ms | New:   52 ms | SPEEDUP: 8.29x
Large int[]          | Elements: 10000  | Old:  220 ms | New:    6 ms | SPEEDUP: 35.79x

@gorsing
Copy link
Contributor Author

gorsing commented Jan 13, 2026

Draft benchmark

import std.stdio;
import std.datetime.stopwatch;
import core.stdc.stdlib;
import core.stdc.string;
import std.array : uninitializedArray;

struct Mem
{
    void* xmalloc(size_t sz)
    {
        return malloc(sz);
    }

    void* xcalloc(size_t sz, size_t n)
    {
        return calloc(sz, n);
    }

    void* xrealloc(void* p, size_t sz)
    {
        return realloc(p, sz);
    }

    void xfree(void* p)
    {
        if (p)
            free(p);
    }
}

struct S
{
    int x;
    this(this)
    {
        x++;
    }
}

__gshared Mem mem;

// Old
struct ArrayOld(T)
{
    size_t length;
    T[] data;

    this(size_t dim)
    {
        reserve(dim);
        this.length = dim;
    }

    ~this()
    {
        if (data.ptr)
            mem.xfree(data.ptr);
    }

    //
    this(T[] elems...)
    {
        this(elems.length);
        foreach (i; 0 .. elems.length)
        {
            data[i] = elems[i];
        }
    }

    void reserve(size_t nentries)
    {
        if (data.length < nentries)
        {
            auto p = cast(T*) mem.xmalloc(nentries * T.sizeof);
            if (length)
                memcpy(p, data.ptr, length * T.sizeof);
            if (data.ptr)
                mem.xfree(data.ptr);
            data = p[0 .. nentries];
        }
    }
}

// New 
struct ArrayNew(T)
{
    size_t length;
    T[] data;

    this(size_t dim)
    {
        reserve(dim);
        this.length = dim;
    }

    ~this()
    {
        if (data.ptr)
            mem.xfree(data.ptr);
    }

    this(T[] elems...)
    {
        this.reserve(elems.length);
        this.length = elems.length;

        static if (__traits(isPOD, T))
        {
            // Fast path:
            if (elems.length)
                memcpy(data.ptr, elems.ptr, elems.length * T.sizeof);
        }
        else
        {
            // Slow path

            auto p = data.ptr;
            auto q = elems.ptr;
            const n = elems.length;

            for (size_t i = 0; i < n; ++i)
            {
                p[i] = q[i];
            }
        }
    }

    void reserve(size_t nentries)
    {
        if (data.length < nentries)
        {
            auto p = cast(T*) mem.xmalloc(nentries * T.sizeof);
            if (length)
                memcpy(p, data.ptr, length * T.sizeof);
            if (data.ptr)
                mem.xfree(data.ptr);
            data = p[0 .. nentries];
        }
    }
}

struct Point
{
    int x, y, z;
}

void runTest(string name, T)(T[] sourceData, int iterations)
{
    writef("%-20s | Elements: %-6d | ", name, sourceData.length);

    // Test Old
    auto sw = StopWatch(AutoStart.yes);
    foreach (i; 0 .. iterations)
    {
        auto a = ArrayOld!T(sourceData);
        // prevent optimization
        if (a.length == 0)
            break;
    }
    sw.stop();
    long oldTime = sw.peek.total!"usecs";

    // Test New
    sw.reset();
    sw.start();
    foreach (i; 0 .. iterations)
    {
        auto a = ArrayNew!T(sourceData);
        if (a.length == 0)
            break;
    }
    sw.stop();
    long newTime = sw.peek.total!"usecs";

    double speedup = cast(double) oldTime / newTime;
    writef("Old: %4d ms | New: %4d ms | SPEEDUP: %.2fx\n", oldTime / 1000, newTime / 1000, speedup);
}

void main()
{
    writeln("=== DMD Array Constructor Benchmark ===\n");

    S[] complexStructs = uninitializedArray!(S[])(100);

    runTest!("Non-POD Struct", S)(complexStructs, 1_000_000);

    int[] smallInts = [1, 2, 3, 4, 5];
    runTest!("Small int[]", int)(smallInts, 10_000_000);

    Point[] mediumPoints = uninitializedArray!(Point[])(100);
    runTest!("Medium Point[]", Point)(mediumPoints, 1_000_000);

    int[] largeInts = uninitializedArray!(int[])(10_000);
    runTest!("Large int[]", int)(largeInts, 10_000);

    writeln("\nDone.");
}

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @gorsing! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#22392"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants