Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Optimization: Jackson ObjectMapper instance shouldn't be constructed on each WebSocket Message #29

Closed
amirulzin opened this issue Jan 11, 2018 · 0 comments

Comments

@amirulzin
Copy link

Related code:

@Override
public void onMessage(WebSocket webSocket, String text) {
ObjectMapper mapper = new ObjectMapper();
try {
T event = mapper.readValue(text, eventClass);
callback.onResponse(event);
} catch (IOException e) {
throw new BinanceApiException(e);
}
}

ObjectMapper is fairly expensive to create, and especially for Android, this can create quite the heavy pressure on the GC and memory usage, considering it is created on every socket message.

Therefore I kindly suggest to either:

  1. Delegate the instance construction to the BinanceApiWebSocketListener class constructor so we can allow more flexibility in creating ObjectMapper.
  2. And maybe create the ObjectMapper instance if it wasn't given and hold it locally as a private reference in a secondary constructor. This can maintain the current library compatibility.

The same issue also present in UserDataUpdateEventDeserializer

public <T> T getUserDataUpdateEventDetail(String json, Class<T> clazz) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(json, clazz);
} catch (IOException e) {
throw new BinanceApiException(e);
}
}

I can help creating a pull request later to fix this, but if you guys have any other idea or other implementations in mind, then please feel free to share.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant