Skip to content

Commit 820ff09

Browse files
committed
sorting optimization 3
1 parent 470078a commit 820ff09

4 files changed

Lines changed: 26 additions & 19 deletions

File tree

generation_orchestrator.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ def run_generation_loop(
422422
pending_batch = []
423423
current_job = 0
424424
total_generated = 0
425+
gen_index_offset = len(existing_data.get("items", [])) # Sequential index for deterministic sort ordering
425426
skipped_count = 0
426427
job_durations = []
427428
eta_start_time = time.time()
@@ -1020,7 +1021,8 @@ def _preload_diff_worker(conf_to_load=next_conf):
10201021

10211022
meta = create_image_metadata(
10221023
conf, w, h, duration, current_seed, batch_idx,
1023-
actual_positive_prompt, actual_negative_prompt
1024+
actual_positive_prompt, actual_negative_prompt,
1025+
gen_index=gen_index_offset + total_generated
10241026
)
10251027
if pos_hash or neg_hash:
10261028
meta["conditioning_pos_hash"] = pos_hash

image_generation.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ def save_image_to_disk(image, output_dir, filename):
141141
return filepath
142142

143143

144-
def create_image_metadata(config, width, height, duration, seed, batch_idx, actual_positive_prompt, actual_negative_prompt):
144+
def create_image_metadata(config, width, height, duration, seed, batch_idx, actual_positive_prompt, actual_negative_prompt, gen_index=None):
145145
"""
146146
Create metadata dictionary for an image.
147-
147+
148148
Args:
149149
config: Configuration dictionary
150150
width: Image width
@@ -154,7 +154,8 @@ def create_image_metadata(config, width, height, duration, seed, batch_idx, actu
154154
batch_idx: Batch index
155155
actual_positive_prompt: Final positive prompt (with triggers)
156156
actual_negative_prompt: Final negative prompt
157-
157+
gen_index: Sequential generation index for deterministic sort ordering (optional, backwards-compatible)
158+
158159
Returns:
159160
dict: Metadata dictionary
160161
"""
@@ -178,15 +179,18 @@ def create_image_metadata(config, width, height, duration, seed, batch_idx, actu
178179
if meta.get("attention_mode") == "default":
179180
meta.pop("attention_mode", None)
180181

181-
meta.update({
182+
update_dict = {
182183
"width": width,
183184
"height": height,
184185
"duration": duration,
185186
"seed": seed,
186187
"batch_idx": batch_idx,
187188
"positive": actual_positive_prompt,
188189
"negative": actual_negative_prompt
189-
})
190+
}
191+
if gen_index is not None:
192+
update_dict["gen_index"] = gen_index
193+
meta.update(update_dict)
190194

191195
return meta
192196

resources/logic_pipeline.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const ASYNC_PIPELINE_THRESHOLD = 500;
1212
// current sort/filter, used for "Go To Card #" and the #N tag on cards.
1313
function refreshIndices() {
1414
if (!activeData) return;
15-
const sorted = activeData.slice().sort((a, b) => a.id - b.id);
15+
const sorted = activeData.slice().sort((a, b) => (a.gen_index ?? a.id) - (b.gen_index ?? b.id));
1616
idToIndexMap = new Map(sorted.map((item, index) => [item.id, index + 1]));
1717
}
1818

@@ -167,8 +167,8 @@ function setSecondarySort(id) {
167167
// Unified Comparison Helper
168168
function compareItems(a, b, sortKey) {
169169
switch (sortKey) {
170-
case 'newest': return b.id - a.id;
171-
case 'oldest': return a.id - b.id;
170+
case 'newest': return (b.gen_index ?? b.id) - (a.gen_index ?? a.id);
171+
case 'oldest': return (a.gen_index ?? a.id) - (b.gen_index ?? b.id);
172172
case 'fastest': return a.duration - b.duration;
173173
case 'favorited': return (b.favorited ? 1 : 0) - (a.favorited ? 1 : 0);
174174
case 'cfg': return a.cfg - b.cfg;
@@ -199,9 +199,9 @@ function runMultiSort(list) {
199199
res = compareItems(a, b, currentSecondarySort);
200200
}
201201

202-
// 3. Fallback to ID (Stable sort)
202+
// 3. Fallback to gen_index/ID (Stable sort, backwards-compatible)
203203
if (res === 0) {
204-
return a.id - b.id;
204+
return (a.gen_index ?? a.id) - (b.gen_index ?? b.id);
205205
}
206206
return res;
207207
});
@@ -313,10 +313,11 @@ function executePipeline() {
313313
});
314314

315315
// Apply sorting
316-
// Fast path: for oldest/newest with no secondary sort, items from activeData.filter()
317-
// are already in ID order — just reverse for newest
316+
// Fast path: for oldest/newest with no secondary sort, activeData is stored newest-first
317+
// (items are inserted/unshifted at position 0), so processedData after filter() is also newest-first.
318+
// For 'oldest' we reverse; for 'newest' it's already correct.
318319
if ((currentSort === 'oldest' || currentSort === 'newest') && currentSecondarySort === 'none') {
319-
if (currentSort === 'newest') {
320+
if (currentSort === 'oldest') {
320321
processedData.reverse();
321322
}
322323
} else {
@@ -402,7 +403,7 @@ function binaryInsert(arr, item) {
402403
cmp = compareItems(arr[mid], item, currentSecondarySort);
403404
}
404405
if (cmp === 0) {
405-
cmp = arr[mid].id - item.id; // Stable sort fallback
406+
cmp = (arr[mid].gen_index ?? arr[mid].id) - (item.gen_index ?? item.id); // Stable sort fallback
406407
}
407408
if (cmp <= 0) {
408409
low = mid + 1;

resources/logic_virtual.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ function renderVisibleItems(forcePositionUpdate = false) {
242242

243243
itemsToShow.forEach((data, offsetIndex) => {
244244
const globalIndex = visibleRange.start + offsetIndex;
245-
const displayNumber = globalIndex + 1; // 1-based position in current sorted view
245+
const genOrderNumber = idToIndexMap.get(data.id) || (globalIndex + 1); // Generation order number (backwards-compatible)
246246

247247
const row = Math.floor(globalIndex / columnsCount);
248248
const col = globalIndex % columnsCount;
@@ -259,9 +259,9 @@ function renderVisibleItems(forcePositionUpdate = false) {
259259
card.style.width = `${itemWidth - 10}px`;
260260
card.style.zIndex = globalIndex;
261261

262-
// Update card number to reflect sorted position
262+
// Update card number to reflect generation order
263263
const indexTag = card.querySelector('.index-tag');
264-
if (indexTag) indexTag.textContent = `#${displayNumber}`;
264+
if (indexTag) indexTag.textContent = `#${genOrderNumber}`;
265265

266266
nodeMap.set(data.id, card);
267267
fragment.appendChild(card);
@@ -286,7 +286,7 @@ function renderVisibleItems(forcePositionUpdate = false) {
286286

287287
// Update card number when position changes (sort/filter changed)
288288
const indexTag = card.querySelector('.index-tag');
289-
if (indexTag) indexTag.textContent = `#${displayNumber}`;
289+
if (indexTag) indexTag.textContent = `#${genOrderNumber}`;
290290
}
291291
}
292292
});

0 commit comments

Comments
 (0)