-
Notifications
You must be signed in to change notification settings - Fork 14
Combobox optimizations #2861
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
Draft
charliepark
wants to merge
10
commits into
main
Choose a base branch
from
combobox-optimizations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Combobox optimizations #2861
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
48a317d
Replace Listbox in Create Disk side modal with Combobox
septum b3a0a7d
Update e2e tests to account for the comboboxes
septum a01a795
Fix issues raised by CI pipeline
septum 7f6b651
Fix issues stated in code review
septum 19621a5
Revert changes in placeholders
septum 84a9473
Merge branch 'main' into create-disk-combobox
charliepark 5c7b0ee
refactor combobox input editing and add tests
charliepark da2f383
add mock images and stress test combobox; memoize as much as possible
charliepark ff21799
More memoizations and attempts to speed up Chrome rendering for large…
charliepark e413356
virtualize long combobox lists
charliepark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,10 +20,10 @@ import { | |
type Image, | ||
} from '@oxide/api' | ||
|
||
import { ComboboxField } from '~/components/form/fields/ComboboxField' | ||
import { DescriptionField } from '~/components/form/fields/DescriptionField' | ||
import { DiskSizeField } from '~/components/form/fields/DiskSizeField' | ||
import { toImageComboboxItem } from '~/components/form/fields/ImageSelectField' | ||
import { ListboxField } from '~/components/form/fields/ListboxField' | ||
import { NameField } from '~/components/form/fields/NameField' | ||
import { RadioField } from '~/components/form/fields/RadioField' | ||
import { SideModalForm } from '~/components/form/SideModalForm' | ||
|
@@ -85,32 +85,66 @@ export function CreateDiskSideModalForm({ | |
const projectImages = useApiQuery('imageList', { query: { project } }) | ||
const siloImages = useApiQuery('imageList', {}) | ||
|
||
// Memoize real images array to prevent recreation on every render | ||
// put project images first because if there are any, there probably aren't | ||
// very many and they're probably relevant | ||
const images = useMemo( | ||
const realImages = useMemo( | ||
() => [...(projectImages.data?.items || []), ...(siloImages.data?.items || [])], | ||
[projectImages.data, siloImages.data] | ||
[projectImages.data?.items, siloImages.data?.items] | ||
) | ||
|
||
// Memoize mock images array (only create once) | ||
const mockImages = useMemo((): Image[] => { | ||
// TODO: REMOVE THIS AFTER STRESS TESTING IS DONE | ||
// Generate 1000 mock items for stress testing | ||
return Array.from({ length: 1000 }, (_, i) => ({ | ||
id: `mock-image-${i}`, | ||
name: `Mock Image ${i.toString().padStart(4, '0')}`, | ||
size: 1073741824, // 1GB | ||
version: '1.0.0', | ||
description: `This is mock image ${i} for stress testing the combobox`, | ||
digest: { | ||
type: 'sha256', | ||
value: '0'.repeat(64), | ||
}, | ||
blockSize: 512, | ||
timeCreated: new Date(), | ||
timeModified: new Date(), | ||
os: 'linux', | ||
})) | ||
}, []) | ||
|
||
// Memoize combined images array | ||
const images = useMemo(() => [...realImages, ...mockImages], [realImages, mockImages]) | ||
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. Again suspect an improvement is negligible here, suspect spreading two arrays is relatively inexpensive |
||
const areImagesLoading = projectImages.isPending || siloImages.isPending | ||
|
||
const snapshotsQuery = useApiQuery('snapshotList', { query: { project } }) | ||
const snapshots = snapshotsQuery.data?.items || [] | ||
const snapshots = useMemo( | ||
() => snapshotsQuery.data?.items || [], | ||
[snapshotsQuery.data?.items] | ||
) | ||
|
||
// validate disk source size | ||
const diskSource = form.watch('diskSource').type | ||
// Use useController for targeted watching instead of form.watch() to reduce re-renders | ||
const diskSourceController = useController({ control: form.control, name: 'diskSource' }) | ||
const diskSource = diskSourceController.field.value.type | ||
|
||
let validateSizeGiB: number | undefined = undefined | ||
if (diskSource === 'snapshot') { | ||
const selectedSnapshotId = form.watch('diskSource.snapshotId') | ||
const selectedSnapshotSize = snapshots.find( | ||
(snapshot) => snapshot.id === selectedSnapshotId | ||
)?.size | ||
validateSizeGiB = selectedSnapshotSize ? bytesToGiB(selectedSnapshotSize) : undefined | ||
} else if (diskSource === 'image') { | ||
const selectedImageId = form.watch('diskSource.imageId') | ||
const selectedImageSize = images.find((image) => image.id === selectedImageId)?.size | ||
validateSizeGiB = selectedImageSize ? bytesToGiB(selectedImageSize) : undefined | ||
} | ||
// Memoize size validation to avoid expensive lookups on every render | ||
const validateSizeGiB = useMemo(() => { | ||
if (diskSource === 'snapshot') { | ||
const selectedSnapshotId = diskSourceController.field.value.snapshotId | ||
if (!selectedSnapshotId) return undefined | ||
const selectedSnapshotSize = snapshots.find( | ||
(snapshot) => snapshot.id === selectedSnapshotId | ||
)?.size | ||
return selectedSnapshotSize ? bytesToGiB(selectedSnapshotSize) : undefined | ||
} else if (diskSource === 'image') { | ||
const selectedImageId = diskSourceController.field.value.imageId | ||
if (!selectedImageId) return undefined | ||
const selectedImageSize = images.find((image) => image.id === selectedImageId)?.size | ||
return selectedImageSize ? bytesToGiB(selectedImageSize) : undefined | ||
} | ||
return undefined | ||
}, [diskSource, diskSourceController.field.value, snapshots, images]) | ||
|
||
return ( | ||
<SideModalForm | ||
|
@@ -172,6 +206,12 @@ const DiskSourceField = ({ | |
} = useController({ control, name: 'diskSource' }) | ||
const diskSizeField = useController({ control, name: 'size' }).field | ||
|
||
// Memoize the expensive toImageComboboxItem mapping to avoid recalculating on every render | ||
const imageComboboxItems = useMemo( | ||
() => images.map((i) => toImageComboboxItem(i, true)), | ||
[images] | ||
) | ||
|
||
return ( | ||
<> | ||
<div className="max-w-lg space-y-2"> | ||
|
@@ -210,16 +250,17 @@ const DiskSourceField = ({ | |
/> | ||
)} | ||
{value.type === 'image' && ( | ||
<ListboxField | ||
<ComboboxField | ||
control={control} | ||
name="diskSource.imageId" | ||
label="Source image" | ||
placeholder="Select an image" | ||
isLoading={areImagesLoading} | ||
items={images.map((i) => toImageComboboxItem(i, true))} | ||
items={imageComboboxItems} | ||
required | ||
onChange={(id) => { | ||
const image = images.find((i) => i.id === id)! // if it's selected, it must be present | ||
const image = images.find((i) => i.id === id) | ||
if (!image) return | ||
const imageSizeGiB = image.size / GiB | ||
if (diskSizeField.value < imageSizeGiB) { | ||
diskSizeField.onChange(diskSizeNearest10(imageSizeGiB)) | ||
|
@@ -250,16 +291,16 @@ const SnapshotSelectField = ({ control }: { control: Control<DiskCreate> }) => { | |
const { project } = useProjectSelector() | ||
const snapshotsQuery = useApiQuery('snapshotList', { query: { project } }) | ||
|
||
const snapshots = snapshotsQuery.data?.items || [] | ||
const snapshots = useMemo( | ||
() => snapshotsQuery.data?.items || [], | ||
[snapshotsQuery.data?.items] | ||
) | ||
const diskSizeField = useController({ control, name: 'size' }).field | ||
|
||
return ( | ||
<ListboxField | ||
control={control} | ||
name="diskSource.snapshotId" | ||
label="Source snapshot" | ||
placeholder="Select a snapshot" | ||
items={snapshots.map((i) => { | ||
// Memoize the expensive snapshot ComboboxItem mapping to avoid recalculating on every render | ||
const snapshotComboboxItems = useMemo( | ||
() => | ||
snapshots.map((i) => { | ||
const formattedSize = filesize(i.size, { base: 2, output: 'object' }) | ||
return { | ||
value: i.id, | ||
|
@@ -275,7 +316,17 @@ const SnapshotSelectField = ({ control }: { control: Control<DiskCreate> }) => { | |
</> | ||
), | ||
} | ||
})} | ||
}), | ||
[snapshots] | ||
) | ||
|
||
return ( | ||
<ComboboxField | ||
control={control} | ||
name="diskSource.snapshotId" | ||
label="Source snapshot" | ||
placeholder="Select a snapshot" | ||
items={snapshotComboboxItems} | ||
isLoading={snapshotsQuery.isPending} | ||
required | ||
onChange={(id) => { | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'd be surprised if this memo has a meaningful effect on performance
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.
It could even be more computation than not memoizing — the
useMemo
call and its dep checks are work too. The point of this would be to prevent the value changing to avoid triggering renders on<Combobox>
below, but this is just a string and it wouldn't be changing its value between renders, so it is almost certainly triggering re-renders.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.
Why would this trigger a re-render – since
description
,placeholder
andallowArbitraryValues
are likely the same values across the lifetime of this component. So the prop on<Combobox>
wouldn't change.Uh oh!
There was an error while loading. Please reload this page.
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.
Yeah, that's what I meant about it being a string. If it was constructing an object or array with a spread like the one in disk create, then it would be a new one every time (even if the underlying values are the same) and then it would trigger renders when passed down the chain. So the other ones are more plausible, though in those cases it's still worth determining whether they have much effect. Triggering renders is not always a problem.
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.
Yeah, the memoization explosion was from before I added in the virtualization; was going to try backing them out to see whether they were having a noticeable effect, but will read the other comments on this first