Commit cb641f2
authored
fix: reduce the number of memory allocations and the latency overhead. (#983)
* fix: reduce the number of memory allocations and the latency overhead.
Struct instrumentedConn would trace every call to Read() and Write().
This incurs a significant overhead because the variadic arguments of
trace.RecordBytesReceived() escape and need to be heap-allocated.
Also, every trace.RecordBytesReceived() would be called in a new
goroutine. That makes the call yet more expensive.
It makes no sense to update the performance counter this often. The
default scraping interval in Prometheus is 1 minute. Google Cloud
Monitoring had the same interval before they added high-resolution
counters that scrape services every 10 seconds.
Only update integer counters in the hot path, and update OpenCensus'
counters once in 5 seconds.
I have a test that just loops through `SELECT * FROM t WHERE id = $1`
and has response sizes that range from several dozen bytes to several
kilobytes. At 64 connections to Postgres and 10k requests per connection,
the test makes approx. 25.5M allocations before this patch, and 7.2M
allocations after the patch. The CPU time goes down accordingly
because OpenCensus and the garbage collector have less work to do.
* fix: do not spawn goroutines to report dial_latency and open_connections.
Creating a goroutine introduces a latency of its own. Moreover, a Dial()
that makes a TLS connection will not benefit from any possiblemicrosend-
level savings.
* chore: use atomic.Int64 and atomic.Int32.
Unlike int64 struct fields, atomic.Int64 fields are guaranteed to be
aligned to 8 bytes on 32-bit platoform. This alignement is required
for atomic ops to work.
Also, replace atomic int32 vars with atomic.Int32 for consistency.1 parent fbe77c1 commit cb641f2
File tree
5 files changed
+84
-58
lines changed5 files changed
+84
-58
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
338 | 338 | | |
339 | 339 | | |
340 | 340 | | |
341 | | - | |
| 341 | + | |
342 | 342 | | |
343 | 343 | | |
344 | 344 | | |
| |||
429 | 429 | | |
430 | 430 | | |
431 | 431 | | |
432 | | - | |
433 | | - | |
434 | | - | |
435 | | - | |
436 | | - | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
437 | 435 | | |
438 | 436 | | |
439 | | - | |
| 437 | + | |
440 | 438 | | |
441 | 439 | | |
442 | 440 | | |
| |||
571 | 569 | | |
572 | 570 | | |
573 | 571 | | |
574 | | - | |
575 | | - | |
576 | | - | |
577 | | - | |
578 | | - | |
579 | | - | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
580 | 581 | | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
581 | 586 | | |
582 | 587 | | |
583 | 588 | | |
584 | 589 | | |
585 | 590 | | |
586 | 591 | | |
587 | | - | |
588 | | - | |
589 | | - | |
590 | | - | |
591 | | - | |
592 | | - | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
593 | 602 | | |
594 | 603 | | |
595 | 604 | | |
596 | 605 | | |
597 | 606 | | |
598 | 607 | | |
599 | 608 | | |
600 | | - | |
| 609 | + | |
601 | 610 | | |
602 | 611 | | |
603 | 612 | | |
| |||
609 | 618 | | |
610 | 619 | | |
611 | 620 | | |
612 | | - | |
| 621 | + | |
613 | 622 | | |
614 | 623 | | |
615 | 624 | | |
| |||
629 | 638 | | |
630 | 639 | | |
631 | 640 | | |
632 | | - | |
633 | | - | |
634 | | - | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
635 | 663 | | |
636 | | - | |
637 | | - | |
638 | 664 | | |
639 | 665 | | |
640 | 666 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1024 | 1024 | | |
1025 | 1025 | | |
1026 | 1026 | | |
1027 | | - | |
| 1027 | + | |
1028 | 1028 | | |
1029 | 1029 | | |
1030 | 1030 | | |
1031 | 1031 | | |
1032 | 1032 | | |
1033 | | - | |
| 1033 | + | |
1034 | 1034 | | |
1035 | 1035 | | |
1036 | 1036 | | |
| |||
1054 | 1054 | | |
1055 | 1055 | | |
1056 | 1056 | | |
1057 | | - | |
1058 | | - | |
1059 | | - | |
| 1057 | + | |
1060 | 1058 | | |
1061 | 1059 | | |
1062 | 1060 | | |
| |||
1084 | 1082 | | |
1085 | 1083 | | |
1086 | 1084 | | |
1087 | | - | |
| 1085 | + | |
1088 | 1086 | | |
1089 | 1087 | | |
1090 | 1088 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
172 | 172 | | |
173 | 173 | | |
174 | 174 | | |
175 | | - | |
176 | 175 | | |
177 | 176 | | |
178 | 177 | | |
| |||
194 | 193 | | |
195 | 194 | | |
196 | 195 | | |
197 | | - | |
198 | 196 | | |
199 | 197 | | |
200 | 198 | | |
| |||
205 | 203 | | |
206 | 204 | | |
207 | 205 | | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
208 | 212 | | |
209 | 213 | | |
210 | 214 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
| 30 | + | |
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
56 | | - | |
57 | 56 | | |
58 | 57 | | |
59 | 58 | | |
| |||
98 | 97 | | |
99 | 98 | | |
100 | 99 | | |
101 | | - | |
| 100 | + | |
102 | 101 | | |
103 | 102 | | |
104 | 103 | | |
105 | 104 | | |
106 | 105 | | |
107 | | - | |
| 106 | + | |
108 | 107 | | |
109 | 108 | | |
110 | 109 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
66 | | - | |
67 | | - | |
68 | | - | |
| 66 | + | |
69 | 67 | | |
70 | 68 | | |
71 | 69 | | |
| |||
81 | 79 | | |
82 | 80 | | |
83 | 81 | | |
84 | | - | |
| 82 | + | |
85 | 83 | | |
86 | 84 | | |
87 | 85 | | |
| |||
93 | 91 | | |
94 | 92 | | |
95 | 93 | | |
96 | | - | |
| 94 | + | |
97 | 95 | | |
98 | | - | |
99 | | - | |
100 | | - | |
| 96 | + | |
101 | 97 | | |
102 | 98 | | |
103 | 99 | | |
| |||
107 | 103 | | |
108 | 104 | | |
109 | 105 | | |
110 | | - | |
| 106 | + | |
111 | 107 | | |
112 | 108 | | |
113 | 109 | | |
114 | 110 | | |
115 | 111 | | |
116 | 112 | | |
117 | | - | |
118 | | - | |
119 | | - | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
120 | 117 | | |
121 | 118 | | |
122 | | - | |
123 | | - | |
124 | | - | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
125 | 123 | | |
126 | 124 | | |
127 | | - | |
128 | | - | |
129 | | - | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
130 | 129 | | |
131 | 130 | | |
132 | | - | |
| 131 | + | |
133 | 132 | | |
134 | 133 | | |
135 | 134 | | |
| |||
138 | 137 | | |
139 | 138 | | |
140 | 139 | | |
141 | | - | |
| 140 | + | |
142 | 141 | | |
143 | 142 | | |
144 | 143 | | |
| |||
0 commit comments