Skip to content

Avoid quadratic clearing of JsonArray sublists - #3157

Open
wozhenbang2004 wants to merge 2 commits into
google:mainfrom
wozhenbang2004:fix/json-array-sublist-clear
Open

wozhenbang2004 wants to merge 2 commits into
google:mainfrom
wozhenbang2004:fix/json-array-sublist-clear

Conversation

@wozhenbang2004

Copy link
Copy Markdown

Purpose

Clearing a range through JsonArray.asList().subList(from, to).clear() currently performs quadratic work for large ranges.

Description

NonNullElementWrapperList inherits AbstractList.removeRange, which removes each element separately. Each indexed removal shifts the remaining elements in its backing ArrayList. Override removeRange to delegate to the backing list's sublist clear, which removes the range in one operation. This complements #3115 for a separate bulk-removal path.

Add tests to the existing JsonArrayAsListTest for middle, nested, and empty ranges, continued use of a cleared view, and consistency with the backing JsonArray. No public API is added.

Validation: compiled the complete wrapper source in memory with JDK 17, including with --release 8, and ran behavior and operation-count probes. Clearing 50,000 elements from a 100,000-element list invoked remove(int) 50,000 times before this change and zero times afterward. Middle/nested/empty ranges, further insertion through the cleared view, and null rejection passed. The added unit tests cover view behavior; the operation-count probe checks the performance regression. The full Maven/JUnit suite has not been run locally; the existing PR workflows will provide that validation.

Checklist

  • New code follows the Google Java Style Guide
    This is automatically checked by mvn verify, but can also be checked on its own using mvn spotless:check.
    Style violations can be fixed using mvn spotless:apply; this can be done in a separate commit to verify that it did not cause undesired changes.
  • If necessary, new public API validates arguments, for example rejects null
  • New public API has Javadoc
    • Javadoc uses @since $next-version$
      ($next-version$ is a special placeholder which is automatically replaced during release)
  • If necessary, new unit tests have been added
    • Assertions in unit tests use Truth, see existing tests
    • No JUnit 3 features are used (such as extending class TestCase)
    • If this pull request fixes a bug, a new test was added for a situation which failed previously and is now fixed
  • mvn clean verify javadoc:jar passes without errors

Delegate range removal to the backing ArrayList so clearing a sublist
does not repeatedly shift the remaining elements.
…ublist views

Cover middle, nested, and empty ranges and verify that changes remain
visible through the backing JsonArray and list views.

This branch has not been deployed

No deployments
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.

1 participant