trafficserver-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jpe...@apache.org
Subject [trafficserver] branch master updated: Remove inefficient use of std::string in C++ transformation plugins.
Date Thu, 15 Mar 2018 17:51:33 GMT
This is an automated email from the ASF dual-hosted git repository.

jpeach pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 13e5d22  Remove inefficient use of std::string in C++ transformation plugins.
13e5d22 is described below

commit 13e5d2294b121896a55c70c9f267971ba172d750
Author: Walt Karas <wkaras@yahoo-inc.com>
AuthorDate: Thu Mar 8 23:35:18 2018 +0000

    Remove inefficient use of std::string in C++ transformation plugins.
    
    Change 'const std::string &' parameters to virtual member functions of the TranformationPlugin
class to ts::string_view.  This
    allows derived classes of TranformationPlugin to avoid using std::string in some cases,
resulting in less copying and dynamic
    memory allocation.
---
 .../DelayTransformationPlugin.cc                       |  4 ++--
 .../gzip_transformation/GzipTransformationPlugin.cc    |  3 ++-
 .../NullTransformationPlugin.cc                        |  4 ++--
 example/cppapi/post_buffer/PostBuffer.cc               |  5 +++--
 lib/cppapi/GzipDeflateTransformation.cc                | 11 +++++------
 lib/cppapi/GzipInflateTransformation.cc                |  9 ++++-----
 lib/cppapi/TransformationPlugin.cc                     |  8 ++++----
 .../include/atscppapi/GzipDeflateTransformation.h      |  9 +++------
 .../include/atscppapi/GzipInflateTransformation.h      | 10 +++-------
 lib/cppapi/include/atscppapi/TransformationPlugin.h    | 18 ++++++------------
 plugins/experimental/webp_transform/ImageTransform.cc  | 11 ++++-------
 11 files changed, 38 insertions(+), 54 deletions(-)

diff --git a/example/cppapi/delay_transformation_plugin/DelayTransformationPlugin.cc b/example/cppapi/delay_transformation_plugin/DelayTransformationPlugin.cc
index bd83445..b1fcca7 100644
--- a/example/cppapi/delay_transformation_plugin/DelayTransformationPlugin.cc
+++ b/example/cppapi/delay_transformation_plugin/DelayTransformationPlugin.cc
@@ -17,6 +17,7 @@
  */
 
 #include <iostream>
+#include <ts/string_view.h>
 #include <atscppapi/GlobalPlugin.h>
 #include <atscppapi/TransactionPlugin.h>
 #include <atscppapi/TransformationPlugin.h>
@@ -26,7 +27,6 @@
 #include "ts/ts.h"
 
 using namespace atscppapi;
-using std::string;
 
 /*
  * This example demonstrates how you can pause a transformation and resume it
@@ -65,7 +65,7 @@ public:
   }
 
   void
-  consume(const string &data)
+  consume(ts::string_view data)
   {
     TS_DEBUG(TAG, "Consuming...");
     produce(data);
diff --git a/example/cppapi/gzip_transformation/GzipTransformationPlugin.cc b/example/cppapi/gzip_transformation/GzipTransformationPlugin.cc
index b4babb5..e7e5940 100644
--- a/example/cppapi/gzip_transformation/GzipTransformationPlugin.cc
+++ b/example/cppapi/gzip_transformation/GzipTransformationPlugin.cc
@@ -17,6 +17,7 @@
  */
 
 #include <iostream>
+#include <ts/string_view.h>
 #include <atscppapi/GlobalPlugin.h>
 #include <atscppapi/TransactionPlugin.h>
 #include <atscppapi/TransformationPlugin.h>
@@ -103,7 +104,7 @@ public:
   }
 
   void
-  consume(const string &data) override
+  consume(ts::string_view data) override
   {
     produce(data);
   }
diff --git a/example/cppapi/null_transformation_plugin/NullTransformationPlugin.cc b/example/cppapi/null_transformation_plugin/NullTransformationPlugin.cc
index d85732e..0fa649c 100644
--- a/example/cppapi/null_transformation_plugin/NullTransformationPlugin.cc
+++ b/example/cppapi/null_transformation_plugin/NullTransformationPlugin.cc
@@ -17,6 +17,7 @@
  */
 
 #include <iostream>
+#include <ts/string_view.h>
 #include <atscppapi/GlobalPlugin.h>
 #include <atscppapi/TransactionPlugin.h>
 #include <atscppapi/TransformationPlugin.h>
@@ -24,7 +25,6 @@
 #include <atscppapi/Logger.h>
 
 using namespace atscppapi;
-using std::string;
 
 namespace
 {
@@ -57,7 +57,7 @@ public:
   }
 
   void
-  consume(const string &data) override
+  consume(ts::string_view data) override
   {
     produce(data);
   }
diff --git a/example/cppapi/post_buffer/PostBuffer.cc b/example/cppapi/post_buffer/PostBuffer.cc
index 941de6b..feda07a 100644
--- a/example/cppapi/post_buffer/PostBuffer.cc
+++ b/example/cppapi/post_buffer/PostBuffer.cc
@@ -17,6 +17,7 @@
  */
 
 #include <iostream>
+#include <ts/string_view.h>
 #include <atscppapi/GlobalPlugin.h>
 #include <atscppapi/TransactionPlugin.h>
 #include <atscppapi/TransformationPlugin.h>
@@ -43,9 +44,9 @@ public:
   }
 
   void
-  consume(const string &data) override
+  consume(ts::string_view data) override
   {
-    buffer_.append(data);
+    buffer_.append(data.data(), data.length());
   }
 
   void
diff --git a/lib/cppapi/GzipDeflateTransformation.cc b/lib/cppapi/GzipDeflateTransformation.cc
index 43aa71c..0695489 100644
--- a/lib/cppapi/GzipDeflateTransformation.cc
+++ b/lib/cppapi/GzipDeflateTransformation.cc
@@ -19,17 +19,16 @@
  * @file GzipDeflateTransformation.cc
  */
 
-#include <string>
 #include <cstring>
 #include <vector>
 #include <zlib.h>
 #include <cinttypes>
+#include <ts/string_view.h>
 #include "atscppapi/TransformationPlugin.h"
 #include "atscppapi/GzipDeflateTransformation.h"
 #include "logging_internal.h"
 
 using namespace atscppapi::transformations;
-using std::string;
 using std::vector;
 
 namespace
@@ -81,7 +80,7 @@ GzipDeflateTransformation::~GzipDeflateTransformation()
 }
 
 void
-GzipDeflateTransformation::consume(const string &data)
+GzipDeflateTransformation::consume(ts::string_view data)
 {
   if (data.size() == 0) {
     return;
@@ -94,7 +93,7 @@ GzipDeflateTransformation::consume(const string &data)
 
   int iteration               = 0;
   state_->z_stream_.data_type = Z_ASCII;
-  state_->z_stream_.next_in   = reinterpret_cast<unsigned char *>(const_cast<char
*>(data.c_str()));
+  state_->z_stream_.next_in   = reinterpret_cast<unsigned char *>(const_cast<char
*>(data.data()));
   state_->z_stream_.avail_in  = data.length();
 
   // For small payloads the size can actually be greater than the original input
@@ -119,7 +118,7 @@ GzipDeflateTransformation::consume(const string &data)
 
     LOG_DEBUG("Iteration %d: Deflate compressed %ld bytes to %d bytes, producing output...",
iteration, data.size(),
               bytes_to_write);
-    produce(string(reinterpret_cast<char *>(&buffer[0]), static_cast<size_t>(bytes_to_write)));
+    produce(ts::string_view(reinterpret_cast<char *>(&buffer[0]), static_cast<size_t>(bytes_to_write)));
   } while (state_->z_stream_.avail_out == 0);
 
   state_->z_stream_.next_out = nullptr;
@@ -153,7 +152,7 @@ GzipDeflateTransformation::handleInputComplete()
     if (status == Z_OK || status == Z_STREAM_END) {
       LOG_DEBUG("Iteration %d: Gzip deflate finalize had an extra %d bytes to process, status
'%d'. Producing output...", iteration,
                 bytes_to_write, status);
-      produce(string(reinterpret_cast<char *>(buffer), static_cast<size_t>(bytes_to_write)));
+      produce(ts::string_view(reinterpret_cast<char *>(buffer), static_cast<size_t>(bytes_to_write)));
     } else if (status != Z_STREAM_END) {
       LOG_ERROR("Iteration %d: Gzip deflinate finalize produced an error '%d'", iteration,
status);
     }
diff --git a/lib/cppapi/GzipInflateTransformation.cc b/lib/cppapi/GzipInflateTransformation.cc
index bd73b3f..e0c8f12 100644
--- a/lib/cppapi/GzipInflateTransformation.cc
+++ b/lib/cppapi/GzipInflateTransformation.cc
@@ -20,17 +20,16 @@
  * @file GzipInflateTransformation.cc
  */
 
-#include <string>
 #include <cstring>
 #include <vector>
 #include <zlib.h>
 #include <cinttypes>
+#include <ts/string_view.h>
 #include "atscppapi/TransformationPlugin.h"
 #include "atscppapi/GzipInflateTransformation.h"
 #include "logging_internal.h"
 
 using namespace atscppapi::transformations;
-using std::string;
 using std::vector;
 
 namespace
@@ -84,7 +83,7 @@ GzipInflateTransformation::~GzipInflateTransformation()
 }
 
 void
-GzipInflateTransformation::consume(const string &data)
+GzipInflateTransformation::consume(ts::string_view data)
 {
   if (data.size() == 0) {
     return;
@@ -101,7 +100,7 @@ GzipInflateTransformation::consume(const string &data)
   vector<char> buffer(inflate_block_size);
 
   // Setup the compressed input
-  state_->z_stream_.next_in  = reinterpret_cast<unsigned char *>(const_cast<char
*>(data.c_str()));
+  state_->z_stream_.next_in  = reinterpret_cast<unsigned char *>(const_cast<char
*>(data.data()));
   state_->z_stream_.avail_in = data.length();
 
   // Loop while we have more data to inflate
@@ -123,7 +122,7 @@ GzipInflateTransformation::consume(const string &data)
 
     LOG_DEBUG("Iteration %d: Gzip inflated a total of %d bytes, producingOutput...", iteration,
               (inflate_block_size - state_->z_stream_.avail_out));
-    produce(string(&buffer[0], (inflate_block_size - state_->z_stream_.avail_out)));
+    produce(ts::string_view(&buffer[0], (inflate_block_size - state_->z_stream_.avail_out)));
     state_->bytes_produced_ += (inflate_block_size - state_->z_stream_.avail_out);
   }
   state_->z_stream_.next_out = nullptr;
diff --git a/lib/cppapi/TransformationPlugin.cc b/lib/cppapi/TransformationPlugin.cc
index fa9c6b9..2746d7c 100644
--- a/lib/cppapi/TransformationPlugin.cc
+++ b/lib/cppapi/TransformationPlugin.cc
@@ -302,7 +302,7 @@ TransformationPlugin::resumeCallback(TSCont cont, TSEvent event, void
*edata)
 }
 
 size_t
-TransformationPlugin::doProduce(const std::string &data)
+TransformationPlugin::doProduce(ts::string_view data)
 {
   LOG_DEBUG("TransformationPlugin=%p tshttptxn=%p producing output with length=%ld", this,
state_->txn_, data.length());
   int64_t write_length = static_cast<int64_t>(data.length());
@@ -331,7 +331,7 @@ TransformationPlugin::doProduce(const std::string &data)
   }
 
   // Finally we can copy this data into the output_buffer
-  int64_t bytes_written = TSIOBufferWrite(state_->output_buffer_, data.c_str(), write_length);
+  int64_t bytes_written = TSIOBufferWrite(state_->output_buffer_, data.data(), write_length);
   state_->bytes_written_ += bytes_written; // So we can set BytesDone on outputComplete().
   LOG_DEBUG("TransformationPlugin=%p tshttptxn=%p write to TSIOBuffer %" PRId64 " bytes total
bytes written %" PRId64, this,
             state_->txn_, bytes_written, state_->bytes_written_);
@@ -358,10 +358,10 @@ TransformationPlugin::doProduce(const std::string &data)
 }
 
 size_t
-TransformationPlugin::produce(const std::string &data)
+TransformationPlugin::produce(ts::string_view data)
 {
   if (state_->type_ == REQUEST_TRANSFORMATION) {
-    state_->request_xform_output_.append(data);
+    state_->request_xform_output_.append(data.data(), data.length());
     return data.size();
   } else if (state_->type_ == SINK_TRANSFORMATION) {
     LOG_DEBUG("produce TransformationPlugin=%p tshttptxn=%p : This is a sink transform. Not
producing any output", this,
diff --git a/lib/cppapi/include/atscppapi/GzipDeflateTransformation.h b/lib/cppapi/include/atscppapi/GzipDeflateTransformation.h
index f25b009..dd23561 100644
--- a/lib/cppapi/include/atscppapi/GzipDeflateTransformation.h
+++ b/lib/cppapi/include/atscppapi/GzipDeflateTransformation.h
@@ -22,10 +22,9 @@
  */
 
 #pragma once
-#ifndef ATSCPPAPI_GZIPDEFLATETRANSFORMATION_H_
-#define ATSCPPAPI_GZIPDEFLATETRANSFORMATION_H_
 
 #include <string>
+#include <ts/string_view.h>
 #include "atscppapi/TransformationPlugin.h"
 
 namespace atscppapi
@@ -71,13 +70,13 @@ namespace transformations
      *
      * @param data the input data to compress
      */
-    void consume(const std::string &data);
+    void consume(ts::string_view data) override;
 
     /**
      * Any TransformationPlugin must implement handleInputComplete(), this method will
      * finalize the gzip compression and flush any remaining data and the epilouge.
      */
-    void handleInputComplete();
+    void handleInputComplete() override;
 
     virtual ~GzipDeflateTransformation();
 
@@ -86,5 +85,3 @@ namespace transformations
   };
 }
 }
-
-#endif /* ATSCPPAPI_GZIPDEFLATETRANSFORMATION_H_ */
diff --git a/lib/cppapi/include/atscppapi/GzipInflateTransformation.h b/lib/cppapi/include/atscppapi/GzipInflateTransformation.h
index d0e4713..425d1ed 100644
--- a/lib/cppapi/include/atscppapi/GzipInflateTransformation.h
+++ b/lib/cppapi/include/atscppapi/GzipInflateTransformation.h
@@ -22,10 +22,8 @@
  */
 
 #pragma once
-#ifndef ATSCPPAPI_GZIPINFLATETRANSFORMATION_H_
-#define ATSCPPAPI_GZIPINFLATETRANSFORMATION_H_
 
-#include <string>
+#include <ts/string_view.h>
 #include "atscppapi/TransformationPlugin.h"
 
 namespace atscppapi
@@ -73,13 +71,13 @@ namespace transformations
      *
      * @param data the input data to decompress
      */
-    void consume(const std::string &);
+    void consume(ts::string_view) override;
 
     /**
      * Any TransformationPlugin must implement handleInputComplete(), this method will
      * finalize the gzip decompression.
      */
-    void handleInputComplete();
+    void handleInputComplete() override;
 
     virtual ~GzipInflateTransformation();
 
@@ -90,5 +88,3 @@ namespace transformations
 } /* transformations */
 
 } /* atscppapi */
-
-#endif /* ATSCPPAPI_GZIPINFLATETRANSFORMATION_H_ */
diff --git a/lib/cppapi/include/atscppapi/TransformationPlugin.h b/lib/cppapi/include/atscppapi/TransformationPlugin.h
index 714d8e2..7c640fc 100644
--- a/lib/cppapi/include/atscppapi/TransformationPlugin.h
+++ b/lib/cppapi/include/atscppapi/TransformationPlugin.h
@@ -21,10 +21,8 @@
  */
 
 #pragma once
-#ifndef ATSCPPAPI_TRANSFORMATIONPLUGIN_H_
-#define ATSCPPAPI_TRANSFORMATIONPLUGIN_H_
 
-#include <string>
+#include <ts/string_view.h>
 #include <atscppapi/Transaction.h>
 #include <atscppapi/TransactionPlugin.h>
 
@@ -64,7 +62,7 @@ struct TransformationPluginState;
  *     transaction.getClientResponse().getHeaders().set("X-Content-Transformed", "1");
  *     transaction.resume();
  *   }
- *   void consume(const string &data) {
+ *   void consume(ts::string_view data) {
  *     produce(data);
  *   }
  *   void handleInputComplete() {
@@ -95,7 +93,7 @@ public:
    * A method that you must implement when writing a TransformationPlugin, this method will
be
    * fired whenever an upstream TransformationPlugin has produced output.
    */
-  virtual void consume(const std::string &data) = 0;
+  virtual void consume(ts::string_view data) = 0;
 
   /**
   * Call this method if you wish to pause the transformation.
@@ -117,11 +115,9 @@ public:
 protected:
   /**
    * This method is how a TransformationPlugin will produce output for the downstream
-   * transformation plugin, if you need to produce binary data this can still be
-   * done with strings by a call to string::assign() or by constructing a string
-   * with string::string(char *, size_t).
+   * transformation plugin.
    */
-  size_t produce(const std::string &);
+  size_t produce(ts::string_view);
 
   /**
    * This is the method that you must call when you're done producing output for
@@ -134,10 +130,8 @@ protected:
 
 private:
   TransformationPluginState *state_; /** Internal state for a TransformationPlugin */
-  size_t doProduce(const std::string &);
+  size_t doProduce(ts::string_view);
   static int resumeCallback(TSCont cont, TSEvent event, void *edata); /** Resume callback*/
 };
 
 } /* atscppapi */
-
-#endif /* ATSCPPAPI_TRANSFORMATIONPLUGIN_H_ */
diff --git a/plugins/experimental/webp_transform/ImageTransform.cc b/plugins/experimental/webp_transform/ImageTransform.cc
index fe37f8c..967b4db 100644
--- a/plugins/experimental/webp_transform/ImageTransform.cc
+++ b/plugins/experimental/webp_transform/ImageTransform.cc
@@ -18,6 +18,7 @@
 
 #include <sstream>
 #include <iostream>
+#include <ts/string_view.h>
 #include <atscppapi/PluginInit.h>
 #include <atscppapi/GlobalPlugin.h>
 #include <atscppapi/TransformationPlugin.h>
@@ -29,10 +30,7 @@ using std::string;
 using namespace Magick;
 using namespace atscppapi;
 
-namespace
-{
 #define TAG "webp_transform"
-}
 
 class ImageTransform : public TransformationPlugin
 {
@@ -53,9 +51,9 @@ public:
   }
 
   void
-  consume(const string &data) override
+  consume(ts::string_view data) override
   {
-    _img.write(data.data(), data.size());
+    _img.write(data.data(), data.length());
   }
 
   void
@@ -69,8 +67,7 @@ public:
     Blob output_blob;
     image.magick("WEBP");
     image.write(&output_blob);
-    string output_data(reinterpret_cast<const char *>(output_blob.data()), output_blob.length());
-    produce(output_data);
+    produce(ts::string_view(reinterpret_cast<const char *>(output_blob.data()), output_blob.length()));
 
     setOutputComplete();
   }

-- 
To stop receiving notification emails like this one, please contact
jpeach@apache.org.

Mime
View raw message