| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent dfc2404 commit 56820c2
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,8 +1,12 @@ | |||
| 1 | 1 | #include "Python.h" | |
| 2 | 2 | #include "pyarena.h" | |
| 3 | 3 | ||
| 4 | - /* A simple arena block structure */ | ||
| 5 | - /* TODO(jhylton): Measurement to justify block size. */ | ||
| 4 | + /* A simple arena block structure | ||
| 5 | + | ||
| 6 | + Measurements with standard library modules suggest the average | ||
| 7 | + allocation is about 20 bytes and that most compiles use a single | ||
| 8 | + block. | ||
| 9 | + */ | ||
| 6 | 10 | ||
| 7 | 11 | #define DEFAULT_BLOCK_SIZE 8192 | |
| 8 | 12 | typedef struct _block { | |
@@ -21,6 +25,14 @@ struct _arena { | |||
| 21 | 25 | block *a_head; | |
| 22 | 26 | block *a_cur; | |
| 23 | 27 | PyObject *a_objects; | |
| 28 | + #if defined(Py_DEBUG) | ||
| 29 | + /* Debug output */ | ||
| 30 | + size_t total_allocs; | ||
| 31 | + size_t total_size; | ||
| 32 | + size_t total_blocks; | ||
| 33 | + size_t total_block_size; | ||
| 34 | + size_t total_big_blocks; | ||
| 35 | + #endif | ||
| 24 | 36 | }; | |
| 25 | 37 | ||
| 26 | 38 | static block * | |
@@ -86,19 +98,35 @@ PyArena_New() | |||
| 86 | 98 | free((void *)arena); | |
| 87 | 99 | return NULL; | |
| 88 | 100 | } | |
| 89 | - arena->a_objects = PyList_New(16); | ||
| 101 | + arena->a_objects = PyList_New(0); | ||
| 90 | 102 | if (!arena->a_objects) { | |
| 91 | 103 | block_free(arena->a_head); | |
| 92 | 104 | free((void *)arena); | |
| 93 | 105 | return NULL; | |
| 94 | 106 | } | |
| 107 | + #if defined(Py_DEBUG) | ||
| 108 | + arena->total_allocs = 0; | ||
| 109 | + arena->total_size = 0; | ||
| 110 | + arena->total_blocks = 1; | ||
| 111 | + arena->total_block_size = DEFAULT_BLOCK_SIZE; | ||
| 112 | + arena->total_big_blocks = 0; | ||
| 113 | + #endif | ||
| 95 | 114 | return arena; | |
| 96 | 115 | } | |
| 97 | 116 | ||
| 98 | 117 | void | |
| 99 | 118 | PyArena_Free(PyArena *arena) | |
| 100 | 119 | { | |
| 101 | 120 | assert(arena); | |
| 121 | + #if defined(Py_DEBUG) | ||
| 122 | + /* | ||
| 123 | + fprintf(stderr, | ||
| 124 | + "alloc=%d size=%d blocks=%d block_size=%d big=%d objects=%d\n", | ||
| 125 | + arena->total_allocs, arena->total_size, arena->total_blocks, | ||
| 126 | + arena->total_block_size, arena->total_big_blocks, | ||
| 127 | + PyList_Size(arena->a_objects)); | ||
| 128 | + */ | ||
| 129 | + #endif | ||
| 102 | 130 | block_free(arena->a_head); | |
| 103 | 131 | assert(arena->a_objects->ob_refcnt == 1); | |
| 104 | 132 | Py_DECREF(arena->a_objects); | |
@@ -111,9 +139,19 @@ PyArena_Malloc(PyArena *arena, size_t size) | |||
| 111 | 139 | void *p = block_alloc(arena->a_cur, size); | |
| 112 | 140 | if (!p) | |
| 113 | 141 | return NULL; | |
| 142 | + #if defined(Py_DEBUG) | ||
| 143 | + arena->total_allocs++; | ||
| 144 | + arena->total_size += size; | ||
| 145 | + #endif | ||
| 114 | 146 | /* Reset cur if we allocated a new block. */ | |
| 115 | 147 | if (arena->a_cur->ab_next) { | |
| 116 | 148 | arena->a_cur = arena->a_cur->ab_next; | |
| 149 | + #if defined(Py_DEBUG) | ||
| 150 | + arena->total_blocks++; | ||
| 151 | + arena->total_block_size += arena->a_cur->ab_size; | ||
| 152 | + if (arena->a_cur->ab_size > DEFAULT_BLOCK_SIZE) | ||
| 153 | + arena->total_big_blocks++; | ||
| 154 | + #endif | ||
| 117 | 155 | } | |
| 118 | 156 | return p; | |
| 119 | 157 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments