libzim icon indicating copy to clipboard operation
libzim copied to clipboard

Decrease redirects memory usage in the writer

Open benoit74 opened this issue 8 months ago • 13 comments

From https://github.com/openzim/libzim/issues/1045#issuecomment-4024533363, @veloman-yunkan said:

The current implementation maintains a separate collection of not-yet-resolved redirections which takes additional memory and it looks like we can get rid of it.

Also, there is a way to decrease the unjustified memory usage in flows where the count of redirections significantly exceeds the number of real entries (meaning that multiple redirections have to point to the same entry). It can be achieved by sharing the strings of the target path in redirections pointing to the same item.

To be investigated, anything decreasing memory consumption is by design worth to investigate IMHO.

benoit74 avatar Mar 18 '26 08:03 benoit74

Implementing this memory usage optimisation around redirects writing, seems to me almost consubstantial, to implement #1001 because the usage of redirects with fragments will lead, for many scrapers, to far more redirects than before.

kelson42 avatar Mar 24 '26 05:03 kelson42

@veloman-yunkan Thank you for the first PR improving significantly the situation. Is that reasonable to expect further/last fixes before releasing 9.6.0?

kelson42 avatar Apr 03 '26 03:04 kelson42

@kelson42 It doesn't make sense to continue the work on optimization until #1056 is resolved (optimizing buggy code may prove to be a waste of time). Hence, in the light of your comment, the answer to your question is negative.

veloman-yunkan avatar Apr 03 '26 07:04 veloman-yunkan

Adding measured data from openzim/sotoki#243 that may be useful here.

On dba.stackexchange.com (103K questions, ~300K total dirents), libzim 9.5.1, 3 workers:

FTS RSS growth (questions stage)
enabled +361 MB
disabled +470 MB

FTS-off is worse, confirming the dominant cost is data->dirents accumulation, not Xapian. Python rendering contributes only +9 MB (isolated via FakeCreator). At Stack Overflow scale (~150M dirents) this projects to ~84 GB, consistent with 96 GiB observed in production in 2021.

its-me-maady avatar Apr 04 '26 09:04 its-me-maady

@its-me-maady Thanks for the information!

The fact that memory usage is lower with FTS enabled may be an artifact of thread concurrency due to additional tasks being enqueued for our worker threads and affecting how peaks from different threads align with each other. I don't see another explanation how such extra processing could result in lower memory consumption.

The peculiarity of StackExchange ZIMs seems to be that for a significant portion of entries the titles and paths are longer than for other ZIMs. In a question entry the (brief) question serves as a title as well as is embedded in the path. For example:

path: questions/10029/what-are-the-conditions-under-which-mysql-replication-might-break title: what are the conditions under which mysql replication might break?

Also for each question there is at least one redirection entry with the title of the question dropped from the path. For the above example, questions/10029 redirects to questions/10029/what-are-the-conditions-under-which-mysql-replication-might-break. If the question has answers, then each answer entry redirects to the question entry (the one with the title encoded in the path) too (though this is affected by #1001 - the redirection doesn't lead to the target answer under the question). At the time of ZIM creation the redirection target is kept (in the current implementation) as a string (the path of the target entry) until late enough in the flow when redirections are resolved and indexes are assigned to all entries. Such unjustified waste of memory is amplified by the described peculiarity of the StackExchange archives.

It has already been proposed how memory usage by redirections can be improved (by sharing the string of their target path). And there seems to be an easier way of achieving that by "resolving" the target dirent during the creation of a redirection entry (rather than in a separate step during the finishZimCreation() stage):

  • If the target entry already exists, everything is simple
  • Otherwise (if the target entry doesn't yet exist) a placeholder entry (with empty title, MIME-type, etc) is created and made a target for the new redirection entry. The title, MIME-type, and payload are set for that entry later during the ZIM creation process. If such an entry is not explicitly created by the user via addItem() or addRedirection(), then it must be removed, and a clean-up of dangling redirects must ensue (see #1056).

Having written all this but without yet having performed detailed measurements myself, my impression is that memory consumption is still higher than it should be given the mentioned inefficiencies. I will make some experiments to find out what the current lower limit for memory consumption should be. Comparing it with what is actually manifested by the code may help to find (and eliminate) other causes of resource waste.

veloman-yunkan avatar Apr 06 '26 13:04 veloman-yunkan

FYI, I just released 9.6.0 with the first set of memory optimisation.

kelson42 avatar Apr 06 '26 13:04 kelson42

Using the scripts from https://github.com/veloman-yunkan/kiwix-dev-utils/commit/d8ecc1e010898f1060c68efb2d664f1663a9c99d and the following nopic flow (extracted and cleaned up from the terminal session where the work has been performed) for dba.stackexchange.com:

$ wget https://archive.org/download/stackexchange/dba.stackexchange.com.7z
...

$ ./run_sotoki dba.stackexchange.com.7z "DBA StackExchange"
...

$ ls -sh dba.stackexchange.com_en_all_nopic_2026-04.zim 
357M dba.stackexchange.com_en_all_nopic_2026-04.zim

$ zimdump info dba.stackexchange.com_en_all_nopic_2026-04.zim 
count-entries: 417993
...

$ zimdump list --details dba.stackexchange.com_en_all_nopic_2026-04.zim > dba.stackexchange.com.full_listing

$ cat dba.stackexchange.com.full_listing | \time ./listing2zim.py
1.03user 0.11system 0:01.14elapsed 99%CPU (0avgtext+0avgdata 125696maxresident)k
0inputs+0outputs (0major+28494minor)pagefaults 0swaps

$ cat dba.stackexchange.com.full_listing | \time ./listing2zim.py --print-entries | wc
1.62user 0.14system 0:01.76elapsed 99%CPU (0avgtext+0avgdata 171776maxresident)k
0inputs+0outputs (0major+39995minor)pagefaults 0swaps
 417992 2278110 41017125

$ grep '^\(path\|* title\): ' dba.stackexchange.com.full_listing|sed 's/^path: //; s/\* title:          //'|wc -c
21925497

$ cat dba.stackexchange.com.full_listing | \time ./listing2zim.py --create-zim dba.stackexchange.com_dirents_only.zim 
Resolve redirect
set index
49.63user 2.69system 0:52.56elapsed 99%CPU (0avgtext+0avgdata 305984maxresident)k
0inputs+721824outputs (0major+73470minor)pagefaults 0swaps

$ ls -sh dba.stackexchange.com_dirents_only.zim 
78M dba.stackexchange.com_dirents_only.zim

$ zimdump info dba.stackexchange.com_dirents_only.zim 
count-entries: 417992
...

produces these figures:

  • There are ~400K entries in the source data
  • Memory required to represent the arguments (paths, titles, and target paths) to all addItem() and addRedirection() calls: ~40MiB
  • Memory required to represent only the paths and titles of all entries: ~22MiB
  • The size of the ZIM file consisting of little more than dirents, title ordered listing and title search (suggestion) database: 78MiB
  • Memory overhead of libzim during the creation of that ZIM: 131MiB

This confirms my feeling expressed in the previous comment that libzim's memory consumption is way higher than it could be.

veloman-yunkan avatar Apr 07 '26 07:04 veloman-yunkan

@veloman-yunkan I just realised that the PR description of #1055 was incomplete and that the PR is not only about memory optimisation but also introduce new scenarios for the writer to simply crash, which is not acceptable IMHO. Therefore the PR should be fixed or rollbacked.

kelson42 avatar Apr 07 '26 15:04 kelson42

@veloman-yunkan I just realised that the PR description of #1055 was incomplete and that the PR is not only about memory optimisation but also introduce new scenarios for the writer to simply crash, which is not acceptable IMHO.

@kelson42 I provided additional info in https://github.com/openzim/libzim/pull/1055#issuecomment-4178705789 which ends in a question. I was surprised when I saw that PR merged, and humbly updated the description after that without complaining that the merge was premature (which I wanted to do).

Therefore the PR should be fixed or rollbacked.

What do you mean by fixing a PR?

veloman-yunkan avatar Apr 07 '26 15:04 veloman-yunkan

It has already been proposed how memory usage by redirections can be improved (by sharing the string of their target path). And there seems to be an easier way of achieving that by "resolving" the target dirent during the creation of a redirection entry (rather than in a separate step during the finishZimCreation() stage):

* If the target entry already exists, everything is simlple

* Otherwise (if the target entry doesn't yet exist) a placeholder entry (with empty title, MIME-type, etc) is created and made a target for the new redirection entry. The title, MIME-type, and payload are set for that entry later during the ZIM creation process. If such an entry is not explicitly created by the user via `addItem()` or `addRedirection()`, then it must be removed, and a clean-up of dangling redirects must ensue (see [Buggy handling of blind chains of redirections during ZIM creation #1056](https://github.com/openzim/libzim/issues/1056)).

Yes, for the first part, but I would just write a WARNING in the log if the user introduces a redirect to an entry which does not exist explaining the danger of doing so (high memory consumption, danger of dangling redirects, etc...) To me this is good enough.

For example, at the end of the ZIM creation, where all the redirects are resolved, just write a warning if we remark that many of them are not resolved.

Very important is to explain all of this in the documentation.

If the user does not want or can reorganise his scraper code, then he will have to pay the "memory tax".

kelson42 avatar Apr 07 '26 15:04 kelson42

@veloman-yunkan I just realised that the PR description of #1055 was incomplete and that the PR is not only about memory optimisation but also introduce new scenarios for the writer to simply crash, which is not acceptable IMHO.

@kelson42 I provided additional info in #1055 (comment) which ends in a question. I was surprised when I saw that PR merged, and humbly updated the description after that without complaining that the merge was premature (which I wanted to do).

To me your proposal/question was for the future improvements (your words "Further optimization"), not for the current PR.

Therefore the PR should be fixed or rollbacked.

What do you mean by fixing a PR?

It should not crash, we should open a dedicated issue about that. This is a serious matter which is treated just BTW, but it should not.

kelson42 avatar Apr 07 '26 15:04 kelson42

Therefore the PR should be fixed or rollbacked.

What do you mean by fixing a PR?

It should not crash, we should open a dedicated issue about that. This is a serious matter which is treated just BTW, but it should not.

I have created https://github.com/openzim/libzim/issues/1063 to improve this

kelson42 avatar Apr 07 '26 18:04 kelson42

It should not crash, we should open a dedicated issue about that. This is a serious matter which is treated just BTW, but it should not.

It doesn't crash. It fails with an exception.

veloman-yunkan avatar Apr 08 '26 08:04 veloman-yunkan