Skip to content

Instantly share code, notes, and snippets.

View llllllllll's full-sized avatar

Joe Jevnik llllllllll

View GitHub Profile
@llllllllll
llllllllll / notes.rst
Last active July 23, 2021 05:41
msgspec notes

Initial Observations

The provided benchmark script is using Python's timeit module for benchmarking. I noticed that I was getting very different results between runs even with the same compiler, so I first switched to pyperf to attempt to get more stable results.

I am not sure if there is a Python API for pyperf, so I started by writing a small bash wrapper for benchmark:

Comments on optimizations around string concatenation.

Note: The code links are to CPython 3.8.5, the most recent release when this was written.

I was recently asked about a performance optimization in CPython around using += and + for string objects. As some people may already know, if you use += or + a string, it can sometimes be just as fast as ''.join. The question was to explain when that optimization couldn't be performed.

We will be going through the following example scenarios:

#include <cstdlib>
#include <compare>
#include <ranges>
namespace jj {
class iota : public std::ranges::view_base {
public:
iota(long start, long end) : m_start(start), m_end(end) {}
iota(long end) : iota(0, end) {}
iota() : iota(0, 0) {}
from collections import UserString
class ResolvedDocString(UserString):
__class__ = str
class DeferDocString(UserString):
def __init__(self, f):
self._f = f
class WithLazyDoc:
def __init__(self, get_doc, f):
self._get_doc = get_doc
self._f = f
@property
def __wrapped__(self):
return self._f
@property
""" Example:
In [5]: class Class:
...: def __init__(self, a, b):
...: self.a = a
...: self.b = b
...:
...: @self_scope
...: def f(self):
...: return a + b
In [1]: class Descr:
...: def __init__(self):
...: self.get_counter = 0
...:
...: def __get__(self, instance, owner):
...: if instance is None:
...: return self
...: self.get_counter += 1
...: return 1
...:
@llllllllll
llllllllll / lazy_talk_code.py
Created January 4, 2019 17:36
lazy-talk-code
"""
$ qipython -i ~/projects/tmp/lazy_talk_code.py
Python 3.4.6 (default, Feb 16 2017, 15:43:12)
Type "copyright", "credits" or "license" for more information.
IPython 5.3.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
import pandas as pd
import networkx
import toolz
df = pd.read_csv('/home/joe/downloads/debug.csv', escapechar='\\')
df.columns = ['stack', 'module', 'function', 'lineno', 'id', 'type', 'repr']
g = networkx.DiGraph()
"""Example usage:
In [1]: !cat ayy.s
.text
.global _start
.type _start, @function
_start:
movq $10, %rdx
pushw $10