Skip to content

Instantly share code, notes, and snippets.

@mgiuca
Created March 21, 2017 23:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgiuca/5642d9146b11b80b6a3d5f3324a6666b to your computer and use it in GitHub Desktop.
Save mgiuca/5642d9146b11b80b6a3d5f3324a6666b to your computer and use it in GitHub Desktop.
Assembly code for iteration over StringPiece; with base::internal::find changed to take arguments by value
# Source:
# void StringPieceFindTestVal(const std::vector<base::StringPiece>& vector) {
# for (base::StringPiece piece : vector)
# printf("%zu\n", piece.find('x'));
# }
#
# void StringPieceFindTestRef(const std::vector<base::StringPiece>& vector) {
# for (const base::StringPiece& piece : vector)
# printf("%zu\n", piece.find('x'));
# }
# (both functions generate same code)
#
# With base::internal::find changed to take arguments by value.
# Compiler: clang++ Linux 64-bit
# BB#0: # %entry
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq (%rdi), %rbx
movq 8(%rdi), %r15
cmpq %r15, %rbx
je .LBB25_3
# BB#1:
leaq .L.str.17(%rip), %r14
.p2align 4, 0x90
.LBB25_2: # %for.body
movq (%rbx), %rdi
movq 8(%rbx), %rsi
movl $120, %edx
xorl %ecx, %ecx
callq _ZN4base8internal4findENS_16BasicStringPieceISsEEcm@PLT
movq %rax, %rcx
xorl %eax, %eax
movq %r14, %rdi
movq %rcx, %rsi
callq printf@PLT
addq $16, %rbx
cmpq %r15, %rbx
jne .LBB25_2
.LBB25_3: # %for.cond.cleanup
addq $8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment