AmendHub

Download:

jcs

/

amend

/

amendments

/

249

browser: Speed up repo loads by only loading newest 100 amendments

Usually I just open a repo and want to diff+commit, but loading 500
amendments takes forever. Just load the newest 100 to show some
history, and add a list item that, when clicked, will load the full
history.
 
This is configurable by a new setting and can be 0 to disable.
 
Also, since we are sorting amendment IDs through bile, assume they're
already in order by date and avoid having to sort them all again
after processing.

jcs made amendment 249 about 1 year ago
--- amend.π.r Tue Apr 18 23:27:25 2023 +++ amend.π.r Thu May 4 17:13:32 2023 @@ -53,14 +53,19 @@ data 'DITL' (130, "ASK") { }; data 'DITL' (131, "SETTINGS") { - $"0005 0000 0000 005A 00A0 006E 00DA 0404" /* .......Z.†.n.... */ - $"5361 7665 0000 0000 005A 005A 006E 0094" /* Save.....Z.Z.n.î */ + $"0008 0000 0000 0078 006E 008C 00A8 0404" /* .......x.n.å.®.. */ + $"5361 7665 0000 0000 0078 0028 008C 0062" /* Save.....x.(.å.b */ $"0406 4361 6E63 656C 0000 0000 0014 006E" /* ..Cancel.......n */ $"0024 00D7 1007 756E 6B6E 6F77 6E61 0000" /* .$.◊..unknowna.. */ - $"0000 0032 006E 0042 008D 1001 346F 0000" /* ...2.n.B.ç..4o.. */ - $"0000 0014 0014 0025 0062 8807 4175 7468" /* .......%.bà.Auth */ - $"6F72 3A00 0000 0000 0032 0014 0042 005F" /* or:......2...B._ */ - $"880A 5461 6220 7769 6474 683A" /* à.Tab width: */ + $"0000 0032 006E 0042 0089 1001 346F 0000" /* ...2.n.B.â..4o.. */ + $"0000 0050 006E 0060 0096 1003 3130 3068" /* ...P.n.`.ñ..100h */ + $"0000 0000 0014 000A 0025 003D 8807 4175" /* .........%.=à.Au */ + $"7468 6F72 3A20 0000 0000 0032 000A 0042" /* thor: .....2...B */ + $"0051 880A 5461 6220 7769 6474 683A 0000" /* .Qà.Tab width:.. */ + $"0000 0046 000A 0068 0065 8813 416D 656E" /* ...F...h.eà.Amen */ + $"646D 656E 7473 2074 6F20 6C6F 6164 3A00" /* dments to load:. */ + $"0000 0000 0050 00A0 0060 00D9 8809 2830" /* .....P.†.`..à∆(0 */ + $"203D 2041 6C6C 2900" /* = All). */ }; data 'DITL' (132, "ALERT") { @@ -90,12 +95,16 @@ data 'STR ' (128, "AUTHOR") { $"036A 6373" /* .jcs */ }; +data 'STR ' (130, "MAX_AMENDMENTS") { + $"0331 3030" /* .100 */ +}; + data 'LDEF' (128, "commit list", purgeable) { $"2F3A 0004 4E75 0000 0000" /* /:..Nu.... */ }; data 'DLOG' (130, "SETTINGS_DLOG_ID") { - $"005C 0080 00D4 0172 0001 0100 0000 0000" /* .\.Ä.‘.r........ */ + $"005C 008C 00F0 0170 0001 0100 0000 0000" /* .\.å...p........ */ $"0000 0083 00BA 280A" /* ...É.∫(. */ }; --- amend.h Wed Mar 29 11:11:19 2023 +++ amend.h Thu May 4 15:57:37 2023 @@ -49,9 +49,11 @@ #define SETTINGS_CANCEL_ID 2 #define SETTINGS_AUTHOR_ID 3 #define SETTINGS_TABWIDTH_ID 4 +#define SETTINGS_MAX_AMENDMENTS_ID 5 #define STR_AUTHOR_ID 128 #define STR_TABWIDTH_ID 129 +#define STR_MAX_AMENDMENTS_ID 130 extern MenuHandle file_menu, edit_menu, repo_menu, amendment_menu; --- browser.c Tue Apr 18 22:59:28 2023 +++ browser.c Thu May 4 16:24:35 2023 @@ -156,6 +156,7 @@ browser_init(struct repo *repo) bounds.right = 100; TextFont(FILE_LIST_FONT); + TextFace(0); TextSize(FILE_LIST_FONT_SIZE); browser->file_list = LNew(&bounds, &data_bounds, cell_size, 0, browser->win, true, true, false, true); @@ -169,6 +170,7 @@ browser_init(struct repo *repo) bounds.bottom = bounds.top + 20; bounds.right += SCROLLBAR_WIDTH; TextFont(DIFF_BUTTON_FONT); + TextFace(0); TextSize(DIFF_BUTTON_FONT_SIZE); browser->diff_button = NewControl(browser->win, &bounds, "\pGenerate Diff", true, 1, 1, 1, pushButProc | useWFont, 0L); @@ -281,6 +283,7 @@ browser_add_files(struct browser *browser) browser_show_amendment(browser, NULL); TextFont(FILE_LIST_FONT); + TextFace(0); TextSize(FILE_LIST_FONT_SIZE); LAddRow(1, cell.v, browser->file_list); LSetCell("[All Files]", 11, cell, browser->file_list); @@ -375,6 +378,8 @@ browser_filter_amendments(struct browser *browser) for (i = 0; i < browser->repo->namendments; i++) { add = 0; amendment = browser->repo->amendments[i]; + if (amendment == NULL) + continue; for (j = 0; j < amendment->nfiles; j++) { for (k = 0; k < nselected_files; k++) { if (selected_files[k] == amendment->file_ids[j]) { @@ -388,13 +393,18 @@ browser_filter_amendments(struct browser *browser) if (!add) continue; - + LAddRow(1, cell.v, browser->amendment_list); LSetCell(&(browser->repo->amendments[i]), sizeof(Ptr), cell, browser->amendment_list); cell.v++; } + if (browser->repo->unloaded_amendments) { + LAddRow(1, cell.v, browser->amendment_list); + LSetCell(NULL, 0, cell, browser->amendment_list); + } + LDoDraw(true, browser->amendment_list); InvalRect(&(*(browser->amendment_list))->rView); @@ -515,6 +525,7 @@ browser_update_menu(struct browser *browser) Cell cell = { 0, 0 }; TextFont(systemFont); + TextFace(0); TextSize(12); HLock(browser->diff_te); @@ -585,6 +596,7 @@ browser_update(struct focusable *focusable, EventRecor FillRect(&r, white); FrameRect(&r); TextFont(FILE_LIST_FONT); + TextFace(0); TextSize(FILE_LIST_FONT_SIZE); LUpdate(browser->win->visRgn, browser->file_list); @@ -608,6 +620,7 @@ browser_update(struct focusable *focusable, EventRecor LUpdate(browser->win->visRgn, browser->amendment_list); TextFont(DIFF_BUTTON_FONT); + TextFace(0); TextSize(DIFF_BUTTON_FONT_SIZE); UpdtControl(browser->win, browser->win->visRgn); @@ -650,6 +663,7 @@ browser_mouse_down(struct focusable *focusable, EventR /* in case any new items are redrawn in LClick */ TextFont(FILE_LIST_FONT); + TextFace(0); TextSize(FILE_LIST_FONT_SIZE); /* possibly highlight a new cell */ @@ -707,7 +721,12 @@ browser_mouse_down(struct focusable *focusable, EventR /* in a filtered list, amendments[now.v] won't be accurate */ data_len = sizeof(Ptr); LGetCell(&amendment, &data_len, now, browser->amendment_list); - browser_show_amendment(browser, amendment); + if (amendment == NULL && browser->repo->unloaded_amendments) { + repo_load_amendments(browser->repo, true); + browser_filter_amendments(browser); + progress(NULL); + } else + browser_show_amendment(browser, amendment); } return; @@ -716,6 +735,7 @@ browser_mouse_down(struct focusable *focusable, EventR switch (FindControl(p, browser->win, &control)) { case inButton: TextFont(DIFF_BUTTON_FONT); + TextFace(0); TextSize(DIFF_BUTTON_FONT_SIZE); if (TrackControl(control, p, 0L) && control == browser->diff_button) --- commit_list.c Mon Apr 17 15:15:52 2023 +++ commit_list.c Thu May 4 17:11:58 2023 @@ -53,16 +53,15 @@ void amendment_list_draw_cell(ListHandle theList, Cell theCell, short dataLen, Rect *cellRect, Boolean selected) { + static char initial_load_text[] = + "[ Click to load earlier amendments ]"; Rect textRect; char tmp[50]; struct repo_amendment *amendment = NULL; struct tm *ttm = NULL; - short len, height; + short len, height, width; LGetCell(&amendment, &dataLen, theCell, theList); - if (amendment == NULL) - /* XXX: why does this happen? */ - return; textRect.left = cellRect->left; textRect.right = cellRect->right; @@ -76,6 +75,16 @@ amendment_list_draw_cell(ListHandle theList, Cell theC TextFont(LIST_FONT); TextSize(LIST_FONT_SIZE); MoveTo(textRect.left, textRect.top + 6); + + if (amendment == NULL) { + TextFace(bold); + width = TextWidth(&initial_load_text, 0, + strlen(initial_load_text)); + MoveTo(textRect.left + ((textRect.right - textRect.left) / 2) - + (width / 2), textRect.top + 12); + DrawText(&initial_load_text, 0, strlen(initial_load_text)); + return; + } HLock(amendment->log); for (len = 1; len < amendment->log_len; len++) { --- repo.c Tue Apr 18 22:59:21 2023 +++ repo.c Thu May 4 17:08:07 2023 @@ -22,12 +22,12 @@ #include "bile.h" #include "diff.h" #include "repo.h" +#include "settings.h" #include "strnatcmp.h" #include "util.h" - + struct repo * repo_init(struct bile *bile, bool is_new); void repo_sort_files(struct repo *repo); -void repo_sort_amendments(struct repo *repo); short repo_get_file_attrs(struct repo *repo, Str255 filename, struct repo_file_attrs *attrs); short repo_file_update(struct repo *repo, struct repo_file *file); @@ -135,35 +135,69 @@ repo_init(struct bile *bile, bool is_new) xfree(&data); } xfree(&ids); + + progress("Organizing files..."); repo_sort_files(repo); } /* fill in amendment info */ - repo->namendments = bile_sorted_ids_by_type(bile, REPO_AMENDMENT_RTYPE, - &ids); - if (repo->namendments) { + repo_load_amendments(repo, false); + + return repo; +} + +void +repo_load_amendments(struct repo *repo, bool fill_in) +{ + size_t size; + char *data; + unsigned long *ids; + unsigned short i; + + if (fill_in) { + progress("Loading first %d/%d amendments...", + repo->namendments - settings.max_amendments, + repo->namendments); + repo->unloaded_amendments = false; + } + + repo->namendments = bile_sorted_ids_by_type(repo->bile, + REPO_AMENDMENT_RTYPE, &ids); + if (!repo->namendments) + return; + + if (!fill_in) { repo->amendments = xcalloc(repo->namendments, sizeof(Ptr), "repo amendments"); - for (i = 0; i < repo->namendments; i++) { - if (i == 0 || i == repo->namendments - 1 || - ((i + 1) % 10) == 0) - progress("Loading amendment %d/%d...", i + 1, - repo->namendments); - size = bile_read_alloc(bile, REPO_AMENDMENT_RTYPE, ids[i], - &data); - if (size == 0) - panic("failed fetching amendment %ld", ids[i]); - repo->amendments[i] = repo_parse_amendment(ids[i], - (unsigned char *)data, size); - if (repo->amendments[i]->id >= repo->next_amendment_id) - repo->next_amendment_id = repo->amendments[i]->id + 1; - xfree(&data); - } - xfree(&ids); - repo_sort_amendments(repo); + + repo->unloaded_amendments = (settings.max_amendments > 0 && + repo->namendments > settings.max_amendments); } - - return repo; + + for (i = 0; i < repo->namendments; i++) { + if (repo->unloaded_amendments && + i < repo->namendments - settings.max_amendments - 1) + continue; + if (fill_in && repo->amendments[i] != NULL) + continue; + + if (i == 0 || i == repo->namendments - 1 || + ((i + 1) % 10) == 0) + progress("Loading amendment %d/%d...", i + 1, + repo->namendments); + + size = bile_read_alloc(repo->bile, REPO_AMENDMENT_RTYPE, ids[i], + &data); + if (size == 0) + panic("failed fetching amendment %ld", ids[i]); + + repo->amendments[repo->namendments - 1 - i] = + repo_parse_amendment(ids[i], (unsigned char *)data, size); + if (repo->amendments[i]->id >= repo->next_amendment_id) + repo->next_amendment_id = repo->amendments[i]->id + 1; + xfree(&data); + } + xfree(&ids); } void @@ -699,24 +733,6 @@ repo_sort_files(struct repo *repo) } } -void -repo_sort_amendments(struct repo *repo) -{ - struct repo_amendment *amendment; - short i, j; - - /* reverse order, newest amendment first */ - for (i = 0; i < repo->namendments; i++) { - for (j = 0; j < repo->namendments - i - 1; j++) { - if (repo->amendments[j]->id < repo->amendments[j + 1]->id) { - amendment = repo->amendments[j]; - repo->amendments[j] = repo->amendments[j + 1]; - repo->amendments[j + 1] = amendment; - } - } - } -} - short repo_diff_file(struct repo *repo, struct repo_file *file) { @@ -1047,8 +1063,6 @@ repo_amend(struct repo *repo, struct diffed_file *diff repo->amendments = xreallocarray(repo->amendments, repo->namendments, sizeof(Ptr)); repo->amendments[repo->namendments - 1] = amendment; - - repo_sort_amendments(repo); } void --- repo.h Sat Mar 4 22:38:31 2023 +++ repo.h Thu May 4 15:58:49 2023 @@ -81,11 +81,13 @@ struct repo { short namendments; struct repo_amendment **amendments; short next_amendment_id; + bool unloaded_amendments; }; struct repo *repo_open(AppFile *file); struct repo *repo_create(void); void repo_close(struct repo *repo); +void repo_load_amendments(struct repo *repo, bool fill_in); struct repo_amendment *repo_parse_amendment(unsigned long id, unsigned char *data, size_t size); struct repo_file * repo_parse_file(unsigned long id, unsigned char *data, --- settings.c Sat Mar 4 22:38:35 2023 +++ settings.c Thu May 4 15:58:31 2023 @@ -36,11 +36,13 @@ settings_load(void) snprintf(settings.author, sizeof(settings.author), "unknown"); settings.tabwidth = (short)xGetStringAsLong(STR_TABWIDTH_ID); - if (settings.tabwidth < 1 || settings.tabwidth > 20) { - warn("Bogus tabwidth resource %d", settings.tabwidth); + if (settings.tabwidth < 1 || settings.tabwidth > 20) settings.tabwidth = 4; - } + settings.max_amendments = (short)xGetStringAsLong(STR_MAX_AMENDMENTS_ID); + if (settings.max_amendments < 0) + settings.max_amendments = 0; + TETabWidth = settings.tabwidth; } @@ -69,6 +71,16 @@ settings_save(void) CtoPstr(*res); ChangedResource(res); ReleaseResource(res); + + res = xGetResource('STR ', STR_MAX_AMENDMENTS_ID); + HLock(res); + snprintf(tmp, sizeof(tmp), "%d", settings.max_amendments); + l = strlen(tmp); + xSetHandleSize(res, l + 1); + memcpy(*res, tmp, l + 1); + CtoPstr(*res); + ChangedResource(res); + ReleaseResource(res); UpdateResFile(CurResFile()); if (ResError()) @@ -100,6 +112,11 @@ settings_edit(void) snprintf((char *)txt, sizeof(txt), "%d", settings.tabwidth); CtoPstr(txt); SetIText(ihandle, txt); + + GetDItem(dlg, SETTINGS_MAX_AMENDMENTS_ID, &itype, &ihandle, &irect); + snprintf((char *)txt, sizeof(txt), "%d", settings.max_amendments); + CtoPstr(txt); + SetIText(ihandle, txt); ShowWindow(dlg); @@ -113,10 +130,6 @@ dialog_wait: break; case SETTINGS_CANCEL_ID: goto dialog_close; - case SETTINGS_AUTHOR_ID: - break; - case SETTINGS_TABWIDTH_ID: - break; } } @@ -135,6 +148,16 @@ dialog_wait: GetIText(ihandle, txt); PtoCstr(txt); settings.tabwidth = atoi((char *)&txt); + if (settings.tabwidth < 1) + settings.tabwidth = 1; + HUnlock(ihandle); + + GetDItem(dlg, SETTINGS_MAX_AMENDMENTS_ID, &itype, &ihandle, &irect); + GetIText(ihandle, txt); + PtoCstr(txt); + settings.max_amendments = atoi((char *)&txt); + if (settings.max_amendments < 0) + settings.max_amendments = 0; HUnlock(ihandle); settings_save(); --- settings.h Sat Mar 4 22:38:39 2023 +++ settings.h Thu May 4 15:54:33 2023 @@ -20,6 +20,7 @@ struct settings { char author[32]; short tabwidth; + short max_amendments; }; extern struct settings settings;