Closed YohDeadfall closed 1 year ago
if you're saying "it make zero sense to pass spans by value", shouldn't the title of the commit msg actually be "Stop passing spans by value"? Otherwise I don't get it.
My bad, spans must be passed by value. Fixed the description, thanks!
Maybe passing spans by reference, even if not replaced, makes performance better? This way there's no copy needed to be done.
Maybe passing spans by reference, even if not replaced, makes performance better? This way there's no copy needed to be done.
Maybe, but a Span is a kind of reference to its underlying data, and is designed to be lightweight to copy. Isn't there a slight overhead when there is a reference because it needs to maintain that link. All in all there's probably negligent overhead difference in either option.
I'd be more worried if the Span was being replaced in the containing method, bad practice, but could still introduce bugs here/ @YohDeadfall You didn't happen to notice any instance of this right?
Maybe passing spans by reference, even if not replaced, makes performance better? This way there's no copy needed to be done.
Spans are two machine word wide value types, so it can be passed through two registers which is dirty cheap. Passing it by a reference leads to double memory reads instead of a single to which the span points too, and because of that it's allocated on the stack while it can be stored in registers completely.
The same applies to passing an array by a reference.
I'd be more worried if the Span was being replaced in the containing method, bad practice, but could still introduce bugs here/ @YohDeadfall You didn't happen to notice any instance of this right?
Had the same thought, so checked all methods to be sure that it's not a case. Check NBitcoin/BitcoinStream.cs
to be sure that everything is fine there.
Ran tests and it looks fine, no failures.
This code break untested stuff from shitcoin.
While I agree byref span doesn't make sense, byref array are used because the .ReadWrite
is responsible to create the array.
@YohDeadfall sorry if I am slow, don't hesitate to track me on twitter or the btcpayserver chat if I miss it. I'm so underwater with notifications that I turned them all off :p
While I agree byref span doesn't make sense, byref array are used because the .ReadWrite is responsible to create the array.
I was worried by that too, but according to following code there are now allocations:
Could you point me where exactly I broke code?
you are right, as said on twitter DM, can you remove from BitcoinStream the methods with ref
not making sense?
I guess they did made sense in the past, but some refactoring made them irrelevant.
There's a method for List<T>
serialization ad deserialization, but I wouldn't touch it without improving the design of the whole library and bringing nullability. So, for this pull request I have no more changes.
It makes zero sense to pass spans by references if that span isn't replaced by another one (I mean changing the address it points to). The same applies to arrays as well.