Skip to content

Commit f6910fd

Browse files
committed
Simplify ArrayTable
Signed-off-by: Krzysztof Bieganski <[email protected]>
1 parent 39d07c8 commit f6910fd

File tree

3 files changed

+24
-36
lines changed

3 files changed

+24
-36
lines changed

graph/Graph.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ Graph::makePrevPaths(Vertex *vertex,
657657
}
658658

659659
PathVertexRep *
660-
Graph::prevPaths(Vertex *vertex) const
660+
Graph::prevPaths(Vertex *vertex)
661661
{
662662
return prev_paths_.pointer(vertex->prevPaths());
663663
}

include/sta/ArrayTable.hh

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public:
4747
uint32_t count);
4848
// Grow as necessary and return pointer for id.
4949
TYPE *ensureId(ObjectId id);
50-
TYPE *pointer(ObjectId id) const;
51-
TYPE &ref(ObjectId id) const;
50+
TYPE *pointer(ObjectId id);
51+
TYPE &ref(ObjectId id);
5252
size_t size() const { return size_; }
5353
void clear();
5454

@@ -66,11 +66,8 @@ private:
6666
BlockIdx free_block_idx_;
6767
// Index of next free object in free_block_idx_.
6868
ObjectIdx free_idx_;
69-
// Don't use std::vector so growing blocks_ can be thread safe.
70-
size_t blocks_size_;
71-
size_t blocks_capacity_;
72-
ArrayBlock<TYPE> *blocks_;
73-
ArrayBlock<TYPE> *prev_blocks_;
69+
std::vector<ArrayBlock<TYPE>> blocks_;
70+
std::vector<ArrayBlock<TYPE>> prev_blocks_;
7471
// Linked list of free arrays indexed by array size.
7572
std::vector<ObjectId> free_list_;
7673
static constexpr ObjectId idx_mask_ = block_size - 1;
@@ -80,28 +77,24 @@ template <class TYPE>
8077
ArrayTable<TYPE>::ArrayTable() :
8178
size_(0),
8279
free_block_idx_(block_idx_null),
83-
free_idx_(object_idx_null),
84-
blocks_size_(0),
85-
blocks_capacity_(1024),
86-
blocks_(new ArrayBlock<TYPE>[blocks_capacity_]),
87-
prev_blocks_(nullptr)
80+
free_idx_(object_idx_null)
8881
{
82+
blocks_.reserve(1024);
8983
}
9084

9185
template <class TYPE>
9286
ArrayTable<TYPE>::~ArrayTable()
9387
{
9488
deleteBlocks();
95-
delete [] blocks_;
96-
delete [] prev_blocks_;
9789
}
9890

9991
template <class TYPE>
10092
void
10193
ArrayTable<TYPE>::deleteBlocks()
10294
{
103-
for (size_t i = 0; i < blocks_size_; i++)
95+
for (size_t i = 0; i < blocks_.size(); i++)
10496
blocks_[i].free();
97+
blocks_.clear();
10598
}
10699

107100
template <class TYPE>
@@ -120,12 +113,12 @@ ArrayTable<TYPE>::make(uint32_t count,
120113
free_list_[count] = *head;
121114
}
122115
else {
123-
ArrayBlock<TYPE> *block = blocks_size_ ? &blocks_[free_block_idx_] : nullptr;
116+
ArrayBlock<TYPE> *block = blocks_.size() ? &blocks_[free_block_idx_] : nullptr;
124117
if ((free_idx_ == object_idx_null
125118
&& free_block_idx_ == block_idx_null)
126119
|| free_idx_ + count >= block->size()) {
127120
uint32_t size = block_size;
128-
if (blocks_size_ == 0
121+
if (blocks_.size() == 0
129122
// First block starts at idx 1.
130123
&& count > block_size - 1)
131124
size = count + 1;
@@ -145,7 +138,7 @@ template <class TYPE>
145138
ArrayBlock<TYPE> *
146139
ArrayTable<TYPE>::makeBlock(uint32_t size)
147140
{
148-
BlockIdx block_idx = blocks_size_;
141+
BlockIdx block_idx = blocks_.size();
149142
pushBlock(size);
150143
free_block_idx_ = block_idx;
151144
// ObjectId zero is reserved for object_id_null.
@@ -157,21 +150,17 @@ template <class TYPE>
157150
void
158151
ArrayTable<TYPE>::pushBlock(size_t size)
159152
{
160-
auto blk_idx = blocks_size_++;
161-
blocks_[blk_idx] = ArrayBlock<TYPE>(size);
153+
blocks_.push_back(ArrayBlock<TYPE>(size));
162154

163-
if (blocks_size_ >= block_id_max)
155+
if (blocks_.size() >= block_id_max)
164156
criticalError(223, "max array table block count exceeded.");
165-
if (blocks_size_ == blocks_capacity_) {
166-
size_t new_capacity = blocks_capacity_ * 1.5;
167-
ArrayBlock<TYPE>* new_blocks = new ArrayBlock<TYPE>[new_capacity];
168-
memcpy(new_blocks, blocks_, blocks_capacity_ * sizeof(ArrayBlock<TYPE>));
169-
if (prev_blocks_)
170-
delete [] prev_blocks_;
157+
if (blocks_.size() == blocks_.capacity()) {
158+
prev_blocks_.reserve(blocks_.capacity() * 1.5);
159+
const auto blocks_mid = blocks_.begin() + prev_blocks_.size();
160+
std::copy(blocks_.begin(), blocks_mid, prev_blocks_.begin());
161+
prev_blocks_.insert(prev_blocks_.end(), blocks_mid, blocks_.end());
171162
// Preserve block array for other threads to reference.
172-
prev_blocks_ = blocks_;
173-
blocks_ = new_blocks;
174-
blocks_capacity_ = new_capacity;
163+
blocks_.swap(prev_blocks_);
175164
}
176165
}
177166

@@ -192,7 +181,7 @@ ArrayTable<TYPE>::destroy(ObjectId id,
192181

193182
template <class TYPE>
194183
TYPE *
195-
ArrayTable<TYPE>::pointer(ObjectId id) const
184+
ArrayTable<TYPE>::pointer(ObjectId id)
196185
{
197186
if (id == object_id_null)
198187
return nullptr;
@@ -210,15 +199,15 @@ ArrayTable<TYPE>::ensureId(ObjectId id)
210199
BlockIdx blk_idx = id >> idx_bits;
211200
ObjectIdx obj_idx = id & idx_mask_;
212201
// Make enough blocks for blk_idx to be valid.
213-
for (BlockIdx i = blocks_size_; i <= blk_idx; i++) {
202+
for (BlockIdx i = blocks_.size(); i <= blk_idx; i++) {
214203
pushBlock(block_size);
215204
}
216205
return blocks_[blk_idx].pointer(obj_idx);
217206
}
218207

219208
template <class TYPE>
220209
TYPE &
221-
ArrayTable<TYPE>::ref(ObjectId id) const
210+
ArrayTable<TYPE>::ref(ObjectId id)
222211
{
223212
if (id == object_id_null)
224213
criticalError(222, "null ObjectId reference is undefined.");
@@ -233,7 +222,6 @@ void
233222
ArrayTable<TYPE>::clear()
234223
{
235224
deleteBlocks();
236-
blocks_size_ = 0;
237225
size_ = 0;
238226
free_block_idx_ = block_idx_null;
239227
free_idx_ = object_idx_null;

include/sta/Graph.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public:
113113
size_t requiredCount() const { return requireds_.size(); }
114114
PathVertexRep *makePrevPaths(Vertex *vertex,
115115
uint32_t count);
116-
PathVertexRep *prevPaths(Vertex *vertex) const;
116+
PathVertexRep *prevPaths(Vertex *vertex);
117117
void clearPrevPaths();
118118
// Reported slew are the same as those in the liberty tables.
119119
// reported_slews = measured_slews / slew_derate_from_library

0 commit comments

Comments
 (0)