2

I want to optimizes ObjectMapper for a list. The requirement is that I need to add a delimiter after each element of the list. My current code looks like :

    StringBuilder strBuilder = new StringBuilder();
    for (Event event : segregatedList) {
        String eventJson = mapper.writeValueAsString(event);
        strBuilder.append("\n");
        strBuilder.append(eventJson);

    }

This take a huge amount of time for a long list (~10000 events) .How can I optimize the code to do serializes the list in a single go ?

Suraj Menon
  • 1,486
  • 3
  • 28
  • 50
  • I think if you divide your task among multiple thread based on number of element in the `segregatedList`. for example if you are using 4 core cpu and you ~10000 list size, divide this list among 4 thread first thread process from `~ 0 - 2500`, second `~2500-5000` in the list and so on 4th thread. use countdown latch to ensure all thread gets completed. once all thread complete their task then combine result into one string. – Pandey Amit Nov 02 '18 at 07:15

2 Answers2

2

mapper instances are thread-safe, so you can split the mapper.writeValueAsString to a parallel job. I guess something like this may help if you don't worry of the order in which they are appended!

segregatedList.parallelStream().map(event -> mapper.writeValueAsString(event)).collect(Collectors.joining("\n")))

Otherwise, I can see very minimum scope of improving here. Maybe you can optimize json by ignoring properties as mentioned by Dark Knight

Mohamed Anees A
  • 4,119
  • 1
  • 22
  • 35
0

There are multiple ways to concatenate Strings in java.

  1. concat() method from java.lang.String
  2. Using + operator
  3. Using StringBuffer
  4. Using StringBuilder

From my personal analysis i can say + call on String gets translated into new StringBuilder().append( "" ). Since StringBuilder(String) constructor allocates a buffer with 16 char, appending more than 16 characters will require buffer reallocation. At last, StringBuffer.toString() calls create a new String object with a copy of StringBuilder buffer.

So if you don't want synchronization overhead StringBuilder stands best among others else i would advice you to use StringBuffer here. I see you are using StringBuilder already, so there is very little scope for improvement here. However you can optimize generated json by ignoring properties which are not useful.

Dark Knight
  • 8,218
  • 4
  • 39
  • 58