`
qiezi
  • 浏览: 497848 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

[转] digitalmars.D上的一个建议。

    博客分类:
  • D
阅读更多
标题:Proposal: Operator overloading without temporaries
作者:Don Clugston

正文:
Background: Operator overloading, in the form it exists in C++ and
currently in D, inherently results in sub-optimal code, because it
always results in unnecessary temporary objects being created.

For example,
X = A - ((B*C) + D)* E;

becomes:
T1 = B * C;
T2 = T1 + D;
T3 = T2 * E;
T4 = A - T3;
X = T4;
Four objects were created, whereas only one was strictly required.
In C++, there are libraries like Blitz++ which use complicated
expression templates in order to avoid these creating these temporaries,
and provide performance comparable with FORTRAN. I think D can do much
better...
Note that temporaries are avoided when using the opXXXAssign() operators
like +=.

===========
   Proposal
===========
(1) Allow the compiler to assume that b = b + c  can be replaced with b
+= c. (In C++, operator + and operator += are just symbols, the compiler
doesn't know that there is any relationship between them).
In the example above, this would allow the compiler to generate:
T1 = B * C;
T1 += D;
T1 *= E;

and we have eliminated two of the three temporaries.
(2). Fill in the gaps in the operator overloading table by introducing
opAddAssign_r, opSubAssign_r, etc.

Just as A.opSubAssign(B)
is the operation  A -= B  or equivalently  A = A - B, similarly

A.opSubAssign_r(B)
would mean
A = B - A.
and would only occur when temporaries are generated in expressions. Like
-=, it's an operation which can frequently be performed very
efficiently, but at present the language has no way of expressing it.

Our original example then becomes:

T1 = B.opMul(C);
T1.opAddAssign(D);
T1.opMulAssign(E);
T1.opSubAssign_r(A);
X = T1;
... and all the useless temporaries are gone!

More formally, when the expression tree for an expression is generated:
With a binary operator XXX, operating on left & right nodes:

if (the left node is *not* an original leaf node) {
    // the left node is a temporary, does not need to be preserved.
    // we don't care if the right node is a temporary or not
    look for opXXXAssign().
} else if (the the right node is not an original leaf node) {
    // the right node is a temporary
    look for opXXXAssign_r()
} else {
   // both left and right nodes are leaf nodes, we have to
   // create a temporary
    look for opXXX(), just as it does now.
}

These rules also cope with the situation where temporaries are required:
eg
X = (A*B) + (C*D);
becomes
T1 = A*B;
T2 = C*D;
T1 += T2;
X = T1;

If this were implemented, it would permanently eradicate (for D) the
most significant advantage which Fortran has managed to retain over
object-oriented languages. And I really don't think it would be
difficult to implement, or have negative side-effects.

There are a couple of decisions to be made:
(I) should the compiler use opAdd() and generate a temporary, if
opAddAssign_r() doesn't exist, to preserve existing behaviour? I think
the answer to this is YES.
(II) should the compiler use opAdd() and generate a temporary, if
oppAddAssign() doesn't exist, to preserve existing behaviour? Again, I'm
inclined to answer YES.
(III) If the code includes +=, and there is an opAdd() but no
opAddAssign(), should the compiler accept this, and just generate an
opAdd() followed by an assignment?? This would mean that opAdd() would
generate the += operation as well as +, while opAddAssign() would be a
performance enhancement. (It would still be possible to have
opAddAssign() without opAdd(), to have += but not +, but it would not be
possible to have + without +=). This would mean that += would be
*purely* syntactic sugar.

Decision III would be a little more difficult to implement and is of
less obvious merit, I only mention it as a possibility.

Comments?
分享到:
评论

相关推荐

    D语言1.0规范(英文版和中文翻译)

    D语言是一门融合了C++、Java以及C#等众多语言优秀特性的新程序设计语言。它拥有Java和C#的高级编程特性以及快速... <br>让我们共同关注D语言的发展,详细内容请浏览作者的官方网站:http://www.digitalmars.com/d/

    DFeed:D新闻聚合器,新闻组客户端,Web新闻阅读器和IRC机器人

    进料DFeed是: NNTP客户端邮件列表档案类似于论坛的Web界面一个ATOM聚合器IRC机器人DFeed在和FreeNode的#d频道上运行。 快速入门指南: git clone --recursive ...

    D新闻聚合器,新闻组客户端,Web新闻阅读器和IRC bot-JavaScript开发

    IRC机器人DFeed在forum.dlang.org上运行,DFeed DFeed是:NNTP客户端,邮件列表存档类似论坛的Web界面一个ATOM聚合器,一个IRC机器人DFeed在forum.dlang.org和FreeNode上的#d频道上运行。 快速入门指南:git clone -...

    D程序设计语言教程中文版

    - **社区支持**:D语言有一个活跃的社区,包括新闻组news.digitalmars.com、D论坛等。 - **编译器**:早期版本支持Win32和x86 Linux平台,David Friedman为GCC编写了D语言前端。 #### 四、D语言与其他语言的比较 1...

    解决proteus的c编译器问题的方法.pdf

    1. 下载 Digital Mars C/C++ Compiler Version 8.51 从 http://www.digitalmars.com/download/freecompiler.html。 2. 下载 MASM32 汇编器,可以从 http://www.masm32.com/masmdl.htm 中的悉尼澳大利亚下载链接获取...

    D 语言编程参考手册 2[1].0(中).pdf

    #### 一、D语言概述 - **D语言**是一种现代、高性能的编程语言,旨在提供C++的强大功能同时简化语法和提高开发效率。 #### 二、版权与发行说明 - **版权归属**:本手册基于DMD官方手册制作,原稿版权属于...

    关于c++的编译器现在有很多

    其他编译器如Watcom C/C++(现为OpenWatcom)、DigitalMars、KAI C++等曾经在历史上占有一定地位,但现在已被其他编译器所取代或不再活跃。其中,DigitalMars C/C++的前身是Symantec C++,现在由其作者以开源形式...

    beaengine:BeaEngine Disasm项目

    BeaEngine是一个C库,旨在对来自16位,32位和64位intel架构的指令进行解码。 它包括标准指令集和FPU,MMX,SSE,SSE2,SSE3,SSSE3,SSE4.1,SSE4.2,VMX,CLMUL,AES,MPX,AVX,AVX2,AVX512(VEX和EVEX前缀),...

Global site tag (gtag.js) - Google Analytics