From 25f21d4824d57e77d43990b243b1a74ccbc18cfc Mon Sep 17 00:00:00 2001
From: Lidi Zheng <lidiz@google.com>
Date: Tue, 20 Aug 2019 09:13:22 -0700
Subject: [PATCH] Make default vtable for pointer argumnet a constant

---
 .../grpcio/grpc/_cython/_cygrpc/arguments.pxd.pxi     |  2 +-
 .../grpcio/grpc/_cython/_cygrpc/arguments.pyx.pxi     |  8 ++++----
 src/python/grpcio/grpc/_cython/_cygrpc/call.pyx.pxi   |  4 ++--
 .../grpcio/grpc/_cython/_cygrpc/channel.pxd.pxi       |  1 -
 .../grpcio/grpc/_cython/_cygrpc/channel.pyx.pxi       |  4 +---
 src/python/grpcio/grpc/_cython/_cygrpc/server.pxd.pxi |  1 -
 src/python/grpcio/grpc/_cython/_cygrpc/server.pyx.pxi |  5 ++---
 src/python/grpcio/grpc/_cython/_cygrpc/tag.pxd.pxi    |  1 -
 src/python/grpcio/grpc/_cython/_cygrpc/tag.pyx.pxi    |  5 ++---
 src/python/grpcio/grpc/_cython/_cygrpc/vtable.pxd.pxi |  5 +----
 src/python/grpcio/grpc/_cython/_cygrpc/vtable.pyx.pxi | 11 ++++-------
 11 files changed, 17 insertions(+), 30 deletions(-)

diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/arguments.pxd.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/arguments.pxd.pxi
index 9415b16344..251efe15b3 100644
--- a/src/python/grpcio/grpc/_cython/_cygrpc/arguments.pxd.pxi
+++ b/src/python/grpcio/grpc/_cython/_cygrpc/arguments.pxd.pxi
@@ -23,7 +23,7 @@ cdef class _ChannelArg:
 
   cdef grpc_arg c_argument
 
-  cdef void c(self, argument, _VTable vtable, references) except *
+  cdef void c(self, argument, references) except *
 
 
 cdef class _ChannelArgs:
diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/arguments.pyx.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/arguments.pyx.pxi
index 9211354b1c..e121ea6039 100644
--- a/src/python/grpcio/grpc/_cython/_cygrpc/arguments.pyx.pxi
+++ b/src/python/grpcio/grpc/_cython/_cygrpc/arguments.pyx.pxi
@@ -33,7 +33,7 @@ cdef grpc_arg _unwrap_grpc_arg(tuple wrapped_arg):
 
 cdef class _ChannelArg:
 
-  cdef void c(self, argument, _VTable vtable, references) except *:
+  cdef void c(self, argument, references) except *:
     key, value = argument
     cdef bytes encoded_key = _encode(key)
     if encoded_key is not key:
@@ -56,7 +56,7 @@ cdef class _ChannelArg:
       # lifecycle of the pointer is fixed to the lifecycle of the
       # python object wrapping it.
       self.c_argument.type = GRPC_ARG_POINTER
-      self.c_argument.value.pointer.vtable = &vtable.c_vtable
+      self.c_argument.value.pointer.vtable = &default_vtable
       self.c_argument.value.pointer.address = <void*>(<intptr_t>int(value))
     else:
       raise TypeError(
@@ -65,7 +65,7 @@ cdef class _ChannelArg:
 
 cdef class _ChannelArgs:
 
-  def __cinit__(self, arguments, _VTable vtable not None):
+  def __cinit__(self, arguments):
     self._arguments = () if arguments is None else tuple(arguments)
     self._channel_args = []
     self._references = []
@@ -75,7 +75,7 @@ cdef class _ChannelArgs:
           self._c_arguments.arguments_length * sizeof(grpc_arg))
       for index, argument in enumerate(self._arguments):
         channel_arg = _ChannelArg()
-        channel_arg.c(argument, vtable, self._references)
+        channel_arg.c(argument, self._references)
         self._c_arguments.arguments[index] = channel_arg.c_argument
         self._channel_args.append(channel_arg)
 
diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/call.pyx.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/call.pyx.pxi
index 84934db4d6..6e4574af8d 100644
--- a/src/python/grpcio/grpc/_cython/_cygrpc/call.pyx.pxi
+++ b/src/python/grpcio/grpc/_cython/_cygrpc/call.pyx.pxi
@@ -17,11 +17,11 @@ cimport cpython
 
 cdef class Call:
 
-  def __cinit__(self, _VTable vtable not None):
+  def __cinit__(self):
     # Create an *empty* call
     fork_handlers_and_grpc_init()
     self.c_call = NULL
-    self.references = [vtable]
+    self.references = []
 
   def _start_batch(self, operations, tag, retain_self):
     if not self.is_valid:
diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/channel.pxd.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/channel.pxd.pxi
index 13c0c02ab2..eb27f2df7a 100644
--- a/src/python/grpcio/grpc/_cython/_cygrpc/channel.pxd.pxi
+++ b/src/python/grpcio/grpc/_cython/_cygrpc/channel.pxd.pxi
@@ -69,7 +69,6 @@ cdef class SegregatedCall:
 cdef class Channel:
 
   cdef _ChannelState _state
-  cdef _VTable _vtable
 
   # TODO(https://github.com/grpc/grpc/issues/15662): Eliminate this.
   cdef tuple _arguments
diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/channel.pyx.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/channel.pyx.pxi
index 1799780fce..70bc8dbed7 100644
--- a/src/python/grpcio/grpc/_cython/_cygrpc/channel.pyx.pxi
+++ b/src/python/grpcio/grpc/_cython/_cygrpc/channel.pyx.pxi
@@ -455,9 +455,7 @@ cdef class Channel:
     self._state.c_connectivity_completion_queue = (
         grpc_completion_queue_create_for_next(NULL))
     self._arguments = arguments
-    self._vtable = _VTable()
-    cdef _ChannelArgs channel_args = _ChannelArgs(
-        arguments, self._vtable)
+    cdef _ChannelArgs channel_args = _ChannelArgs(arguments)
     if channel_credentials is None:
       self._state.c_channel = grpc_insecure_channel_create(
           <char *>target, channel_args.c_args(), NULL)
diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/server.pxd.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/server.pxd.pxi
index b3fadcdc62..b89ed99d97 100644
--- a/src/python/grpcio/grpc/_cython/_cygrpc/server.pxd.pxi
+++ b/src/python/grpcio/grpc/_cython/_cygrpc/server.pxd.pxi
@@ -16,7 +16,6 @@ cdef class Server:
 
   cdef grpc_server *c_server
 
-  cdef _VTable _vtable
   cdef bint is_started  # start has been called
   cdef bint is_shutting_down  # shutdown has been called
   cdef bint is_shutdown  # notification of complete shutdown received
diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/server.pyx.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/server.pyx.pxi
index 2369371cab..67b2e9d4e8 100644
--- a/src/python/grpcio/grpc/_cython/_cygrpc/server.pyx.pxi
+++ b/src/python/grpcio/grpc/_cython/_cygrpc/server.pyx.pxi
@@ -31,8 +31,7 @@ cdef class Server:
     self.is_shutting_down = False
     self.is_shutdown = False
     self.c_server = NULL
-    self._vtable = _VTable()
-    cdef _ChannelArgs channel_args = _ChannelArgs(arguments, self._vtable)
+    cdef _ChannelArgs channel_args = _ChannelArgs(arguments)
     self.c_server = grpc_server_create(channel_args.c_args(), NULL)
     self.references.append(arguments)
 
@@ -43,7 +42,7 @@ cdef class Server:
       raise ValueError("server must be started and not shutting down")
     if server_queue not in self.registered_completion_queues:
       raise ValueError("server_queue must be a registered completion queue")
-    cdef _RequestCallTag request_call_tag = _RequestCallTag(tag, self._vtable)
+    cdef _RequestCallTag request_call_tag = _RequestCallTag(tag)
     request_call_tag.prepare()
     cpython.Py_INCREF(request_call_tag)
     return grpc_server_request_call(
diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/tag.pxd.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/tag.pxd.pxi
index c77beb2819..d8ba1ea9bd 100644
--- a/src/python/grpcio/grpc/_cython/_cygrpc/tag.pxd.pxi
+++ b/src/python/grpcio/grpc/_cython/_cygrpc/tag.pxd.pxi
@@ -29,7 +29,6 @@ cdef class _RequestCallTag(_Tag):
 
   cdef readonly object _user_tag
   cdef Call call
-  cdef _VTable _vtable
   cdef CallDetails call_details
   cdef grpc_metadata_array c_invocation_metadata
 
diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/tag.pyx.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/tag.pyx.pxi
index d1280ef494..e80dc88767 100644
--- a/src/python/grpcio/grpc/_cython/_cygrpc/tag.pyx.pxi
+++ b/src/python/grpcio/grpc/_cython/_cygrpc/tag.pyx.pxi
@@ -30,14 +30,13 @@ cdef class _ConnectivityTag(_Tag):
 
 cdef class _RequestCallTag(_Tag):
 
-  def __cinit__(self, user_tag, _VTable vtable not None):
+  def __cinit__(self, user_tag):
     self._user_tag = user_tag
     self.call = None
     self.call_details = None
-    self._vtable = vtable
 
   cdef void prepare(self) except *:
-    self.call = Call(self._vtable)
+    self.call = Call()
     self.call_details = CallDetails()
     grpc_metadata_array_init(&self.c_invocation_metadata)
 
diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/vtable.pxd.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/vtable.pxd.pxi
index 1799b6e1f1..c96e5cb669 100644
--- a/src/python/grpcio/grpc/_cython/_cygrpc/vtable.pxd.pxi
+++ b/src/python/grpcio/grpc/_cython/_cygrpc/vtable.pxd.pxi
@@ -15,12 +15,9 @@
 
 cdef void* _copy_pointer(void* pointer)
 
-
 cdef void _destroy_pointer(void* pointer)
 
-
 cdef int _compare_pointer(void* first_pointer, void* second_pointer)
 
 
-cdef class _VTable:
-  cdef grpc_arg_pointer_vtable c_vtable
+cdef grpc_arg_pointer_vtable default_vtable
diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/vtable.pyx.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/vtable.pyx.pxi
index 98cb60c10e..da4b81bd97 100644
--- a/src/python/grpcio/grpc/_cython/_cygrpc/vtable.pyx.pxi
+++ b/src/python/grpcio/grpc/_cython/_cygrpc/vtable.pyx.pxi
@@ -30,10 +30,7 @@ cdef int _compare_pointer(void* first_pointer, void* second_pointer):
   else:
     return 0
 
-
-cdef class _VTable:
-  def __cinit__(self):
-    self.c_vtable.copy = &_copy_pointer
-    self.c_vtable.destroy = &_destroy_pointer
-    self.c_vtable.cmp = &_compare_pointer
-
+cdef grpc_arg_pointer_vtable default_vtable
+default_vtable.copy = &_copy_pointer
+default_vtable.destroy = &_destroy_pointer
+default_vtable.cmp = &_compare_pointer
-- 
GitLab