Closed Bug 418643 Opened 18 years ago Closed 18 years ago

Bookmark folder deletion executes too many SQL statements

Categories

(Firefox :: Bookmarks & History, defect, P2)

defect

Tracking

()

VERIFIED FIXED
Firefox 3.1b2

People

(Reporter: ondrej, Assigned: mak)

References

Details

(Keywords: perf)

Attachments

(4 files, 4 obsolete files)

Deleting a bookmark folder with 10 bookmarks generates more than 470 database operations, 20% of them are write operations (see attachment - simple queries are not visible). The reason is very inefficient implementation of nsNavBookmarks::RemoveFolderChildren. This function deletes one by one all the child folders and items. The problem starts with deletion of annotations, all annotations are removed and even if there were no (we should add support for number of affected rows - sqlite3_changes), item's last modification time gets updated and observers notified. nsNavHistoryResult observer then does two more selects, and goes deeper... Another problem is updating of bookmark positions - an update is done after each item removal. However, as we are deleting all children, this is cleanly wasting of time, there will be no items when we finish. The last problem is that each item updates last modification time of its parent. What is definitely superfluous, it should be enough to call this once, when we are finished with deletion. Suggestion: 1) We should avoid calling annotation observers when we are deleting bookmarks. 2) We should reduce number of last modification time changes on parent folders. 3) We should be deleting all items belonging to one folder with one SQL statement using their parent column. Deleting of bookmarks will be much faster. At the moment there is visible delay even when deleting folder with 10 items (in debug mode 3 seconds). Similar optimization is necessary for importing bookmarks (perhaps bug 392193?)
Flags: blocking-firefox3?
OS: Windows XP → All
Priority: -- → P2
Hardware: PC → All
Target Milestone: --- → Firefox 3 beta4
More great analysis from Ondrej; definitely wanted, would be nice to see if we can do a performance sweep through Places after Beta 4.
Flags: wanted-firefox3+
Flags: blocking-firefox3?
Flags: blocking-firefox3-
Target Milestone: Firefox 3 beta4 → Firefox 3
Flags: wanted-firefox3.1?
Blocks: 445016
It seems to me bookmark deletion doesn't use much HD now. Can somebody confirm my impression?
Attached patch wip (obsolete) — — Splinter Review
some work on this. beware, bugs inside! this could be a first improvement for livemark updates since before updating a livemark we delete its contents using removeFolderChildren... From my tests on a debug build this is quite faster when removing complex folders (about 5 times faster), while it's about twice faster when removing children from a simple folder. In the livemarks case i expect this to gain about 10-20ms for every livemark cleanup (but could be more for large ones)
Blocks: 432706
Blocks: 329534
Blocks: 428459
Blocks: 441786
Assignee: nobody → mak77
Status: NEW → ASSIGNED
Target Milestone: Firefox 3 → Firefox 3.1
this would also speed up bookmark restores. is this ready for review?
not ready, should be ready for tomorrow, i need to add back a couple of checks for dynamic containers and tags
Attached patch wip 0.2 (obsolete) — — Splinter Review
more complete, but i still see some strange test failure
Attachment #331101 - Attachment is obsolete: true
Attached patch patch (obsolete) — — Splinter Review
this should be complete, but i'd prefer writing a test for this specific function, to check that the db remain correct and observers are called... So Dietrich if you have slots you can start to review this and i'll come later with a unit test, to be safe.
Attachment #332727 - Flags: review?(dietrich)
Attachment #332652 - Attachment is obsolete: true
Attached patch patch with unit test (obsolete) — — Splinter Review
Attachment #332727 - Attachment is obsolete: true
Attachment #332864 - Flags: review?(dietrich)
Attachment #332727 - Flags: review?(dietrich)
Comment on attachment 332864 [details] [diff] [review] patch with unit test >+nsresult >+nsNavBookmarks::GetFolderChildren(PRInt64 aFolderId, >+ PRInt64 aGrandParentId, >+ nsTArray<folderChildrenInfo>& aFolderChildrenArray) { >+ // New children will be added from this index on. >+ PRUint32 startIndex = aFolderChildrenArray.Length(); > nsresult rv; > { >+ // Collect children informations. > mozStorageStatementScoper scope(mDBGetChildren); >- rv = mDBGetChildren->BindInt64Parameter(0, aFolder); >+ rv = mDBGetChildren->BindInt64Parameter(0, aFolderId); > NS_ENSURE_SUCCESS(rv, rv); > > PRBool hasMore; > while (NS_SUCCEEDED(mDBGetChildren->ExecuteStep(&hasMore)) && hasMore) { >- PRInt32 type = mDBGetChildren->AsInt32(kGetChildrenIndex_Type); >- if (type == TYPE_FOLDER) { >- // folder >- folderChildren.AppendElement( >- mDBGetChildren->AsInt64(nsNavHistory::kGetInfoIndex_ItemId)); >- } else { >- // bookmarks / separators >- itemChildren.AppendElement(mDBGetChildren->AsInt64(nsNavHistory::kGetInfoIndex_ItemId)); >+ folderChildrenInfo child; >+ child.itemId = mDBGetChildren->AsInt64(nsNavHistory::kGetInfoIndex_ItemId); >+ child.folderId = aFolderId; >+ child.grandParentId = aGrandParentId; nit: for consistency of metaphor in this context, maybe do s/folderId/parentId/ for folderChildrenInfo. >+ // Recursively call getFolderChildren for added folders. >+ // We start at startIndex since previous folders are checked >+ // by previous calls to this method. >+ for (PRUint32 i = startIndex; i < aFolderChildrenArray.Length(); i++) { do you want to cache the length used here, since it can change inside the loop? >+NS_IMETHODIMP >+nsNavBookmarks::RemoveFolderChildren(PRInt64 aFolderId) >+{ >+ nsresult rv; >+ PRUint16 itemType; >+ PRInt64 grandParent; should do s/grandParent/grandParentId/ for clarity, as you've done elsewhere. >+ >+ for (PRUint32 i = 0; i < folderChildrenArray.Length(); i++) { >+ folderChildrenInfo child = folderChildrenArray[i]; >+ if (child.itemType == TYPE_BOOKMARK) { >+ PRInt64 placeId = child.placeId; >+ UpdateBookmarkHashOnRemove(placeId); >+ >+ // XXX is this too expensive when updating livemarks? >+ // UpdateBookmarkHashOnRemove() does a sanity check using >+ // IsBookmarkedInDatabase(), so it might not have actually >+ // removed the bookmark. should we have a boolean out param >+ // for if we actually removed it, and use that to decide if we call >+ // UpdateFrecency() and the rest of this code? >+ rv = History()->UpdateFrecency(placeId, PR_FALSE /* isBookmark */); >+ NS_ENSURE_SUCCESS(rv, rv); hrm. isBookmark==false will not be true if the same URI is bookmarked outside of the deleted folder tree. this is not a change particular to this patch, so please file a followup bug for this. >+ // Call observers. >+ for (PRUint32 i = 0; i < folderChildrenArray.Length(); i++) { >+ folderChildrenInfo child = folderChildrenArray[i]; >+ >+ ENUMERATE_WEAKARRAY(mObservers, nsINavBookmarkObserver, >+ OnItemRemoved(child.itemId, >+ child.folderId, >+ child.index)); >+ currently, item notifications occur in a hierarchic fashion: notifications for a deleted bookmark are sent before it's parent folder's notification is sent. maybe walk the list in reverse? >diff --git a/toolkit/components/places/src/nsNavBookmarks.h b/toolkit/components/places/src/nsNavBookmarks.h >--- a/toolkit/components/places/src/nsNavBookmarks.h >+++ b/toolkit/components/places/src/nsNavBookmarks.h >@@ -161,28 +161,46 @@ private: > > nsresult GetParentAndIndexOfFolder(PRInt64 aFolder, PRInt64* aParent, > PRInt32* aIndex); > > nsresult IsBookmarkedInDatabase(PRInt64 aBookmarkID, PRBool* aIsBookmarked); > > nsresult SetItemDateInternal(mozIStorageStatement* aStatement, PRInt64 aItemId, PRTime aValue); > >+ // Structure to hold folder's children informations >+ typedef struct folderChildrenInfo >+ { >+ PRInt64 itemId; >+ PRInt64 itemType; PRInt16? >+ PRInt64 placeId; >+ PRInt64 folderId; >+ PRInt64 grandParentId; >+ PRInt32 index; >+ nsCString url; >+ nsCString folderType; >+ }; >+ >+ // Recursive method to build an array of folder's children >+ nsresult GetFolderChildren(PRInt64 aFolderId, >+ PRInt64 aGrandParentId, >+ nsTArray<folderChildrenInfo>& aFolderChildrenArray); you should make this very clear that it gets *all descendant* children, not just immediate children. i think you should actually rename the method to indicate this. GetDescendantChildren?
Attachment #332864 - Flags: review?(dietrich) → review-
Blocks: 437244
No longer blocks: 329534
No longer blocks: 432706
Blocks: 429379
Whiteboard: [needs new patch]
In bug 445016 the person speaks of 5 seconds for deleting 60 bookmarks. I have about 1500 bookmarks and it takes Firefox 3.x more than 10 minutes to delete them (whilst in freeze). The same happens the other way around, when importing the links from Internet Explorer. Takes a long long time. Was not so in Firefox 2.x were every thing went smooth and fast.
> >+ // XXX is this too expensive when updating livemarks? > >+ // UpdateBookmarkHashOnRemove() does a sanity check using > >+ // IsBookmarkedInDatabase(), so it might not have actually > >+ // removed the bookmark. should we have a boolean out param > >+ // for if we actually removed it, and use that to decide if we call > >+ // UpdateFrecency() and the rest of this code? > >+ rv = History()->UpdateFrecency(placeId, PR_FALSE /* isBookmark */); > >+ NS_ENSURE_SUCCESS(rv, rv); > > hrm. isBookmark==false will not be true if the same URI is bookmarked outside > of the deleted folder tree. this is not a change particular to this patch, so > please file a followup bug for this. filled bug 455315
Attached patch patch — — Splinter Review
addressed all review comments (i've made itemType be PRUint16 in all the code since it's defined as unsigned short in the idl and returned like that by getItemType) i added a test to ensure we are calling children observers before their parents' ones, so that will not get lost in future.
Attachment #338659 - Flags: review?(dietrich)
this wil bitrot fsync stuff, so waiting for decision on that before a checkin
Whiteboard: [needs new patch] → [needs review dietrich][waiting for fsync]
Attachment #338659 - Flags: review?(dietrich) → review+
Whiteboard: [needs review dietrich][waiting for fsync] → [waiting for fsync]
imho this is something we need to block on for 3.1, only waiting an unbitrot on fsync stuff
Flags: blocking-firefox3.1?
Attachment #332864 - Attachment is obsolete: true
Whiteboard: [waiting for fsync]
Target Milestone: Firefox 3.1 → Firefox 3.1b2
Status: ASSIGNED → RESOLVED
Closed: 18 years ago
Resolution: --- → FIXED
Flags: wanted-firefox3.1?
Flags: blocking-firefox3.1?
Flags: in-testsuite+
Status: RESOLVED → VERIFIED
Comment on attachment 346184 [details] [diff] [review] unbitrot upon fsync >diff --git a/toolkit/components/places/src/nsNavBookmarks.h b/toolkit/components/places/src/nsNavBookmarks.h >--- a/toolkit/components/places/src/nsNavBookmarks.h >+++ b/toolkit/components/places/src/nsNavBookmarks.h >+ typedef struct folderChildrenInfo >+ { >+ PRInt64 itemId; [...snip....] >+ nsCString folderType; >+ }; I think the word "typedef" is unnecessary there, and in fact it triggers this warning: /mozilla/toolkit/components/places/src/nsNavBookmarks.h:184: warning: ‘typedef’ was ignored in this declaration I get exactly 8 copies of this warning when building mozilla-central, and they all refer to this particular line.
This trivial follow-up patch removes the aforementioned unnecessary 'typedef', fixing those compiler warnings.
Attachment #351413 - Flags: review?(dietrich)
Comment on attachment 351413 [details] [diff] [review] trivial follow-up to fix compiler warnings: remove unneeded 'typedef' token You only need to do the typdef stuff in C anyway. r=sdwilsh
Attachment #351413 - Flags: review?(dietrich) → review+
Thanks sdwilsh. 'typedef' removal pushed: http://hg.mozilla.org/mozilla-central/rev/23c07da38629
Bug 451915 - move Firefox/Places bugs to Firefox/Bookmarks and History. Remove all bugspam from this move by filtering for the string "places-to-b-and-h". In Thunderbird 3.0b, you do that as follows: Tools | Message Filters Make sure the correct account is selected. Click "New" Conditions: Body contains places-to-b-and-h Change the action to "Delete Message". Select "Manually Run" from the dropdown at the top. Click OK. Select the filter in the list, make sure "Inbox" is selected at the bottom, and click "Run Now". This should delete all the bugspam. You can then delete the filter. Gerv
Component: Places → Bookmarks & History
QA Contact: places → bookmarks
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: