Skip to content

Array move constructor of basic_json copies provided array. #355

@avautour

Description

@avautour

I am using the library to write some GeoJSON that can have fairly large geometries with several coordinates. When doing performance profiling, I noticed some json::array copies that I did not expect to see.

When I call basic_json(array&&), I expect the array to be moved, and not copied.

When I call basic_json(array&&), the array is moved, and then copied in array_storage.

Enumerate the steps to reproduce the bug
Create a json object.
Create an array.
Assigning the array to the object by moving it

Include a small, self-contained example if possible

jsoncons::json someObject;
jsoncons::json::array someArray(4);
someArray[0] = 0;
someArray[1] = 1;
someArray[2] = 2;
someArray[3] = 3;

// This will end-up making a copy the array, which I did not expect.
someObject.insert_or_assign("someKey", std::move(someArray));

I can work-around the problem by doing this instead:

auto result = someObject.insert_or_assign("someKey", jsoncons::json::array()));
result.first->value().array_value().swap(someArray);

What compiler, architecture, and operating system?

  • Compiler: MSVC2017
  • Architecture (e.g. x86, x64): x64
  • Operating system: Windows 10

What jsoncons library version?

  • Latest release 0.168.4
  • Other release 0.168.1
  • master

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions