Skip to content

Conversation

stickies-v
Copy link
Contributor

@stickies-v stickies-v commented Nov 30, 2022

Inspired by MarcoFalke's comment. Main purpose of this PR is to minimize copying (potentially large) RPC named arguments when calling .substr() by using std::string_view instead of std::string. Furthermore, cleans up the code by removing unnecessary complexity in ParseNonRFCJSONValue() (done first to avoid refactoring required to concatenate string and string_view), updates some naming and adds a few test cases. Should not introduce any behaviour change.

Questions

  • Was there actually any merit to ParseNonRFCJSONValue() surrounding the value with brackets and then parsing it as an array? I don't see it, and the new approach doesn't fail any tests. Still a bit suspicious about it though.

@DrahtBot
Copy link
Contributor

DrahtBot commented Nov 30, 2022

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

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK ryanofsky, MarcoFalke
Approach ACK pablomartin4btc

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

Conflicts

No conflicts as of last run.

Since jgarzik/univalue#31, UniValue::read() can now
parse raw literals directly, so there is no more need to wrap them into an
array first.
Minimize copying RPC named argument values when calling .substr() by
using std::string_view instead of std::string.
@stickies-v stickies-v force-pushed the rpc-convert-string-view branch from d53953f to 545ff92 Compare January 18, 2023 17:10
@stickies-v stickies-v changed the title [WIP] refactor: RPC: pass named argument as string_view refactor: RPC: pass named argument value as string_view Jan 18, 2023
@stickies-v stickies-v marked this pull request as ready for review January 18, 2023 17:15
@stickies-v
Copy link
Contributor Author

Rebased now that #26506 is merged, ready for review!

Also added to PR description:

If there are no objections to 7727603, I think we should follow up with a PR to rename ParseNonRFCJSONValue() to a local Parse() function, remove it from client.h and merge the test coverage we currently have on ParseNonRFCJSONValue() with the coverage we have on UniValue::read().

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

Code review ACK 545ff92

I think the followup ideas in the PR description sound very good too

Copy link
Member

@pablomartin4btc pablomartin4btc left a comment

Choose a reason for hiding this comment

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

Approach ACK.

I've investigated why (if?) we would need to declare const std::string_view.
I see that while std::string_view is already read-only by design, declaring it as const can provide additional benefits in terms of communication, const correctness, and optimization but if we expect large values as @MarcoFalke mentioned on his comment perhaps we need to consider it.
This is a very interesting article on reasons to pass std::string_view by value, there are links to godbolt snippets where it compares machine code differences on using one or the other approach.

Is there any benchmark that you think I could run in order to get some stats in terms of gains in performance improvement?

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.

nice

review ACK 545ff92 📻

Show signature

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

review ACK 545ff924ab6303ffabd91fdfc4f0a4962daf133c 📻
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUhxDQwAnkUGV9vWw6I02mle/ZFfeIWLL4UhIJjNmcc0UH+0DwtK7IM9V74ST0QZ
8iv5AOTv43xJ9Ltye54nIbJnjIhW1WmZ/nhwTHDqJtQHP2GqF4ao2uQo2nGMEt/G
O3BixrcDyYHQDOR92bou9pzc7BOJpQ3Imhbq12FmQrVFSDsREF9wyMU0sPm7OypZ
ZIBSZc8/7n3KzGFd4qwBwQHKP0Do4vngE/PnuW+OF5u8k8+xe1kny4KBSadccpNl
eeO/VTeH2hip8pgBweKG7iuWRvHNtsEnrRdHIKLpTQh13l1L4VaRiz/GzkeGDEGf
YdRDOI9f5CT3slSC93hEKODOakmWVxQ/7dvprogcefNpZSq1jYIrK1uK/z1ZSJ1u
FuvPBv6WZ4AhJkdFA9jzLKEZ/Fr6Bur0qeXJNQdoa7CpHK3tsEJ5KqhhFbjhrxlF
vDlABZ6iVD2S4hKF7keu7et+cxMSxsB6VvTd/3tI46TxZqGlyv7bE2VuPBGyiNst
pioPNtzT
=pTHK
-----END PGP SIGNATURE-----

@fanquake fanquake merged commit 3b88c85 into bitcoin:master Mar 3, 2023
@fanquake
Copy link
Member

fanquake commented Mar 3, 2023

I think the followup ideas in the PR description sound very good too

@stickies-v will you follow up here?

@stickies-v stickies-v deleted the rpc-convert-string-view branch March 3, 2023 14:54
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Mar 3, 2023
@stickies-v
Copy link
Contributor Author

@stickies-v will you follow up here?

Done in #27256 (cc @ryanofsky)

fanquake added a commit to bitcoin-core/gui that referenced this pull request Jun 2, 2023
…f ParseNonRFCJSONValue() and rename it

cfbc8a6 refactor: rpc: hide and rename ParseNonRFCJSONValue() (stickies-v)
6c8bde6 test: move coverage on ParseNonRFCJSONValue() to UniValue::read() (stickies-v)

Pull request description:

  Follow-up to bitcoin/bitcoin#26612 (comment). As per bitcoin/bitcoin#26506 (review), `ParseNonRFCJSONValue()` is no longer necessary and we can use `UniValue::read()` directly:

  > IIRC before that PR UniValue::read could only parse JSON object and array values, but now it will parse string/number/boolean/null values as well. laanwj pointed this out in bitcoin/bitcoin#9028 (comment)

  The implementation of `ParseNonRFCJSONValue()` was already [simplified in #26612](https://github.com/bitcoin/bitcoin/pull/26612/files#diff-84c7a7f36362b9724c31e5dec9879b2f81eae0d0addbc9c0933c3558c577de65R259-R263)  and [test coverage updated](https://github.com/bitcoin/bitcoin/pull/26612/files#diff-fc0f86b6c3bb23b0e983bcf79d7546d1c9eaa15d6e4d8a7b03b5b85955f585abR292-R312) to ensure behaviour didn't change.

  To avoid code duplication, we keep the function to throw on invalid input data but rename it to `Parse()` and remove it from the header.

  The existing test coverage we had on `ParseNonRFCJSONValue()` is moved over to `UniValue::read()`.

ACKs for top commit:
  ryanofsky:
    Code review ACK cfbc8a6. Only change since last review is adding a new test

Tree-SHA512: 89be959d2353af7ace0c1348ba1600b9ac1d3c7b3cf7f0b59f6e005b7fb9d695ce3e8720e1be3cf77fe7e318a4017c880df108928e7179ec50447583d13bc781
@bitcoin bitcoin locked and limited conversation to collaborators Mar 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants