Skip to content

Commit

Permalink
Resuse the gson instance when parsing source maps
Browse files Browse the repository at this point in the history
Gson object are kind of expensive to construct (due to the thread local instances).

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=239489982
  • Loading branch information
lukesandberg authored and EatingW committed Mar 21, 2019
1 parent 922b7bf commit 431ca7d
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@
* Java implementation of the source map parser.
*/
public class SourceMapObjectParser {
// Gson objects are safe to share across threads and are somewhat expensive to construct. Use a
// single instance. This is Safe per gson docs:
// https://google.github.io/gson/apidocs/com/google/gson/Gson.html
private static final Gson gson = new Gson();

public static SourceMapObject parse(String contents) throws SourceMapParseException {

SourceMapObject.Builder builder = SourceMapObject.builder();

try {
JsonObject sourceMapRoot = new Gson().fromJson(contents, JsonObject.class);
JsonObject sourceMapRoot = gson.fromJson(contents, JsonObject.class);

builder.setVersion(sourceMapRoot.get("version").getAsInt());
builder.setFile(getStringOrNull(sourceMapRoot, "file"));
Expand Down

0 comments on commit 431ca7d

Please sign in to comment.