Skip to content

Commit 3460ce5

Browse files
committed
Update docstrings and comments
1 parent c2a823d commit 3460ce5

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

mypyc/codegen/emitclass.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,18 @@ def generate_class_type_decl(
189189
def generate_class_reuse(
190190
cl: ClassIR, c_emitter: Emitter, external_emitter: Emitter, emitter: Emitter
191191
) -> None:
192+
"""Generate a definition of a single-object per-class free "list".
193+
194+
This speeds up object allocation and freeing when there are many short-lived
195+
objects.
196+
197+
TODO: Generalize to support a free list with up to N objects.
198+
"""
192199
assert cl.reuse_freed_instance
200+
201+
# The free list implementation doesn't support class hierarchies
202+
assert cl.is_final_class or cl.children == []
203+
193204
context = c_emitter.context
194205
name = cl.name_prefix(c_emitter.names) + "_free_instance"
195206
struct_name = cl.struct_name(c_emitter.names)
@@ -838,10 +849,16 @@ def generate_dealloc_for_class(
838849

839850

840851
def emit_reuse_dealloc(cl: ClassIR, emitter: Emitter) -> None:
852+
"""Emit code to deallocate object by putting it to per-type free list.
853+
854+
The free "list" currently can have up to one object.
855+
"""
841856
prefix = cl.name_prefix(emitter.names)
842857
emitter.emit_line(f"if ({prefix}_free_instance == NULL) {{")
843858
emitter.emit_line(f"{prefix}_free_instance = self;")
844859

860+
# Clear attributes and free referenced objects.
861+
845862
emit_clear_bitmaps(cl, emitter)
846863

847864
for base in reversed(cl.base_mro):

mypyc/ir/class_ir.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ def __init__(
206206

207207
# If True, keep one freed, cleared instance available for immediate reuse to
208208
# speed up allocations. This helps if many objects are freed quickly, before
209-
# other instances of the same class are allocated.
209+
# other instances of the same class are allocated. This is effectively a
210+
# per-type free "list" of up to length 1.
210211
self.reuse_freed_instance = False
211212

212213
def __repr__(self) -> str:

mypyc/lib-rt/mypyc_util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
#elif defined(_MSC_VER)
3939
#define CPyThreadLocal __declspec(thread)
4040

41-
// 3. GNU thread local storage for GCC/Clang targets that still need it
41+
// 3. GNU thread local storage for GCC/Clang targets that still need it
4242
#elif defined(__GNUC__) || defined(__clang__)
4343
#define CPyThreadLocal __thread
4444

0 commit comments

Comments
 (0)