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

Side by Side Diff: gpu/command_buffer/common/id_allocator.cc

Issue 7077006: Speed up IdAllocator. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Clarified description. Created 9 years, 6 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 (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This file contains the implementation of IdAllocator. 5 // This file contains the implementation of IdAllocator.
6 6
7 #include "../common/id_allocator.h" 7 #include "../common/id_allocator.h"
8 #include "../common/logging.h" 8 #include "../common/logging.h"
9 9
10 namespace gpu { 10 namespace gpu {
11 11
12 IdAllocator::IdAllocator() {} 12 IdAllocator::IdAllocator() {}
13 13
14 IdAllocator::~IdAllocator() {} 14 IdAllocator::~IdAllocator() {}
15 15
16 ResourceId IdAllocator::AllocateID() { 16 ResourceId IdAllocator::AllocateID() {
17 ResourceId id = FindFirstFree(); 17 ResourceId id;
18 ResourceIdSet::iterator iter = free_ids_.begin();
19 if (iter != free_ids_.end()) {
20 id = *iter;
21 } else {
22 id = LastUsedId() + 1;
23 if (!id) {
24 // We wrapped around to 0.
25 id = FindFirstUnusedId();
26 }
27 }
18 MarkAsUsed(id); 28 MarkAsUsed(id);
19 return id; 29 return id;
20 } 30 }
21 31
22 ResourceId IdAllocator::AllocateIDAtOrAbove(ResourceId desired_id) { 32 ResourceId IdAllocator::AllocateIDAtOrAbove(ResourceId desired_id) {
23 GPU_DCHECK_LT(static_cast<ResourceId>(used_ids_.size()), 33 ResourceId id;
24 static_cast<ResourceId>(-1)); 34 ResourceIdSet::iterator iter = free_ids_.lower_bound(desired_id);
25 for (; InUse(desired_id); ++desired_id) {} 35 if (iter != free_ids_.end()) {
26 MarkAsUsed(desired_id); 36 id = *iter;
27 return desired_id; 37 } else if (LastUsedId() < desired_id) {
38 id = desired_id;
39 } else {
40 id = LastUsedId() + 1;
41 if (!id) {
42 // We wrapped around to 0.
43 id = FindFirstUnusedId();
44 }
45 }
46 MarkAsUsed(id);
47 return id;
28 } 48 }
29 49
30 bool IdAllocator::MarkAsUsed(ResourceId id) { 50 bool IdAllocator::MarkAsUsed(ResourceId id) {
51 GPU_DCHECK(id);
52 free_ids_.erase(id);
31 std::pair<ResourceIdSet::iterator, bool> result = used_ids_.insert(id); 53 std::pair<ResourceIdSet::iterator, bool> result = used_ids_.insert(id);
32 return result.second; 54 return result.second;
33 } 55 }
34 56
35 void IdAllocator::FreeID(ResourceId id) { 57 void IdAllocator::FreeID(ResourceId id) {
58 GPU_DCHECK(id);
36 used_ids_.erase(id); 59 used_ids_.erase(id);
60 std::pair<ResourceIdSet::iterator, bool> result = free_ids_.insert(id);
61 GPU_DCHECK(result.second);
37 } 62 }
38 63
39 bool IdAllocator::InUse(ResourceId id) const { 64 bool IdAllocator::InUse(ResourceId id) const {
40 return id == kInvalidResource || used_ids_.find(id) != used_ids_.end(); 65 return id == kInvalidResource || used_ids_.find(id) != used_ids_.end();
41 } 66 }
42 67
43 ResourceId IdAllocator::FindFirstFree() const { 68 ResourceId IdAllocator::LastUsedId() const {
69 if (used_ids_.empty()) {
70 return 0u;
71 } else {
72 return *used_ids_.rbegin();
73 }
74 }
75
76 ResourceId IdAllocator::FindFirstUnusedId() const {
44 ResourceId id = 1; 77 ResourceId id = 1;
45 for (ResourceIdSet::const_iterator it = used_ids_.begin(); 78 for (ResourceIdSet::const_iterator it = used_ids_.begin();
46 it != used_ids_.end(); ++it) { 79 it != used_ids_.end(); ++it) {
47 if ((*it) != id) { 80 if ((*it) != id) {
48 return id; 81 return id;
49 } 82 }
50 ++id; 83 ++id;
51 } 84 }
52 return id; 85 return id;
53 } 86 }
54 87
55 } // namespace gpu 88 } // namespace gpu
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698