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

Remove version field from GetSerializeSize #28878

Merged
merged 5 commits into from
Nov 17, 2023

Conversation

ajtowns
Copy link
Contributor

@ajtowns ajtowns commented Nov 15, 2023

Drops the version field from GetSerializeSize(), simplifying the code in various places. Also drop GetSerializeSizeMany() (as just removing the version parameter could result in silent bugs) and remove unnecessary instances of #include <version.h>.

@DrahtBot
Copy link
Contributor

DrahtBot commented Nov 15, 2023

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK maflcko, theuni

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #28451 (refactor: Remove unused SER_DISK, SER_NETWORK, SER_GETHASH by maflcko)
  • #27006 (reduce cs_main scope, guard block index 'nFile' under a local mutex by furszy)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@theuni
Copy link
Member

theuni commented Nov 15, 2023

Concept ACK.

Is this next after #28438? Or something else first?

@theuni
Copy link
Member

theuni commented Nov 15, 2023

Playing around locally, I was also able to drop the include in:

$ git diff --stat
 src/bitcoin-util.cpp            | 1 -
 src/coins.cpp                   | 1 -
 src/core_read.cpp               | 1 -
 src/kernel/coinstats.cpp        | 1 -
 src/rest.cpp                    | 1 -
 src/script/bitcoinconsensus.cpp | 1 -
 src/zmq/zmqpublishnotifier.cpp  | 1 -

(I didn't mess with the tests, maybe could drop some there too).

Also worth pointing out: libbitcoinkernel now only requires version.h for signet.cpp, which I assume will go away in one of these follow-ups.

Edit: Not that the header dependency matters at all for libbitcoinkernel. It's just indicative imo that we're moving the right direction if it ends up un-tethered.

@ajtowns
Copy link
Contributor Author

ajtowns commented Nov 16, 2023

Rebased after #28438 was merged.

Is this next after #28438? Or something else first?

Either this or #28451 seem good to go; I think they conflict due to the "Convert some CDataStream.." commit here though; shouldn't be a hard rebase in either case though, I think.

Playing around locally, I was also able to drop the include in:

Taken.

Also worth pointing out: libbitcoinkernel now only requires version.h for signet.cpp, which I assume will go away in one of these follow-ups.

That just needs SpanReader and CVectorWriter to be updated to not need a version.

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

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

Some nits in the first commit. Otherwise:

ACK 83986f4 📒

Show signature

Signature:

untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
trusted comment: ACK 83986f464c59a6517f790a960a72574e167f3f72 📒
u/O7R7516VMRG5DuyOAXpyIHkMFCYUDfQH4PmkXGtkYoDV483ta0wAcinIAZ/Fijsk9h85VNKL9CG4+pCBLwAA==

@@ -450,7 +450,7 @@ inline unsigned int GetSizeOfVarInt(I n)
}

template<typename I>
inline void WriteVarInt(CSizeComputer& os, I n);
inline void WriteVarInt(SizeComputer& os, I n);
Copy link
Member

Choose a reason for hiding this comment

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

nit in efa9eb6: inline is implied by template and can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(leaving the nits for if it's otherwise necessary to retouch)

public:
explicit CSizeComputer(int nVersionIn) : nVersion(nVersionIn) {}
SizeComputer() {}
Copy link
Member

Choose a reason for hiding this comment

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

Why remove the explicit? Shouldn't matter in this context, but when in doubt, I'd prefer to keep it, unless there is a reason to remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

explicit is good for constructors that take a single argument to prevent accidental conversion, but seems useless for constructors that don't take an argument. (The real question is why leave a constructor there at all?)

@@ -1099,7 +1098,7 @@ class CSizeComputer
}

template<typename T>
CSizeComputer& operator<<(const T& obj)
SizeComputer& operator<<(const T& obj)
{
::Serialize(*this, obj);
return (*this);
Copy link
Member

Choose a reason for hiding this comment

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

unrelated nit in the first commit: Can remove the (), which achieve nothing in this context.

};

template<typename I>
inline void WriteVarInt(CSizeComputer &s, I n)
inline void WriteVarInt(SizeComputer &s, I n)
Copy link
Member

Choose a reason for hiding this comment

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

Same (inline)

@DrahtBot DrahtBot requested a review from theuni November 16, 2023 15:09
Copy link
Member

@theuni theuni left a comment

Choose a reason for hiding this comment

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

ACK 83986f4.

I started working on a patch to remove clientversion.h where it's no longer necessary after these changes, but it's just more work than it's worth for this.

Can we just agree to do a (client)version.h include cleanup at some point when the dust has settled on all this? :)

WriteCompactSize(s, GetSerializeSizeMany(s.GetVersion(), args...));
SizeComputer sizecomp;
SerializeMany(sizecomp, args...);
WriteCompactSize(s, sizecomp.size());
SerializeMany(s, args...);
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated to this PR, but I think we could make this much more efficient by adding a serializer override that keeps a running tally of serialized size? That way size is calculated as a side-effect of the actual serialization and prepended to the stream, rather than running all the way through twice.

No idea if this path is hot enough to bother messing with that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A running tally doesn't help -- we need to write the size of the serialized data before we serialize it, so this would mean caching the serialization until we've finished first which would likely be a bigger waste of memory than running things through the size computation first is a waste of cpu. (I've looked at this at least twice with exactly the same idea already 😄)

@fanquake fanquake merged commit 950af7c into bitcoin:master Nov 17, 2023
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done or Closed or Rethinking
Development

Successfully merging this pull request may close these issues.

None yet

5 participants