Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eth: Add CoinID flags. #1156

Merged
merged 6 commits into from
Aug 24, 2021
Merged

eth: Add CoinID flags. #1156

merged 6 commits into from
Aug 24, 2021

Conversation

JoeGruffins
Copy link
Member

Add TxIDFlag and SwapFlag in order to specify that a coin ID represents
either a txid or a swap. IsTxIDCoinID and IsSwapCoinID package methods
are included so that callers can easily discern if they have the right
type of coin ID for the operation they are performing.

related to #1021

@JoeGruffins JoeGruffins marked this pull request as ready for review August 12, 2021 07:54
@chappjc chappjc added the ETH label Aug 19, 2021
@JoeGruffins
Copy link
Member Author

JoeGruffins commented Aug 20, 2021

Using pointers to common.Address where possible. https://golang.org/doc/effective_go#arrays But more than that it bothered me to sorta use one or the other. Some of the ethclient methods from go-ethereum still take the value anyway.

@chappjc
Copy link
Member

chappjc commented Aug 20, 2021

Using pointers to common.Address where possible. https://golang.org/doc/effective_go#arrays But more than that it bothered me to sorta use one or the other. Some of the ethclient methods from go-ethereum still take the value anyway.

Probably go-ethereum works with value args in places for the sweeter syntax (less & or new usage) and because pointer vs value often conveys intent to modify in the function, but +1 to passing pointers to avoid needless allocations.

If we have a common.Address as a struct field however, we should probably stick to values for sake of data locality, but that's a separate concern.

server/asset/eth/common.go Outdated Show resolved Hide resolved
@JoeGruffins
Copy link
Member Author

If we have a common.Address as a struct field however, we should probably stick to values for sake of data locality, but that's a separate concern.

Do you have some reading material about this? We often store pointers in structs, and in some cases it seems to just be better to use pointers to avoid unexpected bugs like #1124

@chappjc chappjc self-requested a review August 20, 2021 16:20
@chappjc
Copy link
Member

chappjc commented Aug 20, 2021

If we have a common.Address as a struct field however, we should probably stick to values for sake of data locality, but that's a separate concern.

Do you have some reading material about this? We often store pointers in structs, and in some cases it seems to just be better to use pointers to avoid unexpected bugs like #1124

In that issue embedding of a struct by value had a unique set of gotchas, especially when it contained a mutex. Although I'm really thinking about fields that are simple types rather than structs. For a field that is a simple type like an array, it makes sense to have the memory for all of the struct's data, including the array, be contiguous, from a single allocation. I suppose it depends on the specific case though.

decred/dcrd@20a8ccc, although I don't uniformly object to passing small arrays by value.

@JoeGruffins
Copy link
Member Author

ty! Garbage collection is the main reason?

Add TxIDFlag and SwapFlag in order to specify that a coin ID represents
either a txid or a swap. IsTxIDCoinID and IsSwapCoinID package methods
are included so that callers can easily discern if they have the right
type of coin ID for the operation they are performing.
According to the golang docs, passing arrays by pointer is preferred as
it does not copy the array every time. Use where possible.
@chappjc
Copy link
Member

chappjc commented Aug 23, 2021

ty! Garbage collection is the main reason?

GC, but also memory access efficiency. Accessing adjacent memory addresses will often benefit from low-level cache lines, thus speeding up successive access to multiple fields. Go's 64-bit alignment of structs memory and elimination of excess padding for small (<8 byte) fields makes this even more likely. Optimizing for this in this application is surely not critical, but it's a reason.

Also if we have a pointer to an array as a struct field, that's a separate heap allocated object. Might as well have the array just be a segment of the struct's memory. Of course there can be reasons why this isn't desirable, but in general I feel it's cleanest to just avoid the extra level of indirection, esp for small arrays like Hash or this common.Address type. However if we find ourselves doing lots of unnecessary copies or obtaining pointers to these fields, we might reconsider this approach.

Copy link
Member

@chappjc chappjc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just a few thoughts.

server/asset/eth/common.go Outdated Show resolved Hide resolved
server/asset/eth/common.go Outdated Show resolved Hide resolved
server/asset/eth/common.go Outdated Show resolved Hide resolved
@chappjc chappjc merged commit f3f2f16 into decred:master Aug 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants