Skip to content

Commit 69cf662

Browse files
some more perf - use reference instead of copying
1 parent ea07df7 commit 69cf662

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

yoga/algorithm/grid/AutoPlacement.h

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <yoga/node/Node.h>
1212
#include <vector>
1313
#include <yoga/style/GridLine.h>
14-
#include <map>
14+
#include <unordered_map>
1515
#include <unordered_set>
1616

1717
namespace facebook::yoga {
@@ -155,7 +155,7 @@ struct AutoPlacement {
155155
// Step 1: Position anything that's not auto-positioned.
156156
// In spec level 1, span is always definite. Default is 1.
157157
// So for grid position to be definite, we need either start or end to be definite.
158-
for (auto child: node->getChildren()) {
158+
for (const auto& child: node->getChildren()) {
159159
if (child->style().positionType() == PositionType::Absolute) {
160160
continue;
161161
}
@@ -194,8 +194,8 @@ struct AutoPlacement {
194194

195195
// Step 2: Process the items locked to a given row.
196196
// Definite row positions only, exclude items with definite column positions.
197-
std::map<int32_t, int32_t> rowStartToColumnStartCache;
198-
for (auto child: node->getChildren()) {
197+
std::unordered_map<int32_t, int32_t> rowStartToColumnStartCache;
198+
for (const auto& child: node->getChildren()) {
199199
if (child->style().positionType() == PositionType::Absolute) {
200200
continue;
201201
}
@@ -215,9 +215,8 @@ struct AutoPlacement {
215215
auto rowStart = rowPlacement.start;
216216
auto rowEnd = rowPlacement.end;
217217

218-
auto columnStart = rowStartToColumnStartCache.find(rowStart) ==
219-
rowStartToColumnStartCache.end() ?
220-
minColumnStart : rowStartToColumnStartCache[rowStart];
218+
auto columnStart = rowStartToColumnStartCache.contains(rowStart) ?
219+
rowStartToColumnStartCache[rowStart] : minColumnStart;
221220

222221
auto columnPlacement = GridItemTrackPlacement::resolveLinePlacement(gridItemColumnStart, gridItemColumnEnd, explicitColumnLineCount);
223222
auto columnSpan = columnPlacement.span;
@@ -234,7 +233,7 @@ struct AutoPlacement {
234233
child
235234
};
236235

237-
for (auto placedItem: gridItemAreas) {
236+
for (const auto& placedItem: gridItemAreas) {
238237
if (gridItemArea.overlaps(placedItem)) {
239238
columnStart++;
240239
columnEnd = columnStart + columnSpan;
@@ -255,7 +254,7 @@ struct AutoPlacement {
255254
// Step 3: Determine the columns in the implicit grid.
256255
// TODO: we dont need this loop. we can do it in above steps. But keeping it for now, to match the spec.
257256
auto largestColumnSpan = 1;
258-
for (auto child: node->getChildren()) {
257+
for (const auto& child: node->getChildren()) {
259258
if (child->style().positionType() == PositionType::Absolute) {
260259
continue;
261260
}
@@ -291,7 +290,7 @@ struct AutoPlacement {
291290
minColumnStart,
292291
minRowStart
293292
};
294-
for (auto child: node->getChildren()) {
293+
for (const auto& child: node->getChildren()) {
295294
if (child->style().positionType() == PositionType::Absolute) {
296295
continue;
297296
}
@@ -342,7 +341,7 @@ struct AutoPlacement {
342341
};
343342
bool hasOverlap = false;
344343

345-
for (auto placedItem: gridItemAreas) {
344+
for (const auto& placedItem: gridItemAreas) {
346345
if (proposedPlacement.overlaps(placedItem)) {
347346
hasOverlap = true;
348347
break;
@@ -384,7 +383,7 @@ struct AutoPlacement {
384383
};
385384
bool hasOverlap = false;
386385

387-
for (auto placedItem: gridItemAreas) {
386+
for (const auto& placedItem: gridItemAreas) {
388387
if (proposedPlacement.overlaps(placedItem)) {
389388
hasOverlap = true;
390389
break;

yoga/algorithm/grid/GridLayout.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,7 @@ void calculateGridLayoutInternal(Node* node,
408408
}
409409

410410
float alignSelfOffset = 0.0f;
411-
if (alignSelf == Align::Start) {
412-
alignSelfOffset = 0.0f;
413-
} else if (alignSelf == Align::End) {
411+
if (alignSelf == Align::End) {
414412
alignSelfOffset = freeSpaceBlockAxis;
415413
} else if (alignSelf == Align::Center) {
416414
alignSelfOffset = freeSpaceBlockAxis / 2;

yoga/algorithm/grid/TrackSizing.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <yoga/numeric/Comparison.h>
1111
#include <yoga/style/StyleSizeLength.h>
1212
#include <unordered_set>
13+
#include <unordered_map>
14+
#include <map>
1315

1416
namespace facebook::yoga {
1517

@@ -140,6 +142,8 @@ struct TrackSizing {
140142
// https://www.w3.org/TR/css-grid-1/#algo-spanning-items
141143
void accomodateSpanningItemsCrossingContentSizedTracks(Dimension dimension) {
142144
auto& tracks = dimension == Dimension::Width ? columnTracks : rowTracks;
145+
// https://en.cppreference.com/w/cpp/container/map.html
146+
// we use map here because we want to store span in increasing order, map keys are sorted automatically in ascending order
143147
std::map<size_t, std::vector<std::pair<GridItemArea, std::vector<GridTrackSize*>>>> itemsGroupedByIncreasingSpan;
144148
for (auto& item: gridItemAreas) {
145149
auto spannedTracks = getTracksSpannedByItem(item, tracks, dimension);
@@ -153,7 +157,6 @@ struct TrackSizing {
153157
auto containerSize = dimension == Dimension::Width ? containerInnerWidth : containerInnerHeight;
154158
auto sizingMode = dimension == Dimension::Width ? widthSizingMode : heightSizingMode;
155159

156-
// we need to proceed in increasing order of span, keys are automatically sorted in std::map
157160
for (const auto& [span, itemsWithTracks] : itemsGroupedByIncreasingSpan) {
158161
for (const auto& [item, spannedTracks] : itemsWithTracks) {
159162
std::vector<GridTrackSize*> intrinsicMinimumSizingFunctionTracks;
@@ -248,8 +251,10 @@ struct TrackSizing {
248251
auto containerSize = dimension == Dimension::Width ? containerInnerWidth : containerInnerHeight;
249252

250253
// 1. Set planned increase to 0 for all affected tracks
251-
std::map<GridTrackSize*, float> plannedIncrease;
252-
std::map<GridTrackSize*, float> itemIncurredIncrease;
254+
std::unordered_map<GridTrackSize*, float> plannedIncrease;
255+
std::unordered_map<GridTrackSize*, float> itemIncurredIncrease;
256+
plannedIncrease.reserve(affectedTracks.size());
257+
itemIncurredIncrease.reserve(affectedTracks.size());
253258
for (auto& track: affectedTracks) {
254259
plannedIncrease[track] = 0.0f;
255260
itemIncurredIncrease[track] = 0.0f;
@@ -356,8 +361,10 @@ struct TrackSizing {
356361
auto containerSize = dimension == Dimension::Width ? containerInnerWidth : containerInnerHeight;
357362

358363
// 1. Set planned increase to 0 for all affected tracks
359-
std::map<GridTrackSize*, float> plannedIncrease;
360-
std::map<GridTrackSize*, float> itemIncurredIncrease;
364+
std::unordered_map<GridTrackSize*, float> plannedIncrease;
365+
std::unordered_map<GridTrackSize*, float> itemIncurredIncrease;
366+
plannedIncrease.reserve(affectedTracks.size());
367+
itemIncurredIncrease.reserve(affectedTracks.size());
361368
for (auto& track: affectedTracks) {
362369
plannedIncrease[track] = 0.0f;
363370
itemIncurredIncrease[track] = 0.0f;

0 commit comments

Comments
 (0)