Changing &var[0] to var.data() #10793

Open
wants to merge 1 commit into
from

Conversation

Projects
None yet
5 participants

This just continues the work of #9804

Modifies a lot of &vector[]'s to vector.data()'s across all the files including tests, just the stuff that 9804 missed

laanwj added the Refactoring label Jul 11, 2017

Contributor

promag commented Jul 12, 2017

#9804 should cherry pick this one right?

#9804 has just been merged

src/rpc/server.cpp
@@ -444,7 +444,7 @@ static inline JSONRPCRequest transformNamedArguments(const JSONRPCRequest& in, c
const std::vector<UniValue>& values = in.params.getValues();
std::unordered_map<std::string, const UniValue*> argsIn;
for (size_t i=0; i<keys.size(); ++i) {
- argsIn[keys[i]] = &values[i];
+ argsIn[keys[i]] = values.data() + i;
@jtimon

jtimon Jul 13, 2017

Member

Is this really clearer?

- memcpy(&data[0], prevOutPoint.hash.begin(), 32);
- memcpy(&data[32], &prevOutPoint.n, sizeof(unsigned int));
+ memcpy(data.data(), prevOutPoint.hash.begin(), 32);
+ memcpy(data.data()+32, &prevOutPoint.n, sizeof(unsigned int));
@jtimon

jtimon Jul 13, 2017

Member

same here

@dcousens

dcousens Jul 13, 2017 edited

Contributor

This is inline with #9804, and prevents U/B if data is empty

src/test/skiplist_tests.cpp
@@ -20,13 +20,13 @@ BOOST_AUTO_TEST_CASE(skiplist_test)
for (int i=0; i<SKIPLIST_LENGTH; i++) {
vIndex[i].nHeight = i;
- vIndex[i].pprev = (i == 0) ? NULL : &vIndex[i - 1];
+ vIndex[i].pprev = (i == 0) ? NULL : (vIndex.data() + i - 1);
@jtimon

jtimon Jul 13, 2017

Member

same here

src/test/skiplist_tests.cpp
vIndex[i].BuildSkip();
}
for (int i=0; i<SKIPLIST_LENGTH; i++) {
if (i > 0) {
- BOOST_CHECK(vIndex[i].pskip == &vIndex[vIndex[i].pskip->nHeight]);
+ BOOST_CHECK(vIndex[i].pskip == vIndex.data() + vIndex[i].pskip->nHeight);
@jtimon

jtimon Jul 13, 2017

Member

here you aren't even consistent

@dcousens

dcousens Jul 13, 2017

Contributor

what lacks consistency?

@jtimon

jtimon Jul 13, 2017

Member

One time vIndex is accessed by vIndex.data() + n and another with vIndex[m]

@MeshCollider

MeshCollider Jul 13, 2017

That's because one is accessing the element not a pointer...

@dcousens

dcousens Jul 13, 2017 edited

Contributor

The .data() usage here however, is not really inline with #9804.
#9804 replaced usage of &v[0] and &v[N] for specifying a slice/range.
In the change here, you are specifically referring to the memory location of 1 element, which isn't an rvalue, probably unnecessary to use .data().

@MeshCollider

MeshCollider Jul 13, 2017

Isn't it still preferable to access the memory location of an element this way?

@dcousens

dcousens Jul 14, 2017 edited

Contributor

@MeshCollider the advantage of .data() is that it is defined to return nullptr if .empty(), compared to &v[0] potentially de-referencing unitialized memory.
In the above, we assume vIndex is not empty, as you are de-referencing vIndex[i], therefore, it is probably not as necessary.
As far as the pattern goes, #9804 introduced .data() specifically where &v[0] was used to target the front of an array, which may have been uninitialized.
That is not the case here.

src/test/skiplist_tests.cpp
- BOOST_CHECK(vIndex[SKIPLIST_LENGTH - 1].GetAncestor(from) == &vIndex[from]);
- BOOST_CHECK(vIndex[from].GetAncestor(to) == &vIndex[to]);
- BOOST_CHECK(vIndex[from].GetAncestor(0) == &vIndex[0]);
+ BOOST_CHECK(vIndex[SKIPLIST_LENGTH - 1].GetAncestor(from) == vIndex.data() + from);
@jtimon

jtimon Jul 13, 2017

Member

well, you get the idea, if the index was other than 0, it seems to me that the change makes things worse.

Member

jtimon commented Jul 13, 2017

Fast review ACK modulo nits.

@jtimon this is consistent with how #9804 did it

Reverted the changes where specific elements were accessed, thanks @jtimon and @dcousens

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment