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 double decoding in unmarshalSingleValue #3018

Merged
merged 2 commits into from
Jul 10, 2023
Merged

Remove double decoding in unmarshalSingleValue #3018

merged 2 commits into from
Jul 10, 2023

Conversation

quasilyte
Copy link
Contributor

@quasilyte quasilyte commented Jul 10, 2023

Description

The callers of unmarshalSingleValue() always unmarshal the inputs of this function into json.RawMessage.

These json.RawMessage contain valid JSON data
that can be parsed directly. Doing an Unmarshal of json.RawMessage into json.RawMessage looks confusing and not very useful.

This change is mostly about readability and code clarity.

As a side note, it also improves sjson unmarshalling performance.

name                             old time/op    new time/op    delta
Array/array_all/UnmarshalJSON-8    34.6µs ± 1%    21.7µs ± 1%  -37.36%  (p=0.000 n=10+9)
Document/all/UnmarshalJSON-8        120µs ± 0%      77µs ± 1%  -36.04%  (p=0.000 n=9+10)
[Geo mean]                         64.5µs         40.8µs       -36.70%

name                             old speed      new speed      delta
Array/array_all/UnmarshalJSON-8  1.62MB/s ± 1%  2.58MB/s ± 1%        ~  (p=0.000 n=10+9)
Document/all/UnmarshalJSON-8     2.15MB/s ± 1%  3.37MB/s ± 1%  +56.37%  (p=0.000 n=9+10)
[Geo mean]                       1.87MB/s       2.95MB/s       +57.97%

name                             old alloc/op   new alloc/op   delta
Array/array_all/UnmarshalJSON-8    41.4kB ± 0%    22.0kB ± 0%  -46.92%  (p=0.000 n=10+10)
Document/all/UnmarshalJSON-8        117kB ± 0%      62kB ± 0%  -46.89%  (p=0.000 n=10+10)
[Geo mean]                         69.6kB         37.0kB       -46.91%

name                             old allocs/op  new allocs/op  delta
Array/array_all/UnmarshalJSON-8       184 ± 0%       119 ± 0%  -35.33%  (p=0.000 n=10+10)
Document/all/UnmarshalJSON-8          580 ± 0%       374 ± 0%  -35.52%  (p=0.000 n=10+10)
[Geo mean]                            327            211       -35.42%

Readiness checklist

  • I added/updated unit tests.
  • I added/updated integration/compatibility tests.
  • I added/updated comments and checked rendering.
  • I made spot refactorings.
  • I updated user documentation.
  • I ran task all, and it passed.
  • I ensured that PR title is good enough for the changelog.
  • (for maintainers only) I set Reviewers (@FerretDB/core), Labels, Project and project's Sprint fields.
  • I marked all done items in this checklist.

The callers of unmarshalSingleValue() always unmarshal
the inputs of this function into `json.RawMessage`.

These `json.RawMessage` contain a valid JSON data
that can be parsed directly. Doing an Unmarshal of
`json.RawMessage` into `json.RawMessage` looks confusing
and not very useful.

This change is mostly about the readability and code clarity.

As a side note, it also improves `sjson` unmarshalling performance.

    name                             old time/op    new time/op    delta
    Array/array_all/UnmarshalJSON-8    34.6µs ± 1%    21.7µs ± 1%  -37.36%  (p=0.000 n=10+9)
    Document/all/UnmarshalJSON-8        120µs ± 0%      77µs ± 1%  -36.04%  (p=0.000 n=9+10)
    [Geo mean]                         64.5µs         40.8µs       -36.70%

    name                             old speed      new speed      delta
    Array/array_all/UnmarshalJSON-8  1.62MB/s ± 1%  2.58MB/s ± 1%        ~  (p=0.000 n=10+9)
    Document/all/UnmarshalJSON-8     2.15MB/s ± 1%  3.37MB/s ± 1%  +56.37%  (p=0.000 n=9+10)
    [Geo mean]                       1.87MB/s       2.95MB/s       +57.97%

    name                             old alloc/op   new alloc/op   delta
    Array/array_all/UnmarshalJSON-8    41.4kB ± 0%    22.0kB ± 0%  -46.92%  (p=0.000 n=10+10)
    Document/all/UnmarshalJSON-8        117kB ± 0%      62kB ± 0%  -46.89%  (p=0.000 n=10+10)
    [Geo mean]                         69.6kB         37.0kB       -46.91%

    name                             old allocs/op  new allocs/op  delta
    Array/array_all/UnmarshalJSON-8       184 ± 0%       119 ± 0%  -35.33%  (p=0.000 n=10+10)
    Document/all/UnmarshalJSON-8          580 ± 0%       374 ± 0%  -35.52%  (p=0.000 n=10+10)
    [Geo mean]                            327            211       -35.42%
@quasilyte quasilyte requested a review from a team as a code owner July 10, 2023 12:03
@quasilyte quasilyte requested review from AlekSi, chilagrow, a team, rumyantseva and noisersup July 10, 2023 12:03
@quasilyte quasilyte self-assigned this Jul 10, 2023
@quasilyte quasilyte added the code/chore Code maintenance improvements label Jul 10, 2023
@quasilyte quasilyte enabled auto-merge (squash) July 10, 2023 12:05
@codecov
Copy link

codecov bot commented Jul 10, 2023

Codecov Report

Merging #3018 (40bec3f) into main (c35473a) will increase coverage by 3.50%.
The diff coverage is 100.00%.

❗ Current head 40bec3f differs from pull request most recent head a4075e9. Consider uploading reports for the commit a4075e9 to get more accurate results

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3018      +/-   ##
==========================================
+ Coverage   72.72%   76.22%   +3.50%     
==========================================
  Files         382      385       +3     
  Lines       20815    21060     +245     
==========================================
+ Hits        15137    16054     +917     
+ Misses       4730     4080     -650     
+ Partials      948      926      -22     
Impacted Files Coverage Δ
internal/handlers/sjson/sjson.go 83.47% <100.00%> (+14.31%) ⬆️

... and 82 files with indirect coverage changes

Flag Coverage Δ
hana ?
integration 72.74% <100.00%> (+0.02%) ⬆️
mongodb 5.34% <0.00%> (+<0.01%) ⬆️
pg 66.22% <100.00%> (+0.01%) ⬆️
shard-1 55.22% <100.00%> (-0.08%) ⬇️
shard-2 55.45% <100.00%> (+0.12%) ⬆️
shard-3 56.71% <100.00%> (+0.08%) ⬆️
sqlite 45.93% <100.00%> (+<0.01%) ⬆️
unit 24.24% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

@quasilyte quasilyte changed the title Remove double decoding in unmarshalSingleValue() Remove double decoding in unmarshalSingleValue Jul 10, 2023
@quasilyte quasilyte disabled auto-merge July 10, 2023 12:08
@quasilyte quasilyte enabled auto-merge (squash) July 10, 2023 12:08
Copy link
Contributor

@rumyantseva rumyantseva left a comment

Choose a reason for hiding this comment

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

Based on where and how unmarshalSingleValue is used, the changes look good to me (the callers already call checkConsumed and provide json.RawMessage, so no need to do it again). Nice catch!

@AlekSi AlekSi added this to the Next milestone Jul 10, 2023
@quasilyte quasilyte merged commit 7d70c74 into FerretDB:main Jul 10, 2023
@quasilyte quasilyte deleted the quasilyte/remove_double_decoding branch July 11, 2023 07:42
@AlekSi AlekSi added code/enhancement Some user-visible feature could work better and removed code/chore Code maintenance improvements labels Jul 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code/enhancement Some user-visible feature could work better
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants