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

Side by Side Diff: src/lazy-instance.h

Issue 9873023: Fix performance regressions due to lazy initialization. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Add OS::PostSetUp(). Created 8 years, 9 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 // providing your own trait: 58 // providing your own trait:
59 // Example usage: 59 // Example usage:
60 // struct MyCreateTrait { 60 // struct MyCreateTrait {
61 // static void Construct(MyClass* allocated_ptr) { 61 // static void Construct(MyClass* allocated_ptr) {
62 // new (allocated_ptr) MyClass(/* extra parameters... */); 62 // new (allocated_ptr) MyClass(/* extra parameters... */);
63 // } 63 // }
64 // }; 64 // };
65 // static LazyInstance<MyClass, MyCreateTrait>::type my_instance = 65 // static LazyInstance<MyClass, MyCreateTrait>::type my_instance =
66 // LAZY_INSTANCE_INITIALIZER; 66 // LAZY_INSTANCE_INITIALIZER;
67 // 67 //
68 // WARNING: This implementation of LazyInstance is NOT thread-safe by default.
69 // See ThreadSafeInitOnceTrait declared below for that.
70 //
68 // Notes for advanced users: 71 // Notes for advanced users:
69 // LazyInstance can actually be used in two different ways: 72 // LazyInstance can actually be used in two different ways:
70 // 73 //
71 // - "Static mode" which is the default mode since it is the most efficient 74 // - "Static mode" which is the default mode since it is the most efficient
72 // (no extra heap allocation). In this mode, the instance is statically 75 // (no extra heap allocation). In this mode, the instance is statically
73 // allocated (stored in the global data section at compile time). 76 // allocated (stored in the global data section at compile time).
74 // The macro LAZY_STATIC_INSTANCE_INITIALIZER (= LAZY_INSTANCE_INITIALIZER) 77 // The macro LAZY_STATIC_INSTANCE_INITIALIZER (= LAZY_INSTANCE_INITIALIZER)
75 // must be used to initialize static lazy instances. 78 // must be used to initialize static lazy instances.
76 // 79 //
77 // - "Dynamic mode". In this mode, the instance is dynamically allocated and 80 // - "Dynamic mode". In this mode, the instance is dynamically allocated and
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 147
145 148
146 template <typename T> 149 template <typename T>
147 struct DefaultCreateTrait { 150 struct DefaultCreateTrait {
148 static T* Create() { 151 static T* Create() {
149 return new T(); 152 return new T();
150 } 153 }
151 }; 154 };
152 155
153 156
157 template <typename T>
158 struct ThreadSafeInitOnceTrait {
159 template <typename Function, typename Storage>
160 static void Init(OnceType* once, Function function, Storage storage) {
161 CallOnce(once, function, storage);
162 }
163 };
164
165
166 // Initialization trait for users who don't care about thread-safety.
167 template <typename T>
168 struct SingleThreadInitOnceTrait {
169 template <typename Function, typename Storage>
170 static void Init(OnceType* once, Function function, Storage storage) {
171 if (*once == ONCE_STATE_UNINITIALIZED) {
172 function(storage);
173 *once = ONCE_STATE_DONE;
174 }
175 }
176 };
177
178
154 // TODO(pliard): Handle instances destruction (using global destructors). 179 // TODO(pliard): Handle instances destruction (using global destructors).
155 template <typename T, typename AllocationTrait, typename CreateTrait, 180 template <typename T, typename AllocationTrait, typename CreateTrait,
156 typename DestroyTrait /* not used yet. */ > 181 template <class> class InitOnceTrait,
182 typename DestroyTrait /* not used yet. */>
157 struct LazyInstanceImpl { 183 struct LazyInstanceImpl {
158 public: 184 public:
159 typedef typename AllocationTrait::StorageType StorageType; 185 typedef typename AllocationTrait::StorageType StorageType;
160 186
161 private: 187 private:
162 static void InitInstance(StorageType* storage) { 188 static void InitInstance(StorageType* storage) {
163 AllocationTrait::template InitStorageUsingTrait<CreateTrait>(storage); 189 AllocationTrait::template InitStorageUsingTrait<CreateTrait>(storage);
164 } 190 }
165 191
166 void Init() const { 192 void Init() const {
167 CallOnce(&once_, &InitInstance, &storage_); 193 InitOnceTrait<T>::Init(
194 &once_,
195 // Casts to void* are needed here to avoid breaking strict aliasing
196 // rules.
197 reinterpret_cast<void (*)(void*)>(&InitInstance),
198 reinterpret_cast<void*>(&storage_));
168 } 199 }
169 200
170 public: 201 public:
171 T* Pointer() { 202 T* Pointer() {
172 Init(); 203 Init();
173 return AllocationTrait::MutableInstance(&storage_); 204 return AllocationTrait::MutableInstance(&storage_);
174 } 205 }
175 206
176 const T& Get() const { 207 const T& Get() const {
177 Init(); 208 Init();
178 return *AllocationTrait::MutableInstance(&storage_); 209 return *AllocationTrait::MutableInstance(&storage_);
179 } 210 }
180 211
181 mutable OnceType once_; 212 mutable OnceType once_;
182 // Note that the previous field, OnceType, is an AtomicWord which guarantees 213 // Note that the previous field, OnceType, is an AtomicWord which guarantees
183 // the correct alignment of the storage field below. 214 // the correct alignment of the storage field below.
184 mutable StorageType storage_; 215 mutable StorageType storage_;
185 }; 216 };
186 217
187 218
188 template <typename T, 219 template <typename T,
189 typename CreateTrait = DefaultConstructTrait<T>, 220 typename CreateTrait = DefaultConstructTrait<T>,
221 template <class> class InitOnceTrait = SingleThreadInitOnceTrait,
190 typename DestroyTrait = LeakyInstanceTrait<T> > 222 typename DestroyTrait = LeakyInstanceTrait<T> >
191 struct LazyStaticInstance { 223 struct LazyStaticInstance {
192 typedef LazyInstanceImpl<T, StaticallyAllocatedInstanceTrait<T>, CreateTrait, 224 typedef LazyInstanceImpl<T, StaticallyAllocatedInstanceTrait<T>,
193 DestroyTrait> type; 225 CreateTrait, InitOnceTrait, DestroyTrait> type;
194 }; 226 };
195 227
196 228
197 template <typename T, 229 template <typename T,
198 typename CreateTrait = DefaultConstructTrait<T>, 230 typename CreateTrait = DefaultConstructTrait<T>,
231 template <class> class InitOnceTrait = SingleThreadInitOnceTrait,
199 typename DestroyTrait = LeakyInstanceTrait<T> > 232 typename DestroyTrait = LeakyInstanceTrait<T> >
200 struct LazyInstance { 233 struct LazyInstance {
201 // A LazyInstance is a LazyStaticInstance. 234 // A LazyInstance is a LazyStaticInstance.
202 typedef typename LazyStaticInstance<T, CreateTrait, DestroyTrait>::type type; 235 typedef typename LazyStaticInstance<T, CreateTrait, InitOnceTrait,
236 DestroyTrait>::type type;
203 }; 237 };
204 238
205 239
206 template <typename T, 240 template <typename T,
207 typename CreateTrait = DefaultConstructTrait<T>, 241 typename CreateTrait = DefaultConstructTrait<T>,
242 template <class> class InitOnceTrait = SingleThreadInitOnceTrait,
208 typename DestroyTrait = LeakyInstanceTrait<T> > 243 typename DestroyTrait = LeakyInstanceTrait<T> >
209 struct LazyDynamicInstance { 244 struct LazyDynamicInstance {
210 typedef LazyInstanceImpl<T, DynamicallyAllocatedInstanceTrait<T>, CreateTrait, 245 typedef LazyInstanceImpl<T, DynamicallyAllocatedInstanceTrait<T>,
211 DestroyTrait> type; 246 CreateTrait, InitOnceTrait, DestroyTrait> type;
212 }; 247 };
213 248
214 } } // namespace v8::internal 249 } } // namespace v8::internal
215 250
216 #endif // V8_LAZY_INSTANCE_H_ 251 #endif // V8_LAZY_INSTANCE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698