소스 검색

Minor optimizations in MvK code (only on CPython)

Yentl Van Tendeloo 8 년 전
부모
커밋
d33d188eb1
3개의 변경된 파일25개의 추가작업 그리고 37개의 파일을 삭제
  1. 4 4
      hybrid_server/classes/mvkcontroller.xml
  2. 19 32
      kernel/modelverse_kernel/request_handler.py
  3. 2 1
      state/modelverse_state/main.py

+ 4 - 4
hybrid_server/classes/mvkcontroller.xml

@@ -114,14 +114,14 @@
             <![CDATA[
             reply = None
             commands = []
+            mvk = self.mvk
+            mvs_operations = self.mvs_operations
             try:
                 while 1:
-                    commands = self.mvk.execute_yields(taskname, operation, params, reply)
+                    commands = mvk.execute_yields(taskname, operation, params, reply)
                     if commands is None:
                         break
-                    reply = [self.mvs_operations[command[0]](*(command[1]))[0] for command in commands]
-                    #for c, r in zip(commands, reply):
-                    #    print("%s --> %s" % (c, r))
+                    reply = [mvs_operations[command[0]](*(command[1]))[0] for command in commands]
             except:
                 print("ERROR: " + str(self.mvk.debug_info.get(taskname, "Unknown taskname")))
                 raise

+ 19 - 32
kernel/modelverse_kernel/request_handler.py

@@ -8,6 +8,8 @@ class KnownRequestHandled(Exception):
 
 class GeneratorStackEntry(object):
     """An entry in the generator stack of a request handles."""
+    __slots__ = ["generator", "function_name", "source_map", "function_origin", "pending_requests", "finished_requests", "replies"]
+
     def __init__(self, generator):
         self.generator = generator
         self.function_name = None
@@ -17,30 +19,8 @@ class GeneratorStackEntry(object):
         self.finished_requests = True
         self.replies = None
 
-    def append_reply(self, new_reply):
-        """Appends a reply to the this entry's list of pending replies."""
-        if self.replies is None:
-            self.replies = [new_reply]
-        else:
-            self.replies.append(new_reply)
-
-    def extend_replies(self, new_replies):
-        """Appends a list of replies to this entry's list of pending replies."""
-        if new_replies is not None:
-            if self.replies is None:
-                self.replies = new_replies
-            else:
-                self.replies.extend(new_replies)
-
-    def step(self):
-        """Performs a single step: accumulated replies are fed to the generator,
-           which then produces requests."""
-        # Send the replies to the generator, and ask for new requests.
-        self.pending_requests = self.generator.send(self.replies)
-
-        # Reset some data structures.
-        self.finished_requests = False
-        self.replies = None
+    def __getattr__(self, varname):
+        return None
 
 def format_stack_trace(stack_trace):
     """Formats a list of (function name, debug info, origin) triples."""
@@ -92,13 +72,21 @@ class RequestHandler(object):
             raise ValueError('handle_request cannot be called with an empty generator stack.')
 
         # Append the server's replies to the list of replies.
-        self.extend_replies(reply)
+        if reply is not None:
+            if self.generator_stack[-1].replies is None:
+                self.generator_stack[-1].replies = reply
+            else:
+                self.generator_stack[-1].replies.extend(reply)
+
         while 1:
             # Silence pylint's warning about catching Exception.
             # pylint: disable=I0011,W0703
             try:
                 while self.generator_stack[-1].finished_requests:
-                    self.generator_stack[-1].step()
+                    gen = self.generator_stack[-1]
+                    gen.pending_requests = gen.generator.send(gen.replies)
+                    gen.finished_requests = False
+                    gen.replies = None
                 else:
                     return self.pop_requests()
 
@@ -166,11 +154,10 @@ class RequestHandler(object):
 
     def append_reply(self, new_reply):
         """Appends a reply to the top-of-stack generator's list of pending replies."""
-        self.generator_stack[-1].append_reply(new_reply)
-
-    def extend_replies(self, new_replies):
-        """Appends a list of replies to the top-of-stack generator's list of pending replies."""
-        self.generator_stack[-1].extend_replies(new_replies)
+        if self.generator_stack[-1].replies is None:
+            self.generator_stack[-1].replies = [new_reply]
+        else:
+            self.generator_stack[-1].replies.append(new_reply)
 
     def handle_exception(self, exception):
         """Handles the given exception. A Boolean is returned that tells if
@@ -406,5 +393,5 @@ class RequestHandler(object):
         # Format: ("DEBUG_INFO", [function_name, source_map])
         top_entry = self.generator_stack[-1]
         top_entry.function_name, top_entry.source_map, top_entry.function_origin = request_args
-        top_entry.append_reply(None)
+        self.append_reply(None)
 

+ 2 - 1
state/modelverse_state/main.py

@@ -214,7 +214,7 @@ class ModelverseState(object):
 
     def read_edge(self, edge):
         if edge in self.edges:
-            return (list(self.edges[edge]), status.SUCCESS)
+            return ([self.edges[edge][0], self.edges[edge][1]], status.SUCCESS)
         else:
             return ([None, None], status.FAIL_RE_UNKNOWN)
 
@@ -247,6 +247,7 @@ class ModelverseState(object):
                 if e1 in self.outgoing:
                     for e2 in self.outgoing[e1]:
                         result.append(self.edges[e2][1])
+        #NOTE cannot just use the cache here, as some keys in the cache might not actually exist; we would have to check all of them anyway
         return (result, status.SUCCESS)
 
     def read_dict_edge(self, node, value):