Skip to content

Grow ArrayCollection geometrically when inserting at an index - #106

Open
rotundtapir wants to merge 1 commit into
REAndroid:mainfrom
rotundtapir:pr/array-collection-growth
Open

rotundtapir wants to merge 1 commit into
REAndroid:mainfrom
rotundtapir:pr/array-collection-growth

Conversation

@rotundtapir

@rotundtapir rotundtapir commented Sep 27, 2026 •

Copy link
Copy Markdown

add(index, item) goes through slideRight(), which reserves exactly the missing slot, so inserting n items one at a time copies the array n times. Building a type block entry by entry, as TableBlock.merge() does, is quadratic because of it. With this change alone, ApkBundle.mergeModules() on the split bundle of the Google app (com.google.android.googlequicksearchbox 17.61.20, 87 MB resources.arsc) spends 16 s merging instead of 39 s.

It also fixes ensureCapacity(int) allocating too little when some capacity is already free. It subtracted the free slots from the request and then added the result to the size instead of the current length, so it could leave fewer free slots than asked for. ArrayCollectionTest covers both.

Independent of #104. It's the first of three split-merge changes; the other two (merging bundle tables without copying the base table, and less memory per table entry) will follow separately.

add(index, item) went through slideRight(), which reserved exactly the
missing slot, so every insertion reallocated and copied the whole array.
Building a type block entry by entry, as merging tables does, was quadratic
because of it.

Also fix ensureCapacity(int) allocating too little when some capacity is
already free: it subtracted the free slots from the request and then added
the result to the size instead of to the current length, which could leave
fewer free slots than asked for.
@REAndroid

Copy link
Copy Markdown
Owner

I can't merge because i blocked claude. Can you please make clean PR without claude ?

@rotundtapir
rotundtapir force-pushed the pr/array-collection-growth branch from 308e74e to 163afc3 Compare September 28, 2026 20:07
@rotundtapir

Copy link
Copy Markdown
Author

Done, the commit is rewritten without the co-author line, same code. Thanks for taking a look!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants