@@ -558,7 +558,7 @@ From neuroscience: systematically remove parts to understand function
558558
5595591. **Consteval**: Compile-time field name processing
5605602. **SIMD String Escaping**: Vectorized character checks
561- 3. **Fast Digit Counting **: Optimized digit count
561+ 3. **Fast Integer Serialization **: Optimized number handling
5625624. **Branch Prediction Hints**: CPU pipeline optimization
5635635. **Buffer Growth Strategy**: Smart memory allocation
564564
@@ -607,24 +607,69 @@ if (!needs_escape)
607607
608608---
609609
610- # Optimization #3 : Fast Digit Counting
610+ # Optimization #3 : Fast Integer serialization
611+
611612
612- ** Traditional:**
613613``` cpp
614- std::to_string (value).length(); // Allocates string just to count!
614+ while (number >= 10 ) {
615+ *write_pointer-- = char('0' + (number % 10));
616+ number /= 10;
617+ }
618+ *write_pointer = char (' 0' + number);
615619```
616620
617- **Optimized:**
621+ Writing from the end
622+
623+ ---
624+
625+ # Two digits at a time
626+
618627``` cpp
619- fast_digit_count(value); // Bit operations + lookup table
628+ while (number >= 100 ) {
629+ memcpy (write_pointer - 1, &internal::decimal_table[ (pv % 100)* 2] , 2);
630+ write_pointer -= 2;
631+ pv /= 100;
632+ }
633+ if (number >= 10 ) {
634+ *write_pointer-- = char('0' + (number % 10));
635+ number /= 10;
636+ }
637+ *write_pointer = char (' 0' + number);
620638```
621639
622- | Dataset | Baseline | No Fast Digits | ** Speedup** |
623- | ---------| ----------| ----------------| -------------|
624- | Twitter | 3,211 MB/s | 3,035 MB/s | ** 1.06x** |
625- | CITM | 2,360 MB/s | 1,767 MB/s | ** 1.34x** |
640+ ---
641+
642+ # Know where to start writing
643+
644+ - Useful to compute quickly the number of digits
645+
646+ ``` cpp
647+ int fast_digit_count_64 (uint64_t x) {
648+ static uint64_t table[ ] = {9,
649+ 99,
650+ 999,
651+ ...
652+ 9999999999999999ULL,
653+ 99999999999999999ULL,
654+ 999999999999999999ULL,
655+ 9999999999999999999ULL};
656+ int y = (19 * int_log2(x) >> 6);
657+ y += x > table[ y] ;
658+ return y + 1;
659+ }
660+ ```
661+
662+
663+ ---
664+
665+ # Does fast integer processing matter?
666+
667+ Replace fast digit count by naive approach
668+
669+ ```cpp
670+ std::to_string(value).length(); // Allocates string just to count!
671+ ```
626672
627- ** CITM has ~ 10,000+ integers!**
628673
629674---
630675
0 commit comments