[Issue 9584] New: Exceptions in D are ludicrously slow (far worse than Java)

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Feb 24 15:47:01 PST 2013


http://d.puremagic.com/issues/show_bug.cgi?id=9584

           Summary: Exceptions in D are ludicrously slow (far worse than
                    Java)
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: druntime
        AssignedTo: nobody at puremagic.com
        ReportedBy: jmdavisProg at gmx.com


--- Comment #0 from Jonathan M Davis <jmdavisProg at gmx.com> 2013-02-24 15:46:57 PST ---
Take this D program

------
import std.conv;
import std.datetime;
import std.stdio;

void main()
{
    auto sw = StopWatch(AutoStart.yes);
    try
        throw new Exception("blah");
    catch(Exception e)
    {
        auto diff = to!Duration(sw.peek());
        writeln(diff.total!"hnsecs"());
        writeln(diff);
    }
}
------

and this Java program

------
class j
{
    public static final void main(String[] args)
    {
        long before = System.nanoTime();
        try
        {
            throw new Exception("blah");
        }
        catch(Exception e)
        {
            long diff = System.nanoTime() - before;
            long totalHNSecs = diff / 100;
            long msecs = diff / 1000000;
            diff -= msecs * 1000000;
            long usecs = diff / 1000;
            diff -= usecs * 1000;
            long hnsecs = diff / 100;
            System.out.println(totalHNSecs);
            System.out.printf("%sms %sus %shnsecs\n", msecs, usecs, hnsecs);
        }
    }
}
------

They are roughly equivalent, but their performance is drastically different
(and Java wins by a _long_ shot). These are typical outputs for the D version

13962
1 ms, 396 μs, and 2 hnsecs

13658
1 ms, 365 μs, and 8 hnsecs

13745
1 ms, 374 μs, and 5 hnsecs

whereas tese are typical outputs for the Java version

126
0ms 12us 6hnsecs

126
0ms 12us 6hnsecs

140
0ms 14us 0hnsecs

It varies a bit from execution to execution (particularly for D), but in
general, I'm seeing that D is taking 90x - 110x longer than Java is. Oddly,
once in a blue moon, D seems to take more like 20x longer, but even that's a
huge difference (and not in D's favor). In general, Java shouldn't be beating D
for performance, since D is a systems language, and a magnitute of 100x is
pathetic.

Granted, code should not be constantly throwing and catching exeptions, but
this sort of bad performance can make it so that exceptions have far more of a
negative impact then they should. The place that this is particularly negative
is in unit tests. Good unit tests will test that functions act appropriately
when given bad input, which generally means that they throw exceptions, and the
tests catch them, and right now, if you do much of that, unit tests start
taking a very long time to execute - unacceptably long. This makes it so that
code must be tested less in order for the tests to take a reasonable amount of
time, which will lead to worse code quality.

So, I think that it's clear that we need to take steps to improve the
performance of throwing and catching exceptions. As I understand it, the
stacktrace is currently put into string format when the exception is
constructed, which is unnecessary overhead if the stack trace never gets
printed, so that's one improvement that could be made. But I have no idea how
the internals of any of this currently work, so I don't know what other
improvements could be made. Regardless, we need to take steps to significantly
improve the situation.

Note that these tests were done on Linux. I don't know how the times compare on
Windows, though I expect that the situation is similar there.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list