简体   繁体   English

C#优化重载运算符

[英]C# optimizing overloaded operators

Returning a new object on overloaded operators seems the only thing to do. 在重载的运算符上返回新对象似乎是唯一要做的事情。 Right? 对?

  • If you have a class (X) that contains a large buffer 如果您的类(X)包含较大的缓冲区
  • Part of the buffer contains content, rest is free. 缓冲区的一部分包含内容,其余部分是免费的。
  • You have overloaded the binary + operator to merge two objects (of same type) by creating a new object containing the appended contents of both operands. 通过创建一个包含两个操作数的附加内容的新对象,您已经使Binary +运算符过载以合并两个(相同类型的)对象。

In other words: 换一种说法:

z = x + y // z is a new object containing the appended data from x and y. 

This is obviously good because we don't want to mangle x or y during the operation. 这显然很好,因为我们不想在操作过程中操纵x或y。 However, C# language specs 7.2.2 say: 但是,C#语言规范7.2.2说:

When a binary operator is overloaded, the corresponding assignment operator (if any) is also implicitly overloaded. 当二进制运算符重载时,相应的赋值运算符(如果有)也将隐式重载。

this means that: 这意味着:

x += y;  // will create a new x, copy contents of old x merged with y 
         //  and the old x will be garbage collected at some point

This is painful, because it could be much faster to just copy the contents of y in the free portion of x. 这很痛苦,因为仅将y的内容复制到x的自由部分中可能会更快。 That way we don't copy a large object and leave a mess for the nice garbage collector person. 这样一来,我们就不会复制大对象,也不会为好垃圾收集者留下麻烦。

I realize that this could be done with a simple x.Add(y). 我意识到这可以通过简单的x.Add(y)来完成。 I am curious if there is a way to do it using operators. 我很好奇是否有一种使用操作符的方法。 for example to figure out we are inside the assignment operator and perform the optimized routine. 例如,弄清楚我们在赋值运算符内部并执行优化的例程。

I personally like how C# implemented this. 我个人喜欢C#如何实现这一点。

It's more "obvious" that: 更“明显”的是:

x += y;

and

x = x + y;

Should have the same behavior. 应该有相同的行为。 In C#, this is enforced. 在C#中,这是强制性的。 With separate overloads, people could abuse the operator and prevent this from acting the same. 对于单独的过载,人们可能会滥用操作员并阻止其采取同样的行动。

The perf difference will be so minimal, and this also makes it easier to implement and maintain, so I strongly prefer the C# approach. 性能差异将非常小,这也使得它更易于实现和维护,因此我强烈喜欢C#方法。

All mathematical binary operators returns a new object. 所有数学二进制运算符都返回一个新对象。 Eg You wouldn't want 2+3 to alter the value of one of Them to five just because you were adding them. 例如,您不希望2 + 3只是将它们中的一个加到5而将其中一个的值更改为5。 In other words the semantics of the operators in question is very clear and enforced through the specs. 换句话说,所讨论的运算符的语义非常清晰,并通过规范进行了强制执行。 If you want something different you'll have to make something different :). 如果您想要不同的东西,则必须做出不同的:)。 And I personaly like it compare to c++ where you Can make overloading such the A=B=C could result in C being deleted, B being unassigned and A equaling any value with no regards to either C or B 我个人比较喜欢它与c ++的比较,在c ++中,您可以进行重载,例如A = B = C可能导致C被删除,B被取消分配并且A等于任何值,而与C或B无关

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM