Commit 01b1665
authored
Defer building HtmlPage id/name lookup index until first read (#1116)
* Defer building HtmlPage id/name lookup index until first read
Every element added during parsing eagerly invoked addMappedElement,
which issued two getAttribute("id"/"name") lookups, two
ConcurrentHashMap.put operations, and (when applicable) allocated a
MappedElementIndexEntry plus its inner ArrayList. For pages parsed
and walked via querySelector / iteration without any getElementById
call, the entire index is built and never read.
Add a mappedElementsBuilt_ flag on HtmlPage; addMappedElement and
removeMappedElement early-return until it flips. A new private
ensureMappedElementsBuilt() walks the document once on first read of
getElementById / getElementsById / getElementByName / getElementsByName
/ getElementsByIdAndOrName and populates both idMap_ and nameMap_;
subsequent mutations through addMappedElement/removeMappedElement
stay incremental as before. clone() resets the flag so cloned pages
re-lazy-build.
JMH HtmlUnitBenchmark.JMH on a ~30 KB HTML page (5 forks x 3 warmup
x 5 measurement = 25 samples, paired sequentially):
baseline: 633.205 +- 8.793 us/op
with this: 593.611 +- 13.110 us/op
delta: -39.6 us, -6.3%
Targeted tests across HtmlPageTest, HtmlPage2Test, HtmlPage3Test,
HtmlInlineFrameTest, HtmlFrameTest, HtmlFrameSetTest, HTMLDocumentTest,
Html*ParserTest: 1176 tests, 0 failures.
* Set mappedElementsBuilt_ after populating the maps
Per review feedback: the flag was being flipped before addElement() ran,
so a partial failure mid-walk would leave the page with built_=true and
a half-populated index, and subsequent reads would silently return
incomplete results.
Move the assignment after the populate so a thrown exception leaves
built_=false and the next read retries.1 parent 8e3d9cf commit 01b1665
1 file changed
Lines changed: 34 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
154 | 154 | | |
155 | 155 | | |
156 | 156 | | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
157 | 161 | | |
158 | 162 | | |
159 | 163 | | |
| |||
631 | 635 | | |
632 | 636 | | |
633 | 637 | | |
| 638 | + | |
634 | 639 | | |
635 | 640 | | |
636 | 641 | | |
| |||
1708 | 1713 | | |
1709 | 1714 | | |
1710 | 1715 | | |
| 1716 | + | |
1711 | 1717 | | |
1712 | 1718 | | |
1713 | 1719 | | |
| |||
1728 | 1734 | | |
1729 | 1735 | | |
1730 | 1736 | | |
| 1737 | + | |
1731 | 1738 | | |
1732 | 1739 | | |
1733 | 1740 | | |
| |||
1746 | 1753 | | |
1747 | 1754 | | |
1748 | 1755 | | |
| 1756 | + | |
1749 | 1757 | | |
1750 | 1758 | | |
1751 | 1759 | | |
| |||
1765 | 1773 | | |
1766 | 1774 | | |
1767 | 1775 | | |
| 1776 | + | |
1768 | 1777 | | |
1769 | 1778 | | |
1770 | 1779 | | |
| |||
1841 | 1850 | | |
1842 | 1851 | | |
1843 | 1852 | | |
| 1853 | + | |
| 1854 | + | |
| 1855 | + | |
| 1856 | + | |
| 1857 | + | |
1844 | 1858 | | |
1845 | 1859 | | |
1846 | 1860 | | |
1847 | 1861 | | |
1848 | 1862 | | |
1849 | 1863 | | |
| 1864 | + | |
| 1865 | + | |
| 1866 | + | |
| 1867 | + | |
| 1868 | + | |
| 1869 | + | |
| 1870 | + | |
| 1871 | + | |
| 1872 | + | |
| 1873 | + | |
| 1874 | + | |
| 1875 | + | |
| 1876 | + | |
| 1877 | + | |
| 1878 | + | |
1850 | 1879 | | |
1851 | 1880 | | |
1852 | 1881 | | |
| |||
1882 | 1911 | | |
1883 | 1912 | | |
1884 | 1913 | | |
| 1914 | + | |
| 1915 | + | |
| 1916 | + | |
| 1917 | + | |
1885 | 1918 | | |
1886 | 1919 | | |
1887 | 1920 | | |
| |||
1998 | 2031 | | |
1999 | 2032 | | |
2000 | 2033 | | |
| 2034 | + | |
2001 | 2035 | | |
2002 | 2036 | | |
2003 | 2037 | | |
| |||
0 commit comments