Skip to content

JSONArray does not have constructor to allocate the specified initial capacity #516

Closed

Description

@viveksacademia4git

The collection class like ArrayList has the constructor that accepts the number as an input to "Constructs an empty list with the specified initial capacity."
The absence of this functionality in the JSONArray will not affect the performance of the smaller applications with a fairly limited number of iteration, but the huge applications that involve a large number of iterations will result in the performance trade-off because ArrayList grows or expands numerous times.

Activity

  1. changed the title [-]JSONArray does not have constructor to assign the specified initial capacity[/-] [+]JSONArray does not have constructor to allocate the specified initial capacity[/+] on May 6, 2020
  2. johnjaylward commented on May 6, 2020

    @johnjaylward
    Contributor

    You are right. If you have a collection you are converting to a JSONArray, this constructor does the trick: https://github.com/stleary/JSON-java/blob/master/JSONArray.java#L171

    However, if you are building up from scratch using an input where you can guess the size but not know an exact size, we don't expose a way to do that.

    We have a code reorganization going on in PR #515, but a PR to support this should be pretty straightforward.

  3. stleary commented on May 12, 2020

    @stleary
    Owner

    No objection to a pull request for this issue.

  4. viveksacademia4git commented on May 12, 2020

    @viveksacademia4git
    ContributorAuthor

    Should I make changes?

  5. stleary commented on May 12, 2020

    @stleary
    Owner

    Yes, please go ahead.

  6. viveksacademia4git commented on May 13, 2020

    @viveksacademia4git
    ContributorAuthor

    Thank you @stleary.

  7. viveksacademia4git commented on May 15, 2020

    @viveksacademia4git
    ContributorAuthor

    @johnjaylward and @stleary If we implement this constructor then I suppose that we also have to create a method to trim the size of the private ArrayList, like trimToSize(). So the final class will have these added components:

    • Constructor - JSONArray(int)
    • Method - Trims the size of private ArrayList

    What do you think, is it feasible?

  8. viveksacademia4git commented on May 16, 2020

    @viveksacademia4git
    ContributorAuthor

    I did not know that JSON-Java-unit-test existed for the purpose of unit testing, I will pull this branch.

  9. stleary commented on May 16, 2020

    @stleary
    Owner

    @viveksacademia4git You have a reasonable use case for the constructor. What is your use case for the method?

  10. viveksacademia4git commented on May 16, 2020

    @viveksacademia4git
    ContributorAuthor

    @stleary I do not have any practical use case as of now, but it occurred to me while working: what if someone has a use case which involves trimming the capacity of the underlying data structure.

    But as I said before, "is it feasible" at this moment to create this method. Or we could introduce this kind of method if someone has a genuine and practical use case, and creates an issue requesting this functionality.

  11. stleary commented on May 16, 2020

    @stleary
    Owner

    Recommend that you just add the constructor for now.

  12. viveksacademia4git commented on May 16, 2020

    @viveksacademia4git
    ContributorAuthor

    Cool!

  13. viveksacademia4git commented on May 16, 2020

    @viveksacademia4git
    ContributorAuthor

    @stleary I wasn't able to do the pull request that is why I preferred the fork option.
    Please check these commits forked from JSON-Java and JSON-Java-unit-test

  14. viveksacademia4git commented on May 16, 2020

    @viveksacademia4git
    ContributorAuthor

    I wasn't able to run the gradle test command but I executed the JUnit Test in Eclipse for the created test

  15. johnjaylward commented on Jun 1, 2020

    @johnjaylward
    Contributor

    @viveksacademia4git you will need to rebase because of the changes in project structure (or may be easier to just create an new branch and cherry-pick)

    once that's done you can use the compare options to create the PR:
    image

  16. added 2 commits that reference this issue on Jun 3, 2020
  17. viveksacademia4git commented on Jun 3, 2020

    @viveksacademia4git
    ContributorAuthor

    Hello @johnjaylward,

    I have rebased the project as suggested by you and added the previously written code into it; you may find the changes in the commit d088cf0.

    In addition to the above change, I have introduced the pipeline setting for this library in maven.yml and executed the pipelines (see actions). The pipeline has these stages maven: (1)compile, (2)test and (3)build for this library, and they are running successfully. I feel that it could provide an added advantage in terms of CI. I know this is not a part of this issue and I do not know whether it is required from your perspective so please let know whether

    • I should proceed with the pull request for this issue that also includes CI,
    • create another issue for CI
    • or CI is irrelevant as of now and just proceed with the pull request that contains changes for this issue only excluding CI.
  18. johnjaylward commented on Jun 3, 2020

    @johnjaylward
    Contributor

    @stleary I'm OK with the addition of the maven.yml, you?

  19. stleary commented on Jun 3, 2020

    @stleary
    Owner

    @viveksacademia4git What is the use case for adding CI to this project? Also, please provide an overview of you would go about doing it.

    For the initial capacity PR, let's stick to the agreed-upon changes.

  20. viveksacademia4git commented on Jun 3, 2020

    @viveksacademia4git
    ContributorAuthor

    Okay then, I will do the pull request with the agreed changes only.
    But, you may find the maven: compile, test and build report in the pipeline 123544252

  21. johnjaylward commented on Jun 3, 2020

    @johnjaylward
    Contributor

    @viveksacademia4git can you create a separate PR that just has the maven.yml changes?

  22. viveksacademia4git commented on Jun 3, 2020

    @viveksacademia4git
    ContributorAuthor

    Sure, I would be happy to do that.

  23. stleary commented on Jun 3, 2020

    @stleary
    Owner

    Please include text about why this change is needed and a description of the workflow from the point of view of the submitter. FYI, PR merges are on hold until the unit tests are fixed and a few other build problems are addressed.

  24. viveksacademia4git commented on Jun 3, 2020

    @viveksacademia4git
    ContributorAuthor

    Okay, @stleary and @johnjaylward,

    I will create the issue once I find that the unit-test issue is resolved.

  25. added a commit that references this issue on Jun 12, 2020
  26. viveksacademia4git commented on Jun 23, 2020

    @viveksacademia4git
    ContributorAuthor

    Closing this issue, since the code from PR is available in master branch.

  27. added a commit that references this issue on Feb 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions