Commit 47696430 authored by Max Katsev's avatar Max Katsev Committed by Facebook Github Bot

optimize small_vector::clear

Summary:
clang fails to optimize the current version even in the opt mode

code:
```
void clear(folly::small_vector<int, 5>& vec) {
    vec.clear();
}
```

before:

cat_huh

```
0000000000000000 <clear(folly::small_vector<int, 5ul, void, void, void>&)>:
   0:	55                   	push   %rbp
   1:	48 89 e5             	mov    %rsp,%rbp
   4:	41 57                	push   %r15
   6:	41 56                	push   %r14
   8:	53                   	push   %rbx
   9:	50                   	push   %rax
   a:	49 89 fe             	mov    %rdi,%r14
   d:	48 8b 07             	mov    (%rdi),%rax
  10:	48 8d 4f 08          	lea    0x8(%rdi),%rcx
  14:	48 85 c0             	test   %rax,%rax
  17:	78 5a                	js     73 <clear(folly::small_vector<int, 5ul, void, void, void>&)+0x73>
  19:	48 89 cf             	mov    %rcx,%rdi
  1c:	49 bf ff ff ff ff ff 	movabs $0x7fffffffffffffff,%r15
  23:	ff ff 7f
  26:	48 89 c3             	mov    %rax,%rbx
  29:	4c 21 fb             	and    %r15,%rbx
  2c:	74 3a                	je     68 <clear(folly::small_vector<int, 5ul, void, void, void>&)+0x68>
  2e:	48 8d 34 9f          	lea    (%rdi,%rbx,4),%rsi
  32:	48 85 c0             	test   %rax,%rax
  35:	78 53                	js     8a <clear(folly::small_vector<int, 5ul, void, void, void>&)+0x8a>
  37:	48 8d 14 99          	lea    (%rcx,%rbx,4),%rdx
  3b:	48 89 d9             	mov    %rbx,%rcx
  3e:	48 29 f2             	sub    %rsi,%rdx
  41:	74 0e                	je     51 <clear(folly::small_vector<int, 5ul, void, void, void>&)+0x51>
  43:	e8 00 00 00 00       	callq  48 <clear(folly::small_vector<int, 5ul, void, void, void>&)+0x48>
  48:	49 8b 06             	mov    (%r14),%rax
  4b:	48 89 c1             	mov    %rax,%rcx
  4e:	4c 21 f9             	and    %r15,%rcx
  51:	48 c1 e3 02          	shl    $0x2,%rbx
  55:	48 c1 fb 02          	sar    $0x2,%rbx
  59:	48 29 d9             	sub    %rbx,%rcx
  5c:	49 ff c7             	inc    %r15
  5f:	49 21 c7             	and    %rax,%r15
  62:	49 09 cf             	or     %rcx,%r15
  65:	4d 89 3e             	mov    %r15,(%r14)
  68:	48 83 c4 08          	add    $0x8,%rsp
  6c:	5b                   	pop    %rbx
  6d:	41 5e                	pop    %r14
  6f:	41 5f                	pop    %r15
  71:	5d                   	pop    %rbp
  72:	c3                   	retq
  73:	48 8b 39             	mov    (%rcx),%rdi
  76:	49 bf ff ff ff ff ff 	movabs $0x7fffffffffffffff,%r15
  7d:	ff ff 7f
  80:	48 89 c3             	mov    %rax,%rbx
  83:	4c 21 fb             	and    %r15,%rbx
  86:	75 a6                	jne    2e <clear(folly::small_vector<int, 5ul, void, void, void>&)+0x2e>
  88:	eb de                	jmp    68 <clear(folly::small_vector<int, 5ul, void, void, void>&)+0x68>
  8a:	48 8b 09             	mov    (%rcx),%rcx
  8d:	48 8d 14 99          	lea    (%rcx,%rbx,4),%rdx
  91:	48 89 d9             	mov    %rbx,%rcx
  94:	48 29 f2             	sub    %rsi,%rdx
  97:	75 aa                	jne    43 <clear(folly::small_vector<int, 5ul, void, void, void>&)+0x43>
  99:	eb b6                	jmp    51 <clear(folly::small_vector<int, 5ul, void, void, void>&)+0x51>
```

after (much better but still 3 instructions too many):
```
0000000000000000 <clear(folly::small_vector<int, 5ul, void, void, void>&)>:
   0:	55                   	push   %rbp
   1:	48 89 e5             	mov    %rsp,%rbp
   4:	48 b8 00 00 00 00 00 	movabs $0x8000000000000000,%rax
   b:	00 00 80
   e:	48 21 07             	and    %rax,(%rdi)
  11:	5d                   	pop    %rbp
  12:	c3                   	retq
```

Reviewed By: ot, philippv, luciang

Differential Revision: D19890921

fbshipit-source-id: 62f1b5325b4deda95976894b476849b5098fad4f
parent 134472ee
......@@ -897,7 +897,12 @@ class small_vector : public detail::small_vector_base<
}
void clear() {
erase(begin(), end());
// Equivalent to erase(begin(), end()), but neither Clang or GCC are able to
// optimize away the abstraction.
for (auto it = begin(); it != end(); ++it) {
it->~value_type();
}
this->setSize(0);
}
template <class Arg>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment