Commit 1a65b78
btf: Replace
During profiling `binary.Read` uses up a significant amount of CPU time.
This seems to be due to it using reflection to calculate the amount of
bytes to read at runtime and not caching these results.
By doing manual `io.ReadFull` calls, pre-calculating struct sizes and
reusing types and buffers where possible, we can reduce the CPU time
spent in `readAndInflateTypes` by almost 25%.
```
goos: linux
goarch: amd64
pkg: github.com/cilium/ebpf/btf
cpu: 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz
│ before.txt │ after.txt │
│ sec/op │ sec/op vs base │
ParseVmlinux-16 46.08m ± 1% 34.59m ± 2% -24.93% (n=100)
│ before.txt │ after.txt │
│ B/op │ B/op vs base │
ParseVmlinux-16 26.65Mi ± 0% 23.49Mi ± 0% -11.87% (n=100)
│ before.txt │ after.txt │
│ allocs/op │ allocs/op vs base │
ParseVmlinux-16 467.5k ± 0% 267.7k ± 0% -42.73% (n=100)
```
Signed-off-by: Dylan Reimerink <[email protected]>binary.Read with manual parsing in readAndInflateTypes
1 parent 7146ce3 commit 1a65b78
2 files changed
+221
-41
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
153 | 153 | | |
154 | 154 | | |
155 | 155 | | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
156 | 169 | | |
157 | 170 | | |
158 | 171 | | |
| |||
300 | 313 | | |
301 | 314 | | |
302 | 315 | | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
303 | 327 | | |
304 | 328 | | |
305 | 329 | | |
| |||
330 | 354 | | |
331 | 355 | | |
332 | 356 | | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
333 | 370 | | |
334 | 371 | | |
335 | 372 | | |
336 | 373 | | |
337 | 374 | | |
338 | 375 | | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
339 | 395 | | |
340 | 396 | | |
341 | 397 | | |
342 | 398 | | |
343 | 399 | | |
344 | 400 | | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
345 | 420 | | |
346 | 421 | | |
347 | 422 | | |
348 | 423 | | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
349 | 435 | | |
350 | 436 | | |
351 | 437 | | |
352 | 438 | | |
353 | 439 | | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
354 | 458 | | |
355 | 459 | | |
356 | 460 | | |
357 | 461 | | |
358 | 462 | | |
359 | 463 | | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
360 | 483 | | |
361 | 484 | | |
362 | 485 | | |
363 | 486 | | |
364 | 487 | | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
365 | 506 | | |
366 | 507 | | |
367 | 508 | | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
0 commit comments