mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 21:51:50 +00:00 
			
		
		
		
	Speed-up dictionary constructor by about 10%.
New opcode, STORE_MAP saves the compiler from awkward stack manipulations
and specializes for dicts using PyDict_SetItem instead of PyObject_SetItem.
Old disassembly:
              0 BUILD_MAP                0
              3 DUP_TOP
              4 LOAD_CONST               1 (1)
              7 ROT_TWO
              8 LOAD_CONST               2 ('x')
             11 STORE_SUBSCR
             12 DUP_TOP
             13 LOAD_CONST               3 (2)
             16 ROT_TWO
             17 LOAD_CONST               4 ('y')
             20 STORE_SUBSCR
New disassembly:
              0 BUILD_MAP                0
              3 LOAD_CONST               1 (1)
              6 LOAD_CONST               2 ('x')
              9 STORE_MAP
             10 LOAD_CONST               3 (2)
             13 LOAD_CONST               4 ('y')
             16 STORE_MAP
			
			
This commit is contained in:
		
							parent
							
								
									eb103b3577
								
							
						
					
					
						commit
						effde12f5f
					
				
					 5 changed files with 19 additions and 5 deletions
				
			
		|  | @ -45,6 +45,7 @@ extern "C" { | ||||||
| #define DELETE_SLICE	50 | #define DELETE_SLICE	50 | ||||||
| /* Also uses 51-53 */ | /* Also uses 51-53 */ | ||||||
| 
 | 
 | ||||||
|  | #define STORE_MAP	54 | ||||||
| #define INPLACE_ADD	55 | #define INPLACE_ADD	55 | ||||||
| #define INPLACE_SUBTRACT	56 | #define INPLACE_SUBTRACT	56 | ||||||
| #define INPLACE_MULTIPLY	57 | #define INPLACE_MULTIPLY	57 | ||||||
|  |  | ||||||
|  | @ -85,6 +85,7 @@ def jabs_op(name, op): | ||||||
| def_op('DELETE_SLICE+2', 52) | def_op('DELETE_SLICE+2', 52) | ||||||
| def_op('DELETE_SLICE+3', 53) | def_op('DELETE_SLICE+3', 53) | ||||||
| 
 | 
 | ||||||
|  | def_op('STORE_MAP', 54) | ||||||
| def_op('INPLACE_ADD', 55) | def_op('INPLACE_ADD', 55) | ||||||
| def_op('INPLACE_SUBTRACT', 56) | def_op('INPLACE_SUBTRACT', 56) | ||||||
| def_op('INPLACE_MULTIPLY', 57) | def_op('INPLACE_MULTIPLY', 57) | ||||||
|  |  | ||||||
|  | @ -2002,6 +2002,18 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) | ||||||
| 			if (x != NULL) continue; | 			if (x != NULL) continue; | ||||||
| 			break; | 			break; | ||||||
| 
 | 
 | ||||||
|  | 		case STORE_MAP: | ||||||
|  | 			w = TOP();     /* key */ | ||||||
|  | 			u = SECOND();  /* value */ | ||||||
|  | 			v = THIRD();   /* dict */ | ||||||
|  | 			STACKADJ(-2); | ||||||
|  | 			assert (PyDict_CheckExact(v)); | ||||||
|  | 			err = PyDict_SetItem(v, w, u);  /* v[w] = u */ | ||||||
|  | 			Py_DECREF(u); | ||||||
|  | 			Py_DECREF(w); | ||||||
|  | 			if (err == 0) continue; | ||||||
|  | 			break; | ||||||
|  | 
 | ||||||
| 		case LOAD_ATTR: | 		case LOAD_ATTR: | ||||||
| 			w = GETITEM(names, oparg); | 			w = GETITEM(names, oparg); | ||||||
| 			v = TOP(); | 			v = TOP(); | ||||||
|  |  | ||||||
|  | @ -729,6 +729,8 @@ opcode_stack_effect(int opcode, int oparg) | ||||||
| 			return -1; | 			return -1; | ||||||
| 		case STORE_SUBSCR: | 		case STORE_SUBSCR: | ||||||
| 			return -3; | 			return -3; | ||||||
|  | 		case STORE_MAP: | ||||||
|  | 			return -2; | ||||||
| 		case DELETE_SUBSCR: | 		case DELETE_SUBSCR: | ||||||
| 			return -2; | 			return -2; | ||||||
| 
 | 
 | ||||||
|  | @ -2926,13 +2928,11 @@ compiler_visit_expr(struct compiler *c, expr_ty e) | ||||||
| 		/* We must arrange things just right for STORE_SUBSCR.
 | 		/* We must arrange things just right for STORE_SUBSCR.
 | ||||||
| 		   It wants the stack to look like (value) (dict) (key) */ | 		   It wants the stack to look like (value) (dict) (key) */ | ||||||
| 		for (i = 0; i < n; i++) { | 		for (i = 0; i < n; i++) { | ||||||
| 			ADDOP(c, DUP_TOP); |  | ||||||
| 			VISIT(c, expr,  | 			VISIT(c, expr,  | ||||||
| 				(expr_ty)asdl_seq_GET(e->v.Dict.values, i)); | 				(expr_ty)asdl_seq_GET(e->v.Dict.values, i)); | ||||||
| 			ADDOP(c, ROT_TWO); |  | ||||||
| 			VISIT(c, expr,  | 			VISIT(c, expr,  | ||||||
| 				(expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); | 				(expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); | ||||||
| 			ADDOP(c, STORE_SUBSCR); | 			ADDOP(c, STORE_MAP); | ||||||
| 		} | 		} | ||||||
| 		break; | 		break; | ||||||
| 	case ListComp_kind: | 	case ListComp_kind: | ||||||
|  |  | ||||||
|  | @ -66,10 +66,10 @@ extern time_t PyOS_GetLastModificationTime(char *, FILE *); | ||||||
|        Python 2.5c1: 62121 (fix wrong lnotab with for loops and |        Python 2.5c1: 62121 (fix wrong lnotab with for loops and | ||||||
|        			    storing constants that should have been removed) |        			    storing constants that should have been removed) | ||||||
|        Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp) |        Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp) | ||||||
|        Python 2.6a0: 62141 (peephole optimizations) |        Python 2.6a0: 62151 (peephole optimizations and STORE_MAP opcode) | ||||||
| . | . | ||||||
| */ | */ | ||||||
| #define MAGIC (62141 | ((long)'\r'<<16) | ((long)'\n'<<24)) | #define MAGIC (62151 | ((long)'\r'<<16) | ((long)'\n'<<24)) | ||||||
| 
 | 
 | ||||||
| /* Magic word as global; note that _PyImport_Init() can change the
 | /* Magic word as global; note that _PyImport_Init() can change the
 | ||||||
|    value of this global to accommodate for alterations of how the |    value of this global to accommodate for alterations of how the | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Raymond Hettinger
						Raymond Hettinger