Skip to content

Commit 077de5e

Browse files
deploy: 8b9f6a7
1 parent e5011cc commit 077de5e

3 files changed

Lines changed: 14 additions & 12 deletions

File tree

cppcon2025/cppcon_2025_slides.html

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -961,16 +961,17 @@ <h1 id="optimization-3-fast-digit-counting">Optimization #3: Fast Digit Counting
961961
The Problem: Need to know buffer size before converting number to string
962962

963963
Traditional Approach:
964-
<span class="hljs-function"><span class="hljs-type">size_t</span> <span class="hljs-title">digit_count</span><span class="hljs-params">(<span class="hljs-type">uint64_t</span> v)</span> </span>{
964+
```<span class="hljs-function">cpp
965+
<span class="hljs-type">size_t</span> <span class="hljs-title">digit_count</span><span class="hljs-params">(<span class="hljs-type">uint64_t</span> v)</span> </span>{
965966
<span class="hljs-keyword">return</span> std::<span class="hljs-built_in">to_string</span>(v).<span class="hljs-built_in">length</span>();
966967
<span class="hljs-comment">// 1. Allocates memory</span>
967968
<span class="hljs-comment">// 2. Converts entire number to string</span>
968969
<span class="hljs-comment">// 3. Gets length</span>
969970
<span class="hljs-comment">// 4. Deallocates string</span>
970971
}
971-
972-
Our Optimization:
973-
<span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">fast_digit_count</span><span class="hljs-params">(<span class="hljs-type">uint64_t</span> x)</span> </span>{
972+
</code></pre>
973+
<p>Our Optimization:</p>
974+
<pre is="marp-pre" data-auto-scaling="downscale-only"><code class="language-cpp"><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">fast_digit_count</span><span class="hljs-params">(<span class="hljs-type">uint64_t</span> x)</span> </span>{
974975
<span class="hljs-comment">// Approximate using bit operations (no division!)</span>
975976
<span class="hljs-type">int</span> y = (<span class="hljs-number">19</span> * <span class="hljs-built_in">int_log2</span>(x) &gt;&gt; <span class="hljs-number">6</span>);
976977

@@ -980,14 +981,11 @@ <h1 id="optimization-3-fast-digit-counting">Optimization #3: Fast Digit Counting
980981

981982
<span class="hljs-keyword">return</span> y + <span class="hljs-number">1</span>;
982983
}
983-
984-
Zero allocations, no string conversion, just math!
985-
986-
# Optimizations #<span class="hljs-number">4</span> &amp; #<span class="hljs-number">5</span>: Branch Hints &amp; Buffer Growth
987-
988-
**Branch Prediction:**
989-
```<span class="hljs-function">cpp
990-
<span class="hljs-title">if</span> <span class="hljs-params">(UNLIKELY(buffer_full))</span> </span>{ <span class="hljs-comment">// CPU knows this is rare</span>
984+
</code></pre>
985+
<p>Zero allocations, no string conversion, just math!</p>
986+
<h1 id="optimizations-4--5-branch-hints--buffer-growth">Optimizations #4 &amp; #5: Branch Hints &amp; Buffer Growth</h1>
987+
<p><strong>Branch Prediction:</strong></p>
988+
<pre is="marp-pre" data-auto-scaling="downscale-only"><code class="language-cpp"><span class="hljs-keyword">if</span> (<span class="hljs-built_in">UNLIKELY</span>(buffer_full)) { <span class="hljs-comment">// CPU knows this is rare</span>
991989
<span class="hljs-built_in">grow_buffer</span>();
992990
}
993991
<span class="hljs-comment">// CPU optimizes for this path</span>

cppcon2025/cppcon_2025_slides.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,15 +1069,18 @@ How Fast Digit Counting Works
10691069
The Problem: Need to know buffer size before converting number to string
10701070

10711071
Traditional Approach:
1072+
```cpp
10721073
size_t digit_count(uint64_t v) {
10731074
return std::to_string(v).length();
10741075
// 1. Allocates memory
10751076
// 2. Converts entire number to string
10761077
// 3. Gets length
10771078
// 4. Deallocates string
10781079
}
1080+
```
10791081
10801082
Our Optimization:
1083+
```cpp
10811084
int fast_digit_count(uint64_t x) {
10821085
// Approximate using bit operations (no division!)
10831086
int y = (19 * int_log2(x) >> 6);
@@ -1088,6 +1091,7 @@ int fast_digit_count(uint64_t x) {
10881091
10891092
return y + 1;
10901093
}
1094+
```
10911095

10921096
Zero allocations, no string conversion, just math!
10931097

cppcon2025/cppcon_2025_slides.pdf

-2.72 KB
Binary file not shown.

0 commit comments

Comments
 (0)