Use a hybrid allocation scheme in xmlNodeSetContent

On Fri, May 11, 2012 at 9:10 AM, Daniel Veillard <veillard@redhat.com> wrote:
>  Hi Conrad,
>
> that's interesting ! I was initially afraid of a sudden explosion of
> memory allocations for building a tree since by default buffers tend to
> "waste" memory by using doubling allocations, but that's not the case.
>  xmllint --noout doc/libxml2-api.xml
> when compiled with memory debug produce
>
> paphio:~/XML -> cat .memdump
>      MEMORY ALLOCATED : 0, MAX was 12756699
>
> and without your patch 12755657, i.e. the increase is minimal.

Heh, I thought that too. Actually you're looking at the result with XML_ALLOC_EXACT! This
is because EXACT adds 10bytes "spare" on each alloc, and that interestingly wastes about the
same amount of space as XML_ALLOC_DOUBLEIT on this example (see below).

So it turns out that the default realloc() on my system actually handles this case really
well — and I guess that all the time in xmlRealloc() was actually in xmlStrlen, not the
underlying realloc() after all (sorry for misleading you). If you replace the realloc()
with a bad one (like valgrind's), then the performance degrades severely.

This patch implements a HYBRID allocator which has the behaviour you describe (it's
like EXACT to start with, though without the spare 10 bytes; and switches to DOUBLEIT
after 4kb) — that gets the memory back down to 12755657, with no noticeable impact on the
performance of the synthetic pathological example under valgrind.

In summary:

     max_memory on ./xmllint --noout doc/libxml2-api.xml,
     valgrind time on https://gist.github.com/2656940

            max_memory    valgrind time
before   |  12755657    | 29:18.2
EXACT    |  12756699    |  2:58.6 <-- this is the state after the first patch.
DOUBLEIT |  12756727    |  0:02.7
HYBRID   |  12755754    |  0:02.7 <-- this is the state with both patches.

>
> There is also the cost of creating the buffers all the time.
> I need to read the code and check but I may be interested in an hybrid
> approach where we switch to buffer only when the text node starts to
> become too big (4k would remove nearly all usuall types of "document"
> usage, i.e. not blocks of data)

I tried to avoid too much buffer creation by introducing the xmlBufferDetach function,
which allows re-using one buffer to construct many strings. It's maybe a bit of a "hack"
in API terms though I thought the gains would be worth it.

Conrad

------8<------

To keep memory usage tight in normal conditions it's desirable to only
allocate as much space as is needed. Unfortunately this can lead to
problems when constructing a long string out of small chunks, because
every chunk you add will need to resize the buffer.

To fix this XML_ALLOC_HYBRID will switch (when the buffer is 4kb big)
from using exact allocations to doubling buffer size every time it is
full. This limits the number of buffer resizes to O(log n) (down from
O(n)), and thus greatly increases the performance of constructing very
large strings in this manner.
2 files changed
tree: 9efcefe713c890cfc4262f58a6f775e8cc8c6c54
  1. bakefile/
  2. doc/
  3. example/
  4. include/
  5. macos/
  6. optim/
  7. python/
  8. result/
  9. test/
  10. vms/
  11. VxWorks/
  12. win32/
  13. xstc/
  14. .cvsignore
  15. .gitignore
  16. acinclude.m4
  17. AUTHORS
  18. autogen.sh
  19. build_glob.py
  20. c14n.c
  21. catalog.c
  22. ChangeLog
  23. check-relaxng-test-suite.py
  24. check-relaxng-test-suite2.py
  25. check-xinclude-test-suite.py
  26. check-xml-test-suite.py
  27. check-xsddata-test-suite.py
  28. chvalid.c
  29. chvalid.def
  30. configure.in
  31. Copyright
  32. dbgen.pl
  33. dbgenattr.pl
  34. debugXML.c
  35. dict.c
  36. DOCBparser.c
  37. elfgcchack.h
  38. encoding.c
  39. entities.c
  40. error.c
  41. genChRanges.py
  42. gentest.py
  43. genUnicode.py
  44. global.data
  45. globals.c
  46. HACKING
  47. hash.c
  48. HTMLparser.c
  49. HTMLtree.c
  50. INSTALL.libxml2
  51. legacy.c
  52. libxml-2.0-uninstalled.pc.in
  53. libxml-2.0.pc.in
  54. libxml.3
  55. libxml.h
  56. libxml.m4
  57. libxml.spec.in
  58. libxml2.doap
  59. libxml2.syms
  60. list.c
  61. MAINTAINERS
  62. Makefile.am
  63. Makefile.tests
  64. Makefile.win
  65. nanoftp.c
  66. nanohttp.c
  67. NEWS
  68. parser.c
  69. parserInternals.c
  70. pattern.c
  71. README
  72. README.cvs-commits
  73. README.tests
  74. regressions.py
  75. regressions.xml
  76. relaxng.c
  77. rngparser.c
  78. runsuite.c
  79. runtest.c
  80. runxmlconf.c
  81. SAX.c
  82. SAX2.c
  83. schematron.c
  84. testapi.c
  85. testAutomata.c
  86. testC14N.c
  87. testchar.c
  88. testdict.c
  89. testdso.c
  90. testHTML.c
  91. testModule.c
  92. testOOM.c
  93. testOOMlib.c
  94. testOOMlib.h
  95. testReader.c
  96. testrecurse.c
  97. testRegexp.c
  98. testRelax.c
  99. testSAX.c
  100. testSchemas.c
  101. testThreads.c
  102. testThreadsWin32.c
  103. testURI.c
  104. testXPath.c
  105. threads.c
  106. TODO
  107. TODO_SCHEMAS
  108. tree.c
  109. trio.c
  110. trio.h
  111. triodef.h
  112. trionan.c
  113. trionan.h
  114. triop.h
  115. triostr.c
  116. triostr.h
  117. uri.c
  118. valid.c
  119. xinclude.c
  120. xlink.c
  121. xml2-config.1
  122. xml2-config.in
  123. xml2Conf.sh.in
  124. xmlcatalog.c
  125. xmlIO.c
  126. xmllint.c
  127. xmlmemory.c
  128. xmlmodule.c
  129. xmlreader.c
  130. xmlregexp.c
  131. xmlsave.c
  132. xmlschemas.c
  133. xmlschemastypes.c
  134. xmlstring.c
  135. xmlunicode.c
  136. xmlwriter.c
  137. xpath.c
  138. xpointer.c
  139. xzlib.c
  140. xzlib.h