Commits

Alex Gaynor committed c30cb1d

Unroll dict(key=value) when the set of keys is constant

Comments (0)

Files changed (1)

pypy/objspace/std/dictmultiobject.py

 init_signature = Signature(['seq_or_map'], None, 'kwargs')
 init_defaults = [None]
 
+
 def update1(space, w_dict, w_data):
     if space.findattr(w_data, space.wrap("keys")) is None:
         # no 'keys' method, so we assume it is a sequence of pairs
-        for w_pair in space.listview(w_data):
-            pair = space.fixedview(w_pair)
-            if len(pair) != 2:
-                raise OperationError(space.w_ValueError,
-                             space.wrap("sequence of pairs expected"))
-            w_key, w_value = pair
-            w_dict.setitem(w_key, w_value)
+        update1_pairs(space, w_dict, w_data)
     else:
         if isinstance(w_data, W_DictMultiObject):    # optimization case only
             update1_dict_dict(space, w_dict, w_data)
         else:
             # general case -- "for k in o.keys(): dict.__setitem__(d, k, o[k])"
-            w_keys = space.call_method(w_data, "keys")
-            for w_key in space.listview(w_keys):
-                w_value = space.getitem(w_data, w_key)
-                w_dict.setitem(w_key, w_value)
+            update1_keys(space, w_dict, w_data)
 
+
+@jit.look_inside_iff(lambda space, w_dict, w_data: w_dict_unrolling_heuristic(w_data))
 def update1_dict_dict(space, w_dict, w_data):
     iterator = w_data.iteritems()
     while 1:
             break
         w_dict.setitem(w_key, w_value)
 
+
+def update1_pairs(space, w_dict, w_data):
+    for w_pair in space.listview(w_data):
+        pair = space.fixedview(w_pair)
+        if len(pair) != 2:
+            raise OperationError(space.w_ValueError,
+                         space.wrap("sequence of pairs expected"))
+        w_key, w_value = pair
+        w_dict.setitem(w_key, w_value)
+
+
+def update1_keys(space, w_dict, w_data):
+    w_keys = space.call_method(w_data, "keys")
+    for w_key in space.listview(w_keys):
+        w_value = space.getitem(w_data, w_key)
+        w_dict.setitem(w_key, w_value)
+
+
 def init_or_update(space, w_dict, __args__, funcname):
     w_src, w_kwds = __args__.parse_obj(
             None, funcname,
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.