Code sample that shows casting to uint is more efficient than range check

后端 未结 3 1424
梦如初夏
梦如初夏 2020-12-20 17:39

So I am looking at this question and the general consensus is that uint cast version is more efficient than range check with 0. Since the code is also in MS\'s implementatio

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-20 18:01

    You aren't comparing like with like.

    The code you were talking about not only saved one branch by using the optimisation, but also 4 bytes of CIL in a small method.

    In a small method 4 bytes can be the difference in being inlined and not being inlined.

    And if the method calling that method is also written to be small, then that can mean two (or more) method calls are jitted as one piece of inline code.

    And maybe some of it is then, because it is inline and available for analysis by the jitter, optimised further again.

    The real difference is not between index < 0 || index >= _size and (uint)index >= (uint)_size, but between code that has repeated efforts to minimise the method body size and code that does not. Look for example at how another method is used to throw the exception if necessary, further shaving off a couple of bytes of CIL.

    (And no, that's not to say that I think all methods should be written like that, but there certainly can be performance differences when one does).

提交回复
热议问题