Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(920)

Side by Side Diff: Source/wtf/VectorTraits.h

Issue 581683002: Expanding Type Traits to make Vector use mem ops more. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: More efficient Vector with more type traits. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/wtf/TypeTraits.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3 * 3 *
4 * This library is free software; you can redistribute it and/or 4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public 5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either 6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version. 7 * version 2 of the License, or (at your option) any later version.
8 * 8 *
9 * This library is distributed in the hope that it will be useful, 9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 17 matching lines...) Expand all
28 28
29 using std::pair; 29 using std::pair;
30 30
31 namespace WTF { 31 namespace WTF {
32 32
33 class AtomicString; 33 class AtomicString;
34 34
35 template<typename T> 35 template<typename T>
36 struct VectorTraitsBase 36 struct VectorTraitsBase
37 { 37 {
38 static const bool needsDestruction = !IsPod<T>::value; 38 static const bool needsDestruction = !IsTriviallyDestructible<T>::value;
39 static const bool canInitializeWithMemset = IsPod<T>::value; 39 static const bool canInitializeWithMemset = IsTriviallyDefaultConstructi ble<T>::value;
40 static const bool canMoveWithMemcpy = IsPod<T>::value; 40 static const bool canMoveWithMemcpy = IsTriviallyMoveAssignable<T>::valu e;
41 static const bool canCopyWithMemcpy = IsPod<T>::value; 41 static const bool canCopyWithMemcpy = IsTriviallyCopyAssignable<T>::valu e;
42 static const bool canFillWithMemset = IsPod<T>::value && (sizeof(T) == s izeof(char)); 42 static const bool canFillWithMemset = IsTriviallyDefaultConstructible<T> ::value && (sizeof(T) == sizeof(char));
43 static const bool canCompareWithMemcmp = IsPod<T>::value; 43 static const bool canCompareWithMemcmp = IsScalar<T>::value; // Types wi thout padding.
44 template<typename U = void> 44 template<typename U = void>
45 struct NeedsTracingLazily { 45 struct NeedsTracingLazily {
46 static const bool value = NeedsTracing<T>::value; 46 static const bool value = NeedsTracing<T>::value;
47 }; 47 };
48 static const WeakHandlingFlag weakHandlingFlag = NoWeakHandlingInCollect ions; // We don't support weak handling in vectors. 48 static const WeakHandlingFlag weakHandlingFlag = NoWeakHandlingInCollect ions; // We don't support weak handling in vectors.
49 }; 49 };
50 50
51 template<typename T> 51 template<typename T>
52 struct VectorTraits : VectorTraitsBase<T> { }; 52 struct VectorTraits : VectorTraitsBase<T> { };
53 53
54 // Classes marked with SimpleVectorTraits will use memmov, memcpy, memcmp 54 // Classes marked with SimpleVectorTraits will use memmov, memcpy, memcmp
55 // instead of constructors, copy operators, etc for initialization, move 55 // instead of constructors, copy operators, etc for initialization, move
56 // and comparison. 56 // and comparison.
57 template<typename T> 57 template<typename T>
58 struct SimpleClassVectorTraits : VectorTraitsBase<T> 58 struct SimpleClassVectorTraits : VectorTraitsBase<T>
59 { 59 {
60 static const bool canInitializeWithMemset = true; 60 static const bool canInitializeWithMemset = true;
61 static const bool canMoveWithMemcpy = true; 61 static const bool canMoveWithMemcpy = true;
62 static const bool canCompareWithMemcmp = true; 62 static const bool canCompareWithMemcmp = true;
63 }; 63 };
64 64
65 // We know OwnPtr and RefPtr are simple enough that initializing to 0 and 65 // We know OwnPtr and RefPtr are simple enough that initializing to 0 and
66 // moving with memcpy (and then not destructing the original) will totally 66 // moving with memcpy (and then not destructing the original) will totally
67 // work. 67 // work.
68 template<typename P> 68 template<typename P>
69 struct VectorTraits<RefPtr<P> > : SimpleClassVectorTraits<RefPtr<P> > { }; 69 struct VectorTraits<RefPtr<P> > : SimpleClassVectorTraits<RefPtr<P> > { };
70 70
71 template<typename P> 71 template<typename P>
72 struct VectorTraits<OwnPtr<P> > : SimpleClassVectorTraits<OwnPtr<P> > { }; 72 struct VectorTraits<OwnPtr<P> > : SimpleClassVectorTraits<OwnPtr<P> > {
73 // OwnPtr -> PassOwnPtr has a very particular structure that
74 // tricks the normal type traits into thinking that the class
75 // is "trivially copyable".
76 static const bool canCopyWithMemcpy = false;
77 };
78 COMPILE_ASSERT(VectorTraits<RefPtr<int> >::canInitializeWithMemset, ineffici entRefPtrVector);
79 COMPILE_ASSERT(VectorTraits<RefPtr<int> >::canMoveWithMemcpy, inefficientRef PtrVector);
80 COMPILE_ASSERT(VectorTraits<RefPtr<int> >::canCompareWithMemcmp, inefficient RefPtrVector);
81 COMPILE_ASSERT(VectorTraits<OwnPtr<int> >::canInitializeWithMemset, ineffici entOwnPtrVector);
82 COMPILE_ASSERT(VectorTraits<OwnPtr<int> >::canMoveWithMemcpy, inefficientOwn PtrVector);
83 COMPILE_ASSERT(VectorTraits<OwnPtr<int> >::canCompareWithMemcmp, inefficient OwnPtrVector);
73 84
74 template<typename First, typename Second> 85 template<typename First, typename Second>
75 struct VectorTraits<pair<First, Second> > 86 struct VectorTraits<pair<First, Second> >
76 { 87 {
77 typedef VectorTraits<First> FirstTraits; 88 typedef VectorTraits<First> FirstTraits;
78 typedef VectorTraits<Second> SecondTraits; 89 typedef VectorTraits<Second> SecondTraits;
79 90
80 static const bool needsDestruction = FirstTraits::needsDestruction || Se condTraits::needsDestruction; 91 static const bool needsDestruction = FirstTraits::needsDestruction || Se condTraits::needsDestruction;
81 static const bool canInitializeWithMemset = FirstTraits::canInitializeWi thMemset && SecondTraits::canInitializeWithMemset; 92 static const bool canInitializeWithMemset = FirstTraits::canInitializeWi thMemset && SecondTraits::canInitializeWithMemset;
82 static const bool canMoveWithMemcpy = FirstTraits::canMoveWithMemcpy && SecondTraits::canMoveWithMemcpy; 93 static const bool canMoveWithMemcpy = FirstTraits::canMoveWithMemcpy && SecondTraits::canMoveWithMemcpy;
83 static const bool canCopyWithMemcpy = FirstTraits::canCopyWithMemcpy && SecondTraits::canCopyWithMemcpy; 94 static const bool canCopyWithMemcpy = FirstTraits::canCopyWithMemcpy && SecondTraits::canCopyWithMemcpy;
84 static const bool canFillWithMemset = false; 95 static const bool canFillWithMemset = false;
85 static const bool canCompareWithMemcmp = FirstTraits::canCompareWithMemc mp && SecondTraits::canCompareWithMemcmp; 96 static const bool canCompareWithMemcmp = FirstTraits::canCompareWithMemc mp && SecondTraits::canCompareWithMemcmp;
86 template <typename U = void> 97 template <typename U = void>
87 struct NeedsTracingLazily { 98 struct NeedsTracingLazily {
88 static const bool value = ShouldBeTraced<FirstTraits>::value || Shou ldBeTraced<SecondTraits>::value; 99 static const bool value = ShouldBeTraced<FirstTraits>::value || Shou ldBeTraced<SecondTraits>::value;
89 }; 100 };
90 static const WeakHandlingFlag weakHandlingFlag = NoWeakHandlingInCollect ions; // We don't support weak handling in vectors. 101 static const WeakHandlingFlag weakHandlingFlag = NoWeakHandlingInCollect ions; // We don't support weak handling in vectors.
91 }; 102 };
92 103
93 } // namespace WTF 104 } // namespace WTF
94 105
95 #define WTF_ALLOW_MOVE_INIT_AND_COMPARE_WITH_MEM_FUNCTIONS(ClassName) \ 106 #define WTF_ALLOW_MOVE_INIT_AND_COMPARE_WITH_MEM_FUNCTIONS(ClassName) \
96 namespace WTF { \ 107 namespace WTF { \
108 COMPILE_ASSERT(!IsTriviallyDefaultConstructible<ClassName>::value || !IsTriviall yCopyAssignable<ClassName>::value, macro_not_needed); \
97 template<> \ 109 template<> \
98 struct VectorTraits<ClassName> : SimpleClassVectorTraits<ClassName> { }; \ 110 struct VectorTraits<ClassName> : SimpleClassVectorTraits<ClassName> { }; \
99 } 111 }
100 112
101 #define WTF_ALLOW_MOVE_AND_INIT_WITH_MEM_FUNCTIONS(ClassName) \ 113 #define WTF_ALLOW_MOVE_AND_INIT_WITH_MEM_FUNCTIONS(ClassName) \
102 namespace WTF { \ 114 namespace WTF { \
115 COMPILE_ASSERT(!WTF::IsTriviallyDefaultConstructible<ClassName>::value || !IsTri viallyCopyAssignable<ClassName>::value, macro_not_needed); \
103 template<> \ 116 template<> \
104 struct VectorTraits<ClassName> : VectorTraitsBase<ClassName> \ 117 struct VectorTraits<ClassName> : VectorTraitsBase<ClassName> \
105 { \ 118 { \
106 static const bool canInitializeWithMemset = true; \ 119 static const bool canInitializeWithMemset = true; \
107 static const bool canMoveWithMemcpy = true; \ 120 static const bool canMoveWithMemcpy = true; \
108 }; \ 121 }; \
109 } 122 }
110 123
111 #define WTF_ALLOW_INIT_WITH_MEM_FUNCTIONS(ClassName) \ 124 #define WTF_ALLOW_INIT_WITH_MEM_FUNCTIONS(ClassName) \
112 namespace WTF { \ 125 namespace WTF { \
126 COMPILE_ASSERT(!WTF::IsTriviallyDefaultConstructible<ClassName>::value, macro_no t_needed); \
113 template<> \ 127 template<> \
114 struct VectorTraits<ClassName> : VectorTraitsBase<ClassName> \ 128 struct VectorTraits<ClassName> : VectorTraitsBase<ClassName> \
115 { \ 129 { \
116 static const bool canInitializeWithMemset = true; \ 130 static const bool canInitializeWithMemset = true; \
117 }; \ 131 }; \
118 } 132 }
119 133
120 using WTF::VectorTraits; 134 using WTF::VectorTraits;
121 using WTF::SimpleClassVectorTraits; 135 using WTF::SimpleClassVectorTraits;
122 136
123 #endif // WTF_VectorTraits_h 137 #endif // WTF_VectorTraits_h
OLDNEW
« no previous file with comments | « Source/wtf/TypeTraits.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698