-
Notifications
You must be signed in to change notification settings - Fork 24.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add "CellRenderMask" Region Tracking Structure #31420
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict | ||
* @format | ||
*/ | ||
|
||
import invariant from 'invariant'; | ||
|
||
export type CellRegion = { | ||
first: number, | ||
last: number, | ||
isSpacer: boolean, | ||
}; | ||
|
||
export class CellRenderMask { | ||
_numCells: number; | ||
_regions: Array<CellRegion>; | ||
|
||
constructor(numCells: number) { | ||
invariant( | ||
numCells >= 0, | ||
'CellRenderMask must contain a non-negative number os cells', | ||
); | ||
|
||
this._numCells = numCells; | ||
|
||
if (numCells === 0) { | ||
this._regions = []; | ||
} else { | ||
this._regions = [ | ||
{ | ||
first: 0, | ||
last: numCells - 1, | ||
isSpacer: true, | ||
}, | ||
]; | ||
} | ||
} | ||
|
||
enumerateRegions(): $ReadOnlyArray<CellRegion> { | ||
return this._regions; | ||
} | ||
|
||
addCells(cells: {first: number, last: number}) { | ||
invariant( | ||
cells.first >= 0 && | ||
cells.first < this._numCells && | ||
cells.last >= 0 && | ||
cells.last < this._numCells && | ||
cells.last >= cells.first, | ||
'CellRenderMask.addCells called with invalid cell range', | ||
); | ||
|
||
const [firstIntersect, firstIntersectIdx] = this._findRegion(cells.first); | ||
const [lastIntersect, lastIntersectIdx] = this._findRegion(cells.last); | ||
|
||
// Fast-path if the cells to add are already all present in the mask. We | ||
// will otherwise need to do some mutation. | ||
if (firstIntersectIdx === lastIntersectIdx && !firstIntersect.isSpacer) { | ||
return; | ||
} | ||
|
||
// We need to replace the existing covered regions with 1-3 new regions | ||
// depending whether we need to split spacers out of overlapping regions. | ||
const newLeadRegion: Array<CellRegion> = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are these arrays? Is it just for ease of spreading? I guess in that case could we create the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, that is why they are arrays. Meant to represent that there may be zero or one region. We could create replacementRegions a bit earlier and push. Might be marginally more efficient, but also maybe less clear to reason about. |
||
const newTailRegion: Array<CellRegion> = []; | ||
const newMainRegion: CellRegion = { | ||
...cells, | ||
isSpacer: false, | ||
}; | ||
|
||
if (firstIntersect.first < newMainRegion.first) { | ||
if (firstIntersect.isSpacer) { | ||
newLeadRegion.push({ | ||
first: firstIntersect.first, | ||
last: newMainRegion.first - 1, | ||
isSpacer: true, | ||
}); | ||
} else { | ||
newMainRegion.first = firstIntersect.first; | ||
} | ||
} | ||
|
||
if (lastIntersect.last > newMainRegion.last) { | ||
if (lastIntersect.isSpacer) { | ||
newTailRegion.push({ | ||
first: newMainRegion.last + 1, | ||
last: lastIntersect.last, | ||
isSpacer: true, | ||
}); | ||
} else { | ||
newMainRegion.last = lastIntersect.last; | ||
} | ||
} | ||
|
||
const replacementRegions: Array<CellRegion> = [ | ||
...newLeadRegion, | ||
newMainRegion, | ||
...newTailRegion, | ||
]; | ||
const numRegionsToDelete = lastIntersectIdx - firstIntersectIdx + 1; | ||
this._regions.splice( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be simpler to create a new regions array? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't necessarily think so. We would still need to figure out how to split existing intersecting cells when iterating through them. It would also be less efficient if we ever had large masks since we would need to rebuild the whole buffer. |
||
firstIntersectIdx, | ||
numRegionsToDelete, | ||
...replacementRegions, | ||
); | ||
} | ||
|
||
equals(other: CellRenderMask): boolean { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add test cases for this? What's the use case for checking equality for render masks? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can add tests for this. The use-case is comparing newly derived state to the existing, for whether to return |
||
return ( | ||
this._numCells === other._numCells && | ||
this._regions.length === other._regions.length && | ||
this._regions.every( | ||
(region, i) => | ||
region.first === other._regions[i].first && | ||
region.last === other._regions[i].last && | ||
region.isSpacer === other._regions[i].isSpacer, | ||
) | ||
); | ||
} | ||
|
||
_findRegion(cellIdx: number): [CellRegion, number] { | ||
let firstIdx = 0; | ||
let lastIdx = this._regions.length - 1; | ||
|
||
while (firstIdx <= lastIdx) { | ||
const middleIdx = Math.floor((firstIdx + lastIdx) / 2); | ||
const middleRegion = this._regions[middleIdx]; | ||
|
||
if (cellIdx >= middleRegion.first && cellIdx <= middleRegion.last) { | ||
return [middleRegion, middleIdx]; | ||
} else if (cellIdx < middleRegion.first) { | ||
lastIdx = middleIdx - 1; | ||
} else if (cellIdx > middleRegion.last) { | ||
firstIdx = middleIdx + 1; | ||
} | ||
} | ||
|
||
invariant(false, `A region was not found containing cellIdx ${cellIdx}`); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict | ||
* @format | ||
*/ | ||
|
||
import {CellRenderMask} from '../CellRenderMask'; | ||
|
||
describe('CellRenderMask', () => { | ||
it('throws when constructed with invalid size', () => { | ||
expect(() => new CellRenderMask(-1)).toThrow(); | ||
}); | ||
|
||
it('allows creation of empty mask', () => { | ||
const renderMask = new CellRenderMask(0); | ||
expect(renderMask.enumerateRegions()).toEqual([]); | ||
}); | ||
|
||
it('allows creation of single-cell mask', () => { | ||
const renderMask = new CellRenderMask(1); | ||
expect(renderMask.enumerateRegions()).toEqual([ | ||
{first: 0, last: 0, isSpacer: true}, | ||
]); | ||
}); | ||
|
||
it('throws when adding invalid cell ranges', () => { | ||
const renderMask = new CellRenderMask(5); | ||
|
||
expect(() => renderMask.addCells({first: -1, last: 0})).toThrow(); | ||
expect(() => renderMask.addCells({first: 1, last: 0})).toThrow(); | ||
expect(() => renderMask.addCells({first: 0, last: 5})).toThrow(); | ||
expect(() => renderMask.addCells({first: 6, last: 7})).toThrow(); | ||
}); | ||
|
||
it('allows adding single cell at beginning', () => { | ||
const renderMask = new CellRenderMask(5); | ||
renderMask.addCells({first: 0, last: 0}); | ||
|
||
expect(renderMask.enumerateRegions()).toEqual([ | ||
{first: 0, last: 0, isSpacer: false}, | ||
{first: 1, last: 4, isSpacer: true}, | ||
]); | ||
}); | ||
|
||
it('allows adding single cell at end', () => { | ||
const renderMask = new CellRenderMask(5); | ||
renderMask.addCells({first: 4, last: 4}); | ||
|
||
expect(renderMask.enumerateRegions()).toEqual([ | ||
{first: 0, last: 3, isSpacer: true}, | ||
{first: 4, last: 4, isSpacer: false}, | ||
]); | ||
}); | ||
|
||
it('allows adding single cell in middle', () => { | ||
const renderMask = new CellRenderMask(5); | ||
renderMask.addCells({first: 2, last: 2}); | ||
|
||
expect(renderMask.enumerateRegions()).toEqual([ | ||
{first: 0, last: 1, isSpacer: true}, | ||
{first: 2, last: 2, isSpacer: false}, | ||
{first: 3, last: 4, isSpacer: true}, | ||
]); | ||
}); | ||
|
||
it('allows marking entire cell range', () => { | ||
const renderMask = new CellRenderMask(5); | ||
renderMask.addCells({first: 0, last: 4}); | ||
|
||
expect(renderMask.enumerateRegions()).toEqual([ | ||
{first: 0, last: 4, isSpacer: false}, | ||
]); | ||
}); | ||
|
||
it('correctly replaces fragmented cell ranges', () => { | ||
const renderMask = new CellRenderMask(10); | ||
|
||
renderMask.addCells({first: 3, last: 3}); | ||
renderMask.addCells({first: 5, last: 7}); | ||
|
||
expect(renderMask.enumerateRegions()).toEqual([ | ||
{first: 0, last: 2, isSpacer: true}, | ||
{first: 3, last: 3, isSpacer: false}, | ||
{first: 4, last: 4, isSpacer: true}, | ||
{first: 5, last: 7, isSpacer: false}, | ||
{first: 8, last: 9, isSpacer: true}, | ||
]); | ||
|
||
renderMask.addCells({first: 3, last: 7}); | ||
|
||
expect(renderMask.enumerateRegions()).toEqual([ | ||
{first: 0, last: 2, isSpacer: true}, | ||
{first: 3, last: 7, isSpacer: false}, | ||
{first: 8, last: 9, isSpacer: true}, | ||
]); | ||
}); | ||
|
||
it('left-expands region', () => { | ||
const renderMask = new CellRenderMask(5); | ||
|
||
renderMask.addCells({first: 3, last: 3}); | ||
|
||
expect(renderMask.enumerateRegions()).toEqual([ | ||
{first: 0, last: 2, isSpacer: true}, | ||
{first: 3, last: 3, isSpacer: false}, | ||
{first: 4, last: 4, isSpacer: true}, | ||
]); | ||
|
||
renderMask.addCells({first: 2, last: 3}); | ||
|
||
expect(renderMask.enumerateRegions()).toEqual([ | ||
{first: 0, last: 1, isSpacer: true}, | ||
{first: 2, last: 3, isSpacer: false}, | ||
{first: 4, last: 4, isSpacer: true}, | ||
]); | ||
}); | ||
|
||
it('right-expands region', () => { | ||
const renderMask = new CellRenderMask(5); | ||
|
||
renderMask.addCells({first: 3, last: 3}); | ||
|
||
expect(renderMask.enumerateRegions()).toEqual([ | ||
{first: 0, last: 2, isSpacer: true}, | ||
{first: 3, last: 3, isSpacer: false}, | ||
{first: 4, last: 4, isSpacer: true}, | ||
]); | ||
|
||
renderMask.addCells({first: 3, last: 4}); | ||
|
||
expect(renderMask.enumerateRegions()).toEqual([ | ||
{first: 0, last: 2, isSpacer: true}, | ||
{first: 3, last: 4, isSpacer: false}, | ||
]); | ||
}); | ||
|
||
it('left+right expands region', () => { | ||
const renderMask = new CellRenderMask(5); | ||
|
||
renderMask.addCells({first: 3, last: 3}); | ||
|
||
expect(renderMask.enumerateRegions()).toEqual([ | ||
{first: 0, last: 2, isSpacer: true}, | ||
{first: 3, last: 3, isSpacer: false}, | ||
{first: 4, last: 4, isSpacer: true}, | ||
]); | ||
|
||
renderMask.addCells({first: 2, last: 4}); | ||
|
||
expect(renderMask.enumerateRegions()).toEqual([ | ||
{first: 0, last: 1, isSpacer: true}, | ||
{first: 2, last: 4, isSpacer: false}, | ||
]); | ||
}); | ||
|
||
it('does nothing when adding existing cells', () => { | ||
const renderMask = new CellRenderMask(5); | ||
|
||
renderMask.addCells({first: 2, last: 3}); | ||
|
||
expect(renderMask.enumerateRegions()).toEqual([ | ||
{first: 0, last: 1, isSpacer: true}, | ||
{first: 2, last: 3, isSpacer: false}, | ||
{first: 4, last: 4, isSpacer: true}, | ||
]); | ||
|
||
renderMask.addCells({first: 3, last: 3}); | ||
|
||
expect(renderMask.enumerateRegions()).toEqual([ | ||
{first: 0, last: 1, isSpacer: true}, | ||
{first: 2, last: 3, isSpacer: false}, | ||
{first: 4, last: 4, isSpacer: true}, | ||
]); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious why we don't also need removeCells. I presume you may want to remove entire ranges as well as things virtualize away.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current VirtualizedList implementation I have in #31422 will compute a whole new mask if inputs change, instead of trying to reason about the existing one.
A cell can have multiple conditions that would cause it to be rendered, so we do not know at the absense of just one condition that the cell should not be rendered. So it becomes tricky to mutate the existing mask.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a use case of adding cells that are spacers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The render function in the linked PR above gives a good example of usage. When we hit spacers we still need to add views to the scroll view. So this structure allows us to iterate through regions and either add cells, or if a spacer, add the spacer view. If we omitted spacers we would need to derive where they were in VirtualizedList.