Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ContentTypeEngine improvement #534

Closed
mhagnumdw opened this issue Feb 28, 2020 · 4 comments
Closed

ContentTypeEngine improvement #534

mhagnumdw opened this issue Feb 28, 2020 · 4 comments

Comments

@mhagnumdw
Copy link
Member

Hi!

As an example I will take GsonEngine, but it must be valid for all implementations of ContentTypeEngine.

With each call to the fromString and toString methods, the GsonBuilder and Gson objects are created. Maybe it is good to cache Gson.

Below is a simple benchmark with 1,000,000 times:

GsonBuilder and Gson created inside the loop (not reusing)

public static void main(String[] args) {
    Stopwatch s = new Stopwatch();
    for (int i = 0; i < 1000000; i++) {
        GsonBuilder builder = SerializationUtils.getGsonBuilderToPippoJsonEngine();
        Gson gson = builder.create();
        Dummy d = new Dummy("key" + i, "value1" + i, DummyType.TEXT);
        String json = gson.toJson(p);
    }
    s.mark("End");
    System.out.println(s.toString());
}

Result

  • Round 1: [Begin -> End] 12642 => Total: 12642ms / 0d 00:00:12.642
  • Round 2: [Begin -> End] 13088 => Total: 13088ms / 0d 00:00:13.088
  • Round 3: [Begin -> End 12268 => Total: 12268ms / 0d 00:00:12.268

GsonBuilder and Gson created out of the loop (reusing)

public static void main(String[] args) {
    Stopwatch s = new Stopwatch();
    GsonBuilder builder = SerializationUtils.getGsonBuilderToPippoJsonEngine();
    Gson gson = builder.create();
    for (int i = 0; i < 1000000; i++) {
        Dummy d = new Dummy("key" + i, "value1" + i, DummyType.TEXT);
        String json = gson.toJson(p);
    }
    s.mark("End");
    System.out.println(s.toString());
}

Result

  • Round 1: [Begin -> End] 1542 => Total: 1542ms / 0d 00:00:01.542
  • Round 2: [Begin -> End] 1463 => Total: 1463ms / 0d 00:00:01.463
  • Round 3: [Begin -> End] 1487 => Total: 1487ms / 0d 00:00:01.487

Now I ask myself: given this time difference will it be worth it?

@decebals
Copy link
Member

The performance gain is very good, but I wonder if it doesn't come with a penalty. Maybe we miss something.
If you have a little time, I think that you can create a small PR for a particular Engine and we will see.

@mhagnumdw
Copy link
Member Author

Hi @decebals !

I created an MR for you to take a look.

Another thing ... but for another issue ... to be able to customize the engine. In my case I need to make extra configurations in GsonBuilder. What do you think?

@decebals
Copy link
Member

@mhagnumdw Thanks for PR. It's good.

Another thing ... but for another issue ... to be able to customize the engine. In my case I need to make extra configurations in GsonBuilder. What do you think?

Yes. It's also a good idea. I want the developer to have total control for almost everything in Pippo (to be able to create an instance of ContentTypeEngine and to register that instance). We come by default with some default implementations but the developer should be able to adapt or even change the created instance with his own version. Please create a new issue and I will come add more comments.

@mhagnumdw
Copy link
Member Author

MR #535 solves this.

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

No branches or pull requests

2 participants