Slow code, slow

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Feb 26 18:57:23 UTC 2018


On Mon, Feb 26, 2018 at 08:38:39PM +0200, ketmar via Digitalmars-d wrote:
> H. S. Teoh wrote:
> 
> > On Sat, Feb 24, 2018 at 09:43:35AM +0000, Stefan Koch via Digitalmars-d
> > wrote:
> > [...]
> > > This particular slowdown happens because there are somehow
> > > depdencies on std.format.format which is instantiated.  Which has
> > > a ton of dependencies itself.
> > 
> > Aha!  That explains it.  Thanks, Stefan, for the accurate diagnosis. :-)
> > 
> > Now the next problem is: how to trim the fat off std.format ...
> 
> p.s.: and ditch type safety. 'cause `foo(T...) (T args)` is a major
> slowdown -- it is a template. ;-)

Actually, I think this is the classic example of why the compiler should
improve the way it implements templates.

In my mind, even C's printf API is sucky, because it involves runtime
parsing of what's usually a static string, over and over again. What we
*really* want is for something like:

	writeln("blah %d bluh %s", i, s);

to be translated into something like:

	stdout.putString("blah ");
	stdout.putInt(i);
	stdout.putString(" bluh ");
	stdout.putString(s);

I.e., there should not be Yet Another Template with Yet Another
Ridiculously Long Argument List Type Encoded In The Mangled Name, along
with needless marshalling of function arguments on the stack, branching
to some other part of the code (potentially causing an instruction cache
miss), tons of copy-pasta for calling the same old functions for
outputting strings and formatting integers, and incurring Yet Another
Branch Hazard when the function finally returns.

And there should definitely be no silly runtime parsing of format
strings and all of that useless dance.

The latest Phobos does support compile-time format strings, but all that
does currently is to forward to the silly runtime parsing code (not to
mention coming with its own baggage of additional templates to do the
compile-time format string checking).

Basically, the whole stupid function call should just be completely
inlined and any external template function bodies thrown out the window,
because chances are you'll never call format() again with exactly the
same parameters somewhere else in the code.

I haven't checked if ldc will actually do this level of inlining, but
dmd certainly won't with its overly-conservative inliner.  And besides,
it's a stupid waste of compiler resources to have to generate all of
that template code every single time format() is called, only to have
the optimizer basically undo half of the work.  There should be some way
in the language to express format() in a way that doesn't involve tons
of template bloat and wasted function body copy-pasta.


T

-- 
Meat: euphemism for dead animal. -- Flora


More information about the Digitalmars-d mailing list