You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by GitBox <gi...@apache.org> on 2021/09/08 14:03:32 UTC

[GitHub] [pinot] richardstartin opened a new pull request #7412: Reduce allocation in JSONPath evaluation

richardstartin opened a new pull request #7412:
URL: https://github.com/apache/pinot/pull/7412


   ## Description
   
   The JSONPath library allocates a stateless object and reinitialises configuration once per JSON path invocation, which contributes to high allocation rate per JSON path evaluation and shows up in profiles. This is another piece of profiling motivated low hanging fruit.
   
   <img width="753" alt="Screenshot 2021-09-08 at 14 04 56" src="https://user-images.githubusercontent.com/16439049/132522686-6f7276a1-e6db-4cd8-bef6-88401b6bd554.png">
   <img width="1451" alt="Screenshot 2021-09-08 at 14 05 24" src="https://user-images.githubusercontent.com/16439049/132522702-cbe4978b-4cec-4c36-a5f8-80dab2f41ff3.png">
   
   This change allocates `ParseContextImpl` once during `JsonFunctions.<clinit>` and also avoids varargs allocations, which reduces memory allocated per JSON path evaluation by 152 bytes and improves throughput by ~5%.
   
   Before:
   ```
   Benchmark                                                     (_json)                          (_jsonPath)   Mode  Cnt     Score     Error   Units
   JSONPathEvaluationBenchmark.get                               DEFAULT  $.tool.jsonpath.creator.location[2]  thrpt    5   736.002 ±   5.899  ops/ms
   JSONPathEvaluationBenchmark.get:·gc.alloc.rate.norm           DEFAULT  $.tool.jsonpath.creator.location[2]  thrpt    5  3056.006 ±   0.001    B/op
   ```
   
   After
   ```
   Benchmark                                                     (_json)                          (_jsonPath)   Mode  Cnt     Score     Error   Units
   JSONPathEvaluationBenchmark.get                               DEFAULT  $.tool.jsonpath.creator.location[2]  thrpt    5   770.663 ±   4.739  ops/ms
   JSONPathEvaluationBenchmark.get:·gc.alloc.rate.norm           DEFAULT  $.tool.jsonpath.creator.location[2]  thrpt    5  2904.006 ±   0.001    B/op
   ```
   
   ```java
   @State(Scope.Benchmark)
   public class JSONPathEvaluationBenchmark {
   
     public enum JSON {
       DEFAULT("{\n"
           + "    \"tool\": \n"
           + "    {\n"
           + "        \"jsonpath\": \n"
           + "        {\n"
           + "            \"creator\": \n"
           + "            {\n"
           + "                \"name\": \"Jayway Inc.\",\n"
           + "                \"location\": \n"
           + "                [\n"
           + "                    \"Malmo\",\n"
           + "                    \"San Francisco\",\n"
           + "                    \"Helsingborg\"\n"
           + "                ]\n"
           + "            }\n"
           + "        }\n"
           + "    }\n"
           + "}");
   
       private final String json;
   
       JSON(String json) {
         this.json = json;
       }
   
       @Override
       public String toString() {
         return json;
       }
     }
   
     @Param
     private JSON _json;
   
     @Param("$.tool.jsonpath.creator.location[2]")
     private String _jsonPath;
   
     @Setup(Level.Trial)
     public void setup() {
       // avoid bad quality cache from dominating
       CacheProvider.setCache(new Cache() {
         private final Map<String, JsonPath> _map = new HashMap<>();
         @Override
         public JsonPath get(String key) {
           return _map.get(key);
         }
   
         @Override
         public void put(String key, JsonPath value) {
           _map.put(key, value);
         }
       });
     }
   
     @Benchmark
     public Object get() {
       return JsonFunctions.jsonPath(_json.toString(), _jsonPath);
     }
   }
   ```
   
   ## Upgrade Notes
   Does this PR prevent a zero down-time upgrade? (Assume upgrade order: Controller, Broker, Server, Minion)
   * [ ] Yes (Please label as **<code>backward-incompat</code>**, and complete the section below on Release Notes)
   
   Does this PR fix a zero-downtime upgrade introduced earlier?
   * [ ] Yes (Please label this as **<code>backward-incompat</code>**, and complete the section below on Release Notes)
   
   Does this PR otherwise need attention when creating release notes? Things to consider:
   - New configuration options
   - Deprecation of configurations
   - Signature changes to public methods/interfaces
   - New plugins added or old plugins removed
   * [ ] Yes (Please label this PR as **<code>release-notes</code>** and complete the section on Release Notes)
   ## Release Notes
   <!-- If you have tagged this as either backward-incompat or release-notes,
   you MUST add text here that you would like to see appear in release notes of the
   next release. -->
   
   <!-- If you have a series of commits adding or enabling a feature, then
   add this section only in final commit that marks the feature completed.
   Refer to earlier release notes to see examples of text.
   -->
   ## Documentation
   <!-- If you have introduced a new feature or configuration, please add it to the documentation as well.
   See https://docs.pinot.apache.org/developers/developers-and-contributors/update-document
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] Jackie-Jiang merged pull request #7412: Reduce allocation in JSONPath evaluation

Posted by GitBox <gi...@apache.org>.
Jackie-Jiang merged pull request #7412:
URL: https://github.com/apache/pinot/pull/7412


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a change in pull request #7412: Reduce allocation in JSONPath evaluation

Posted by GitBox <gi...@apache.org>.
richardstartin commented on a change in pull request #7412:
URL: https://github.com/apache/pinot/pull/7412#discussion_r704476933



##########
File path: pinot-common/src/main/java/org/apache/pinot/common/function/scalar/JsonFunctions.java
##########
@@ -97,9 +102,9 @@ public static String jsonFormat(Object object)
   @ScalarFunction
   public static Object jsonPath(Object object, String jsonPath) {
     if (object instanceof String) {
-      return JsonPath.read((String) object, jsonPath);
+      return PARSE_CONTEXT.parse((String) object).read(jsonPath, NO_PREDICATES);

Review comment:
       yes, I don't know why it's allocated each time in the library, it just builds a `Configuration` each time (also thread-safe) which makes a wrapper around a Jackson `ObjectMapper` (thread-safe) available via `configuration.jsonProvider()`:
   
   ```java
       private final Configuration configuration;
   
       public ParseContextImpl() {
           this(Configuration.defaultConfiguration());
       }
   
       public ParseContextImpl(Configuration configuration) {
           this.configuration = configuration;
       }
   
       @Override
       public DocumentContext parse(String json) {
           notEmpty(json, "json string can not be null or empty");
           Object obj = configuration.jsonProvider().parse(json);
           return new JsonContext(obj, configuration);
       }
   ```
   
   It might be best to fix this in the library but there's no guarantee on if or when that will get merged...




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a change in pull request #7412: Reduce allocation in JSONPath evaluation

Posted by GitBox <gi...@apache.org>.
richardstartin commented on a change in pull request #7412:
URL: https://github.com/apache/pinot/pull/7412#discussion_r705017892



##########
File path: pinot-common/src/main/java/org/apache/pinot/common/function/scalar/JsonFunctions.java
##########
@@ -97,9 +102,9 @@ public static String jsonFormat(Object object)
   @ScalarFunction
   public static Object jsonPath(Object object, String jsonPath) {
     if (object instanceof String) {
-      return JsonPath.read((String) object, jsonPath);
+      return PARSE_CONTEXT.parse((String) object).read(jsonPath, NO_PREDICATES);

Review comment:
       Yes, it prevents from allocating the array on each call. Alternatively we could try to add no varargs overloads to the library, which would be cleaner modulo the point above about guarantees.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] Jackie-Jiang commented on a change in pull request #7412: Reduce allocation in JSONPath evaluation

Posted by GitBox <gi...@apache.org>.
Jackie-Jiang commented on a change in pull request #7412:
URL: https://github.com/apache/pinot/pull/7412#discussion_r704867325



##########
File path: pinot-common/src/main/java/org/apache/pinot/common/function/scalar/JsonFunctions.java
##########
@@ -97,9 +102,9 @@ public static String jsonFormat(Object object)
   @ScalarFunction
   public static Object jsonPath(Object object, String jsonPath) {
     if (object instanceof String) {
-      return JsonPath.read((String) object, jsonPath);
+      return PARSE_CONTEXT.parse((String) object).read(jsonPath, NO_PREDICATES);

Review comment:
       Is there performance gain from passing in an empty array vs not passing the predicates?
   ```suggestion
         return PARSE_CONTEXT.parse((String) object).read(jsonPath);
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] kishoreg commented on a change in pull request #7412: Reduce allocation in JSONPath evaluation

Posted by GitBox <gi...@apache.org>.
kishoreg commented on a change in pull request #7412:
URL: https://github.com/apache/pinot/pull/7412#discussion_r704472738



##########
File path: pinot-common/src/main/java/org/apache/pinot/common/function/scalar/JsonFunctions.java
##########
@@ -97,9 +102,9 @@ public static String jsonFormat(Object object)
   @ScalarFunction
   public static Object jsonPath(Object object, String jsonPath) {
     if (object instanceof String) {
-      return JsonPath.read((String) object, jsonPath);
+      return PARSE_CONTEXT.parse((String) object).read(jsonPath, NO_PREDICATES);

Review comment:
       I am guessing this is threadsafe?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org