0

I am using lambdas for ensuring closing of the result set and database connection.

In my below code I am capturing variables like (activeList, inActiveList, user). In many places it was told, although linkage(one time cost) and invocation cost are similar for capturing and non-capturing lambdas, capturing lambdas are not performant, due to the intermediate "capture cost". At each invocation lambda object needs to be created. For non-capturing lambda it is one time, then subsequent calls will reuse the same object. But for capturing lambda each time object has to be created.

I have added my code snippet below. This type of capturing lambdas comes more often in my web-application code. My assumption is since number of select query, executed for each request is relatively low, using this type of capturing lambdas would not make considerable performance impact. Is it correct, or I am doing wrong.

User user = getUser(id);
List<Integer> activeList = new ArrayList<>();
List<Integer> inActiveList = new ArrayList<>();
HashMap<String, Object> h = new HashMap<>();

 DBUtil.execute(sqlQuery (rs) -> {
    String id = rs.getString("id");
    h.put("id", id)
    h.put("name", rs.getString("name"));

    if(user.isAdmin()){
      h.put("pay", re.getInt("pay"))
    }

    int status = rs.getInt("status");
    if(status == 1){
      activeList.add(id);
    } else if(status == 2){
      inActiveList.add(id);
    }

  })
static void execute(String sql, Consumer<ResultSet> iteratorFn) {
   try {
      Connection conn = getConn();
      Statement stmt = conn.createStatement(sql)
      ResultSet rs = stmt.executeQuery(sql);
      while(rs.next()){
        iteratorFn.accept(rs);
      }
      rs.close();
    } finally {
          if(stmt != null){
            stmt.close()
          }
          if(conn != null){
            conn.close()
          }
     }
}

Reference

https://ionutbalosin.com/2018/12/passing-thismethod-reference-within-a-loop-affects-performance/

https://www.infoq.com/articles/Java-8-Lambdas-A-Peek-Under-the-Hood/

peru
  • 29
  • 3
  • 1
    Why not try-with-resources? – chrylis -cautiouslyoptimistic- Mar 13 '20 at 05:05
  • execute() will be called from many places in my code. Using lambda will be better in terms of readability and easy to write than try-with-resources in all places where I execute query. – peru Mar 13 '20 at 05:25
  • 4
    I do not have hard numbers, but I would *never even dare* thinking that capturing lambda costs has *any kind of remotely noticeable* impact on a DB call. You are compariing a few instructions and memory loads against a ConnPool acquisition (synchronized), at least a network roundtrip (maybe more) DB, statement caching and compilation costs, plus query execution time and data serialziation. I really do not have number, but I'd bet you're comparing 10s of *nano* seconds to 10s of *milli* seconds. So back of the enveloppe : a 1 million factor betwenn the twos.I'd love to see real data though ! – GPI Mar 13 '20 at 09:40
  • The question is not about connection pool or roundtrip. My code is to explain my use case and give some context about have how frequent and when it will be called. My question is related to whether it is okay to use capturing lambdas for reading result of the select queries. Please refer https://www.slideshare.net/jaxlondon2012/lambda-a-peek-under-the-hood-brian-goetz – peru Mar 13 '20 at 10:37
  • 1
    _The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming._ [Donald E. Knuth, Computer Programming as an Art (1974)] – Abra Mar 14 '20 at 06:17

0 Answers0