Skip to content

Add functionality to detect big integers - #2115

Closed
saleyn wants to merge 14 commits into
simdjson:masterfrom
saleyn:parse-number
Closed

saleyn wants to merge 14 commits into
simdjson:masterfrom
saleyn:parse-number

Conversation

@saleyn

@saleyn saleyn commented Jan 31, 2024

Copy link
Copy Markdown
Contributor

This extention adds the ability to identify the cause of number parsing failure due to a number not fitting in the 64-bit space.

@lemire

lemire commented Jan 31, 2024

Copy link
Copy Markdown
Member

It is a good idea, but adding a boolean is not going to be nice.

Here is my recommendation.

Drop the bigint Boolean. Undo your work.

Then add something like...

#define BIGINT_NUMBER(SRC) (found_invalid_number((SRC)), BIGINT_ERROR)

near were #define INVALID_NUMBER appears.

Then, at a couple of places, replace INVALID_NUMBER by BIGINT_NUMBER.

And...

    { BIGINT_ERROR, "BIGINT_ERROR: Big integer value that cannot be represented using 64 bits" },

in

{ NUMBER_ERROR, "NUMBER_ERROR: Problem while parsing a number" },

Doing so will make some of our tests fail, but it should easy to fix.

The documentation may need to be updated slightly.

It should not be very difficult.

It will be a great feature!!!

@lemire lemire left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a superb contribution, but what we want is to return a different error (not NUMBER_ERROR). Adding a new bool and changing function signatures is unnecessary and burdensome.

@lemire lemire added enhancement New feature or request documentation labels Jan 31, 2024
@lemire lemire mentioned this pull request Jan 31, 2024
@saleyn

saleyn commented Feb 1, 2024

Copy link
Copy Markdown
Contributor Author

It is a good idea, but adding a boolean is not going to be nice.

Here is my recommendation.

Drop the bigint Boolean. Undo your work.

Then add something like...

#define BIGINT_NUMBER(SRC) (found_invalid_number((SRC)), BIGINT_ERROR)

This is pretty much what I initially did, but then thought it would present backward compatibility issues, so I scratched that and introduced this less intrusive backward-compatible version. :) So, if you are ok with the appoach you suggested, I can revert to my prior changes.

@lemire

lemire commented Feb 1, 2024

Copy link
Copy Markdown
Member

It is a change but we never committed to a fixed set of error codes. We shall warn our users.

@saleyn

saleyn commented Feb 1, 2024

Copy link
Copy Markdown
Contributor Author

LMK what you think of this version

@lemire

lemire commented Feb 1, 2024

Copy link
Copy Markdown
Member

I like it! It is very clean.

@saleyn Can you check the failing tests? I think that these are expected. They relate to the fact that the change is breaking in some sense.

@saleyn

saleyn commented Feb 1, 2024 •

Copy link
Copy Markdown
Contributor Author

@lemire, I have a question about value_iterator::is_root_integer(), and value_interator::get_root_number(): why does the value_interator copy the json string to a tmpbuf rather than using start and end pointers of the value string to the source json?

@lemire

lemire commented Feb 1, 2024

Copy link
Copy Markdown
Member

rather than using start and end pointers of the value string to the source json

We generally handle the special case where the document is a scalar value (string, number, null, etc.) in this manner. We create a temporary buffer, we copy the data to the buffer and parse it from the buffer. It is very uncommon that documents take this form, so we do not care much about the performance. Using a temporary buffer ensures that we can use the same code we generally use, instead of having to deal with two different code paths... We just want to reduce our maintenance and make bugs less likely.

@saleyn

saleyn commented Feb 1, 2024

Copy link
Copy Markdown
Contributor Author

The reason I am asking is because in case of a big integer we don't know the size of the buffer upfront, and it wouldn't be possible to preset that statically on the stack. Would it be ok to use the pointers in that case to the source json, or you suggest otherwise?

@lemire

lemire commented Feb 1, 2024

Copy link
Copy Markdown
Member

Yes.

@lemire

lemire commented Feb 5, 2024

Copy link
Copy Markdown
Member

@saleyn I am planning a release soon, if you ever get some time to finish this PR, we could include it.

@saleyn

saleyn commented Feb 6, 2024

Copy link
Copy Markdown
Contributor Author

Thanks for letting me know. I'll try to finish it within a week or so.

@saleyn

saleyn commented Feb 19, 2024

Copy link
Copy Markdown
Contributor Author

@lemire, I've been traveling for the past few days and didn't get to finish the changes in PR yet. Hopefully will do by the end of this week.

@lemire

lemire commented Feb 19, 2024

Copy link
Copy Markdown
Member

@saleyn That's ok. We have a bunch of new changes, and I'll wait for your component for the release.

@saleyn

saleyn commented Feb 23, 2024

Copy link
Copy Markdown
Contributor Author

I completed the necessary changes. The tests pass, however, @lemire, do you mind taking a look at why the CIFuzz is failing? I am uncertain about the cause.

@lemire

lemire commented Feb 23, 2024

Copy link
Copy Markdown
Member

@saleyn I'll work on it today.

I am planning on a release soon after.

@saleyn

saleyn commented Feb 23, 2024

Copy link
Copy Markdown
Contributor Author

I force pushed a small update.

Comment thread fuzz/fuzz_dump.cpp
os << element.get_uint64().value_unsafe() << endl;
break;
case simdjson::dom::element_type::BIGINT:
os << element.get_bigint().value_unsafe() << endl;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This appears to be unsafe according to our CI.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think I understand the root cause. Let me see.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm working on it too.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think your fix is correct. The issue here is where do you store the data in the DOM model. You cannot rely on the document being around.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The other problem is that a bigint_t is not a 64-bit type, so the payload union is incorrect.

Adding bigint support to the DOM API requires more work.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, the commit below was my initial thought this morning, but right after that I realized the problem was related to the code inside tape_writer::append_bigint(...), which you also noted down below. I haven't had time this morning to investigate it further. Will try to check this tonight.

Comment thread include/simdjson/base.h Resolved
Comment thread include/simdjson/dom/element.h Resolved
Comment thread include/simdjson/dom/element.h Resolved
Comment thread include/simdjson/dom/element.h Resolved
Comment thread include/simdjson/dom/element.h Resolved
Comment thread src/generic/stage2/tape_writer.h Resolved
}

simdjson_inline void tape_writer::append_bigint(const uint8_t *const src, const uint8_t *const end) noexcept {
append(end - src, internal::tape_type::BIGINT);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Where is the bigint written? Note that the DOM tape is meant to be complete, it does not include a reference to the original document. You could write it to the string buffer, but as far as I can tell, it is not done now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks! This morning I also saw that this part was incorrect.

Comment thread singleheader/simdjson.h Resolved
Comment thread include/simdjson/generic/base.h Resolved
double floating_point_number;
int64_t signed_integer;
uint64_t unsigned_integer;
bigint_t big_integer;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The union is meant to span 64-bit. A bigint_t is not a 64-bit data structure.

It is also very much unlike the other values because it requires a variable-length amount of memory (possibly on the string buffer).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I see. The 64bit union size restriction wasn't obvious by reading the code, which is why I thought that it didn't matter. Where is it in the code that expects the size of the number struct to be 64 bits?

@lemire lemire Feb 24, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Where is it in the code that expects the size of the number struct to be 64 bits?

The union is at least as big as necessary to hold its largest data member. Currently, all values use 64 bits so there is no waste. By introducing a type that is almost surely larger (I expect an std::string_view to span 16 bytes typically), the number type is thus made larger.

How this affects the code and its performance is hard to tell in the abstract, but we cannot assume that it is free. It could be free, but we have to prove that it is so.

We should try to optimize for the common case. And the common case is that there is no big integer. We do not want everyone to be paying the price for the uncommon cases when there are big integers...

Now, it is possible that your design is correct and that there is no trade-off but my instinct is to make the big integer case exceptional.

In any case, we have to remember that the big integer type is not usable as is. That is, you get a string-view. You are still going to need to parse it into a usable type. So it is bound to be a special case.

In any case, we need to justify the design and make sure we don't make everyone's code slower for a feature that very few people will ever use. That is not meant to be discouraging... for some people, your implementation might be absolutely super useful... but we don't want to trade that for slower performance everywhere else.

@lemire lemire left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall, this is pretty good code and the ideas are quite nice, but it raises questions.

The initial PR memory detected the big integers and reported a corresponding error. This was non-invasive and relatively easy. This left the parse_number function unchanged. For users relying on on-demand, the big-integer error can be followed by a token access and so we could support big integer in on-demand in a manner that is functionally equivalent to the one in this PR, but with far fewer changes to the core library.

I would recommend you still consider this approach due to its simplicity.

More recently, the scope changed significantly, with the introduction of a bigint_t type which is essentially an std::string_view. The DOM tape format is also changed by this PR, but the tape.md file documenting the format was not changed, so it is not clear whether the format is correct. Importantly, we need for the data to appear somewhere in the tape data structure, possibly copied to the string buffer.

The PR also appears to introduce significant changes to the parse_number function. This might be fine, but we cannot merge it without assessing the effect on performance.

Nevertheless, we can adopt the more ambitious approach. It will require more effort, however.

Please consider the following tasks:

  1. The new tape format needs to be documented. Please edit the tape.md file and explain how a big int is stored.
  2. I don't think that the payload union can include bigint_t. I think that the design is wrong given how different a big int is.
  3. We need before/after benchmarks. Please follow the following instructions (I am assuming that you are under Linux, macOS or a similar system):
cmake -B build -D SIMDJSON_DEVELOPER_MODE=ON
cmake --build build --target bench_ondemand --target parse
./build/benchmark/dom/parse jsonexamples/twitter.json
./build/benchmark/bench_ondemand --benchmark_filter="kostya<simdjson"

Build and run the benchmarks before and after the changes. Even better, if you can, run them as a privileged user so that we can get performance counters.

Ideally, we should not see a performance penalty as a result of this PR when big integers are not encountered. That is, we don't want to pay for supporting big integers when they are not present.

Importantly, also, we do not want to introduce new bugs. Thus I have a fourth task:

  1. We need more extensive dedicated tests regarding big integers.

@saleyn

saleyn commented Feb 24, 2024

Copy link
Copy Markdown
Contributor Author

@lemire, thank you for your extensive comments!

The initial PR memory detected the big integers and reported a corresponding error. This was non-invasive and relatively easy. This left the parse_number function unchanged. For users relying on on-demand, the big-integer error can be followed by a token access and so we could support big integer in on-demand in a manner that is functionally equivalent to the one in this PR, but with far fewer changes to the core library.

I would recommend you still consider this approach due to its simplicity.

Unfortunately with the recent traveling and timing gaps in working on this PR I can't recall what issue I ran into that forced me to change the direction and implement the more intrusive approach. :(

I could try to revert and go back to that original approach and then attempt to do this one incrementally. However, I have another business trip abroad next week and will be out for two more weeks. So, won't be able to focus on this code for a while.

More recently, the scope changed significantly, with the introduction of a bigint_t type which is essentially an std::string_view. The DOM tape format is also changed by this PR, but the tape.md file documenting the format was not changed, so it is not clear whether the format is correct. Importantly, we need for the data to appear somewhere in the tape data structure, possibly copied to the string buffer.

That was my oversight. I'll modify it tonight.

The PR also appears to introduce significant changes to the parse_number function. This might be fine, but we cannot merge it without assessing the effect on performance.

Nevertheless, we can adopt the more ambitious approach. It will require more effort, however.

Please consider the following tasks:

  1. The new tape format needs to be documented. Please edit the tape.md file and explain how a big int is stored.
  2. I don't think that the payload union can include bigint_t. I think that the design is wrong given how different a big int is.
  3. We need before/after benchmarks. Please follow the following instructions (I am assuming that you are under Linux, macOS or a similar system)

Will do.

  1. We need more extensive dedicated tests regarding big integers.

I added one such test to the ondemand_number_tests.cpp, but realize that more cases are needed. Perhaps the bigint support could be considered experimental and given the time constraints the testing work could be done incrementally?

@lemire

lemire commented Feb 24, 2024

Copy link
Copy Markdown
Member

@saleyn You may need to sync with our main branch for the tests to run. :-)

@lemire

lemire commented Feb 24, 2024

Copy link
Copy Markdown
Member

@saleyn

Perhaps the bigint support could be considered experimental and given the time constraints the testing work could be done incrementally?

Yes. This is fine... in theory... but the PR introduces a change in the front-facing API that might affect many people. The library used to reject some numbers, now it will accept them. This can break code.

It is not like we are offering a way to opt-in. You get big integers whether you want them or not. So it should be pretty good otherwise we might break systems.

@saleyn

saleyn commented Feb 24, 2024 •

Copy link
Copy Markdown
Contributor Author

Benchmark results

DOM parser

NOTE: the values differ from run to run, but the master branch does have a slight edge. Let me know if you consider these results significant. The main question is if using a 128 bits number struct is still prohibitive with these results? Also please LMK if you consider these results ok but still feel that bigint_t doesn't belong to the union in the number struct.

  1. master branch:
$ build/benchmark/dom/parse jsonexamples/twitter.json
number of iterations 200

jsonexamples/twitter.json
=========================
     9867 blocks -     631515 bytes - 55262 structurals (  8.8 %)
special blocks with: utf8      2284 ( 23.1 %) - escape       598 (  6.1 %) - 0 structurals      1287 ( 13.0 %) - 1+ structurals      8581 ( 87.0 %) - 8+ structurals      3272 ( 33.2 %) - 16+ structurals         0 (  0.0 %)
special block flips: utf8      1104 ( 11.2 %) - escape       642 (  6.5 %) - 0 structurals       940 (  9.5 %) - 1+ structurals       940 (  9.5 %) - 8+ structurals      2593 ( 26.3 %) - 16+ structurals         0 (  0.0 %)

All Stages (excluding allocation)
|    Speed        :  24.9596 ns per block ( 94.59%) -   0.3900 ns per byte -   4.4570 ns per structural -   2.5640 GB/s
|    Cycles       :  99.8009 per block    ( 95.87%) -   1.5595 per byte    -  17.8212 per structural    -    3.999 GHz est. frequency
|    Instructions : 288.1751 per block    (100.00%) -   4.5030 per byte    -  51.4587 per structural    -    2.888 per cycle
|    Misses       :    5279 branch misses ( 97.39%) - 79 cache misses (  5.58%) - 78367.00 cache references
|- Stage 1
|    Speed        :  11.8565 ns per block ( 44.93%) -   0.1853 ns per byte -   2.1172 ns per structural -   5.3976 GB/s
|    Cycles       :  47.4002 per block    ( 45.53%) -   0.7407 per byte    -   8.4641 per structural    -    3.998 GHz est. frequency
|    Instructions : 140.0335 per block    ( 48.59%) -   2.1882 per byte    -  25.0054 per structural    -    2.954 per cycle
|    Misses       :    3250 branch misses ( 59.96%) - 55 cache misses (  3.88%) - 27413.00 cache references
|- Stage 2
|    Speed        :  12.9916 ns per block ( 49.23%) -   0.2030 ns per byte -   2.3199 ns per structural -   4.9260 GB/s
|    Cycles       :  51.9425 per block    ( 49.90%) -   0.8116 per byte    -   9.2753 per structural    -    3.998 GHz est. frequency
|    Instructions : 148.1416 per block    ( 51.41%) -   2.3148 per byte    -  26.4533 per structural    -    2.852 per cycle
|    Misses       :    1854 branch misses ( 34.20%) - 40 cache misses (  2.82%) - 51063.00 cache references

4060.1 documents parsed per second (best)
  1. parse-number branch:
$ build/benchmark/dom/parse jsonexamples/twitter.json
number of iterations 200

jsonexamples/twitter.json
=========================
     9867 blocks -     631515 bytes - 55262 structurals (  8.8 %)
special blocks with: utf8      2284 ( 23.1 %) - escape       598 (  6.1 %) - 0 structurals      1287 ( 13.0 %) - 1+ structurals      8581 ( 87.0 %) - 8+ structurals      3272 ( 33.2 %) - 16+ structurals         0 (  0.0 %)
special block flips: utf8      1104 ( 11.2 %) - escape       642 (  6.5 %) - 0 structurals       940 (  9.5 %) - 1+ structurals       940 (  9.5 %) - 8+ structurals      2593 ( 26.3 %) - 16+ structurals         0 (  0.0 %)

All Stages (excluding allocation)
|    Speed        :  25.0001 ns per block ( 86.51%) -   0.3906 ns per byte -   4.4642 ns per structural -   2.5598 GB/s
|    Cycles       :  99.9156 per block    ( 87.99%) -   1.5613 per byte    -  17.8417 per structural    -    3.997 GHz est. frequency
|    Instructions : 286.9191 per block    (100.00%) -   4.4834 per byte    -  51.2344 per structural    -    2.872 per cycle
|    Misses       :    4863 branch misses ( 87.04%) - 4 cache misses (  0.10%) - 78568.00 cache references
|- Stage 1
|    Speed        :  11.8261 ns per block ( 40.92%) -   0.1848 ns per byte -   2.1118 ns per structural -   5.4114 GB/s
|    Cycles       :  47.2337 per block    ( 41.60%) -   0.7381 per byte    -   8.4344 per structural    -    3.994 GHz est. frequency
|    Instructions : 140.0335 per block    ( 48.81%) -   2.1882 per byte    -  25.0054 per structural    -    2.965 per cycle
|    Misses       :    3159 branch misses ( 56.54%) - 1 cache misses (  0.03%) - 27465.00 cache references
|- Stage 2
|    Speed        :  13.0320 ns per block ( 45.10%) -   0.2036 ns per byte -   2.3271 ns per structural -   4.9107 GB/s
|    Cycles       :  52.0480 per block    ( 45.84%) -   0.8133 per byte    -   9.2941 per structural    -    3.994 GHz est. frequency
|    Instructions : 146.8856 per block    ( 51.19%) -   2.2952 per byte    -  26.2290 per structural    -    2.822 per cycle
|    Misses       :    1713 branch misses ( 30.66%) - 175 cache misses (  4.50%) - 51126.00 cache references

4053.5 documents parsed per second (best)

OnDemand Parser

  1. master branch
$ sudo ./build/benchmark/bench_ondemand --benchmark_filter="kostya<simdjson"
2024-02-23T21:58:07-05:00
Running ./build/benchmark/bench_ondemand
Run on (8 X 4008 MHz CPU s)
CPU Caches:
  L1 Data 32 KiB (x4)
  L1 Instruction 32 KiB (x4)
  L2 Unified 256 KiB (x4)
  L3 Unified 8192 KiB (x1)
Load Average: 0.51, 0.52, 0.68
simdjson::dom implementation:      haswell
simdjson::ondemand implementation (stage 1): haswell
simdjson::ondemand implementation (stage 2): fallback
Creating a source file spanning 134087 KB
------------------------------------------------------------------------------------------------
Benchmark                                      Time             CPU   Iterations UserCounters...
------------------------------------------------------------------------------------------------
kostya<simdjson_ondemand>/manual_time   62129868 ns     73054200 ns           11 best_branch_miss=449.157k best_bytes_per_sec=2.31449G best_cache_miss=10.3347M best_cache_ref=14.1213M best_cycles=235.377M best_cycles_per_byte=1.71426 best_docs_per_sec=16.8566 best_frequency=3.96765G best_instructions=668.194M best_instructions_per_byte=4.8665 best_instructions_per_cycle=2.83883 best_items_per_sec=8.83769M branch_miss=454.235k bytes=137.305M bytes_per_second=2.05819G/s cache_miss=10.222M cache_ref=13.9302M cycles=244.983M cycles_per_byte=1.78423 docs_per_sec=16.0953/s frequency=3.94308G/s instructions=668.194M instructions_per_byte=4.8665 instructions_per_cycle=2.72751 items=524.288k items_per_second=8.43858M/s [BEST: throughput=  2.31 GB/s doc_throughput=    16 docs/s instructions=   668194356 cycles=   235376922 branch_miss=  449157 cache_miss=10334729 cache_ref=  14121265 items=    524288 avg_time=  62129868 ns]
kostya<simdjson_dom>/manual_time        97495370 ns    109041887 ns            8 best_branch_miss=439.83k best_bytes_per_sec=1.56793G best_cache_miss=15.9609M best_cache_ref=22.2006M best_cycles=346.265M best_cycles_per_byte=2.52187 best_docs_per_sec=11.4193 best_frequency=3.95411G best_instructions=952.568M best_instructions_per_byte=6.9376 best_instructions_per_cycle=2.75098 best_items_per_sec=5.98702M branch_miss=471.538k bytes=137.305M bytes_per_second=1.3116G/s cache_miss=15.9704M cache_ref=22.2623M cycles=372.426M cycles_per_byte=2.7124 docs_per_sec=10.2569/s frequency=3.81993G/s instructions=952.568M instructions_per_byte=6.9376 instructions_per_cycle=2.55774 items=524.288k items_per_second=5.37757M/s [BEST: throughput=  1.57 GB/s doc_throughput=    11 docs/s instructions=   952567708 cycles=   346264794 branch_miss=  439830 cache_miss=15960883 cache_ref=  22200598 items=    524288 avg_time=  97495370 ns]
  1. parse-number branch
$ sudo ./build/benchmark/bench_ondemand --benchmark_filter="kostya<simdjson"
2024-02-23T21:57:49-05:00
Running ./build/benchmark/bench_ondemand
Run on (8 X 4008 MHz CPU s)
CPU Caches:
  L1 Data 32 KiB (x4)
  L1 Instruction 32 KiB (x4)
  L2 Unified 256 KiB (x4)
  L3 Unified 8192 KiB (x1)
Load Average: 0.52, 0.52, 0.68
simdjson::dom implementation:      haswell
simdjson::ondemand implementation (stage 1): haswell
simdjson::ondemand implementation (stage 2): fallback
Creating a source file spanning 134087 KB
------------------------------------------------------------------------------------------------
Benchmark                                      Time             CPU   Iterations UserCounters...
------------------------------------------------------------------------------------------------
kostya<simdjson_ondemand>/manual_time   64154209 ns     75045575 ns           12 best_branch_miss=455.529k best_bytes_per_sec=2.30763G best_cache_miss=10.0512M best_cache_ref=13.8055M best_cycles=235.518M best_cycles_per_byte=1.71529 best_docs_per_sec=16.8066 best_frequency=3.95825G best_instructions=668.194M best_instructions_per_byte=4.8665 best_instructions_per_cycle=2.83713 best_items_per_sec=8.81148M branch_miss=467.041k bytes=137.305M bytes_per_second=1.99325G/s cache_miss=10.1664M cache_ref=13.9364M cycles=249.667M cycles_per_byte=1.81834 docs_per_sec=15.5874/s frequency=3.89167G/s instructions=668.194M instructions_per_byte=4.8665 instructions_per_cycle=2.67634 items=524.288k items_per_second=8.17231M/s [BEST: throughput=  2.31 GB/s doc_throughput=    16 docs/s instructions=   668194356 cycles=   235517803 branch_miss=  455529 cache_miss=10051235 cache_ref=  13805468 items=    524288 avg_time=  64154208 ns]
kostya<simdjson_dom>/manual_time        97182318 ns    108159788 ns            8 best_branch_miss=442.236k best_bytes_per_sec=1.55099G best_cache_miss=16.0065M best_cache_ref=22.3629M best_cycles=349.926M best_cycles_per_byte=2.54853 best_docs_per_sec=11.296 best_frequency=3.95276G best_instructions=948.373M best_instructions_per_byte=6.90706 best_instructions_per_cycle=2.71021 best_items_per_sec=5.92234M branch_miss=464.832k bytes=137.305M bytes_per_second=1.31583G/s cache_miss=16.0094M cache_ref=22.2926M cycles=379.716M cycles_per_byte=2.76549 docs_per_sec=10.2899/s frequency=3.90726G/s instructions=948.373M instructions_per_byte=6.90706 instructions_per_cycle=2.49758 items=524.288k items_per_second=5.39489M/s [BEST: throughput=  1.55 GB/s doc_throughput=    11 docs/s instructions=   948373254 cycles=   349926496 branch_miss=  442236 cache_miss=16006478 cache_ref=  22362910 items=    524288 avg_time=  97182317 ns]

@lemire

lemire commented Feb 24, 2024

Copy link
Copy Markdown
Member

@saleyn The performance looks good.

The fuzzers indicate that we probably have a safety issue.

@saleyn

saleyn commented Feb 24, 2024

Copy link
Copy Markdown
Contributor Author

Another unrelated improvement suggestion would be to add [[unlikely]] hints to the error checks to help the branch predictor, e.g.:

simdjson_inline error_code array::consume() noexcept {
  auto error = iter.json_iter().skip_child(iter.depth()-1);
  if(error) [[unlikely]] { iter.abandon(); }
  return error;
}

Given that there are a lot of places containing such branching, it may have some performance benefit.

@lemire

lemire commented Feb 24, 2024

Copy link
Copy Markdown
Member

Given that there are a lot of places containing such branching, it may have some performance benefit.

This is a fair topic to investigate. If get good results, please issue a separate pull request.

Of course, these are keywords introduced in C++20 and we want the main library to support C++11. In simdjson, we have simdjson_likely and simdjson_unlikely.

@saleyn

saleyn commented Feb 24, 2024 •

Copy link
Copy Markdown
Contributor Author

I recalled why I decided to do a more intrusive change. The issue was the question of what get_number_type() should return for big integers. The current implementation of the library required to introduce another number_type::big_integer to distinguish from ints that fit in 64 bits, and in the value_interator there didn't seem to be a way of getting the pointer to the content of the big integer (get_raw_json_string expected the string to be enclosed in "'). From that point on it seemed more convenient to have a special bigint_t` type that handled everything similarly to other integer types. Now that we've been through several rounds of code reviews, and given the time constraints, I think I'll try to go back to the less intrusive approach, create another PR today, and finish this more intrusive approach when the time permits, but for now will close it.

@saleyn saleyn closed this Feb 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants