diff options
author | Milo Yip <miloyip@gmail.com> | 2014-07-26 16:28:35 +0400 |
---|---|---|
committer | Milo Yip <miloyip@gmail.com> | 2014-07-26 16:28:35 +0400 |
commit | 01126defd2edd6cf2491ddecfe06e441b1a98a3d (patch) | |
tree | 9e4b7cc2dd473deccc48153a6f171f059d8db65c /include/rapidjson/internal/stack.h | |
parent | e9597255b4f2035e1a586933a2f66056c38ed1eb (diff) |
Make Stack::Push() force inline in normal path
Diffstat (limited to 'include/rapidjson/internal/stack.h')
-rw-r--r-- | include/rapidjson/internal/stack.h | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/include/rapidjson/internal/stack.h b/include/rapidjson/internal/stack.h index ff5ff82a..85484655 100644 --- a/include/rapidjson/internal/stack.h +++ b/include/rapidjson/internal/stack.h @@ -28,20 +28,14 @@ public: void Clear() { /*stack_top_ = 0;*/ stack_top_ = stack_; }
+ // Optimization note: try to minimize the size of this function for force inline.
+ // Expansion is run very infrequently, so it is moved to another (probably non-inline) function.
template<typename T>
- T* Push(size_t count = 1) {
+ RAPIDJSON_FORCEINLINE T* Push(size_t count = 1) {
// Expand the stack if needed
- if (stack_top_ + sizeof(T) * count >= stack_end_) {
- size_t new_capacity = stack_capacity_ * 2;
- size_t size = GetSize();
- size_t new_size = GetSize() + sizeof(T) * count;
- if (new_capacity < new_size)
- new_capacity = new_size;
- stack_ = (char*)allocator_->Realloc(stack_, stack_capacity_, new_capacity);
- stack_capacity_ = new_capacity;
- stack_top_ = stack_ + size;
- stack_end_ = stack_ + stack_capacity_;
- }
+ if (stack_top_ + sizeof(T) * count >= stack_end_)
+ Expand<T>(count);
+
T* ret = reinterpret_cast<T*>(stack_top_);
stack_top_ += sizeof(T) * count;
return ret;
@@ -69,6 +63,19 @@ public: size_t GetCapacity() const { return stack_capacity_; }
private:
+ template<typename T>
+ void Expand(size_t count) {
+ size_t new_capacity = stack_capacity_ * 2;
+ size_t size = GetSize();
+ size_t new_size = GetSize() + sizeof(T) * count;
+ if (new_capacity < new_size)
+ new_capacity = new_size;
+ stack_ = (char*)allocator_->Realloc(stack_, stack_capacity_, new_capacity);
+ stack_capacity_ = new_capacity;
+ stack_top_ = stack_ + size;
+ stack_end_ = stack_ + stack_capacity_;
+ }
+
// Prohibit copy constructor & assignment operator.
Stack(const Stack&);
Stack& operator=(const Stack&);
|