Skip to content

Conversation

Variable-ind
Copy link
Contributor

@Variable-ind Variable-ind commented Jul 15, 2025

This introduces two independent optimizations:

1. lock_selection_rect optimization:

Stores existing selection rect as cache and forces get_selection_rect in SelectionMap to use cache instead. use this to save time when the rect isn't meant to change between repeated calls (e.g see it's use in Bucket tool _flood_fill). This causes roughly an 87% boost to the area fill algorithm

2. area_fill compatible with selection size:

when segments were created in _compute_segments_for_image

for j in image.get_height():
	_add_new_segment(j)

When we have selection, this ends up adding segments not necessary for calculation. As an optimization, i change it to

var y_range = [0, image.get_height()]
	_area_end_idx = project.size.y - 1
	_area_start_idx = 0
	if project.has_selection:
		var selection_rect := project.selection_map.get_selection_rect(project)
		_area_start_idx = selection_rect.position.y
		_area_end_idx = selection_rect.end.y - 1
		y_range = [selection_rect.position.y, selection_rect.end.y]
	for j in range(y_range[0], y_range[1]):
		_add_new_segment(j)

This dramatically decreases the fill time even further.

Benchmark of area_fill on a 30x30 selection in a 646x646 canvas

Version Area fill time (msec)
master 3834
PR 32

Related optimization that tagged along.

  • The lock_selection_rect optimization is used by draw_tool and draw_fill_gap as well (this will be noticeable when drawing a line using Pencil on a 5000x5000 canvas in presence of selection).

@Variable-ind Variable-ind mentioned this pull request Jul 15, 2025
@Variable-ind Variable-ind marked this pull request as draft July 15, 2025 16:01
@Variable-ind Variable-ind marked this pull request as ready for review July 15, 2025 17:03
Copy link
Member

@OverloadedOrama OverloadedOrama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thank you!

@OverloadedOrama OverloadedOrama merged commit 2356940 into Orama-Interactive:master Jul 19, 2025
4 checks passed
@Variable-ind Variable-ind deleted the optimize-bucket branch July 19, 2025 05:58
@Variable-ind Variable-ind mentioned this pull request Aug 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants