Skip to content
This repository was archived by the owner on Oct 30, 2023. It is now read-only.
This repository was 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

@amirulzin

Description

@amirulzin

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions