PRUNERS / FLiT

A project to quickly detect discrepancies in floating point computation across hardware, compilers, libraries and software.
Other
36 stars 6 forks source link

Replace Variant with JSON #300

Open mikebentley15 opened 4 years ago

mikebentley15 commented 4 years ago

Feature Request

Describe the new feature: So far, we have implemented our own flit::Variant type that can take on different types of values. This has proven to be difficult to add new types and time consuming to develop and test. Now that we have it, it is stable and works well.

We see that many of the goals and types supported by flit::Variant are also supported by JSON. The JSON type is quite standard and found everywhere. It may be good to move to having JSON be our backend type to add more flexibility and stability. With a JSON type, you can have dictionaries, lists, and compose them in each other seamlessly. This would add significant capabilities to the use of FLiT.

Suggested change: I propose that we specifically use this implementation of JSON in C++:

https://github.com/nlohmann/json

for the following reasons:

It may not be the fastest implementation of JSON in C++, but ease of use and a single file that can be copied into the FLiT repository are enticing.

We could incorporate a JSON library in the following ways:

  1. Allow json objects to be another type stored inside of flit::Variant
    • The least amount of obtrusiveness.
    • Supplies backward compatibility with both source code and output *.dat files
  2. Change flit::Variant to use json as its backend implementation
    • Lightly breaks backward compatibility of the *.dat output files
    • Perhaps json.hpp could be modified to allow long double types
  3. Replace flit::Variant with json to be returned from flit tests
    • Significantly breaks backward compatibility requiring change of test source code
    • Would increase the stability of FLiT and perhaps make it easier to use
    • Perhaps json.hpp could be modified to allow long double types

Note: This library is under the MIT license. I recommend moving to a 3rdparty directory along with tinydir.h. Since we would only copy over the single header file (of 22,000 lines of code), we would not need to copy over the LICENSE file since it has the license declaration at the beginning of the file.

Alternative approaches: One thing we could do is implement our own JSON parser and output. We would be able to customize the output to also support long double and float.

Besides making our own implementation, there are many other JSON libraries out there for use by C++. Here are some choices:

This would add another external dependency rather than just a single header file pulled in.

If speed is considered, here is a benchmark to compare 41 different implementations. It seems rapidjson is one of the fastest with a large conformance (significantly faster than others).