Skip to content

Instantly share code, notes, and snippets.

View kjelly's full-sized avatar

Kuo-tung Kao kjelly

View GitHub Profile
@kjelly
kjelly / gist:7252623
Created October 31, 2013 16:24
python vm 對 += 的最佳化方法
static PyObject *
string_concatenate(PyObject *v, PyObject *w,
PyFrameObject *f, unsigned char *next_instr)
{
/* This function implements 'variable += expr' when both arguments
are strings. */
Py_ssize_t v_len = PyString_GET_SIZE(v);
Py_ssize_t w_len = PyString_GET_SIZE(w);
Py_ssize_t new_len = v_len + w_len;
if (new_len < 0) {
@kjelly
kjelly / gist:7496787
Created November 16, 2013 06:46
觀察與實驗
a='''
def test_join():
data = []
for i in range(1000000):
data.append('addddddeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeewweeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffffffffffffffffffffffffffffffffffeeeeeeeeeeeeeeeeeeeeeeeeeerwerwerwerwetrwegfsghklujsahdflkiuahfgluhewihrlwelfhlwuhfawhrgfiuhagrliwrwreqwerqwerwefsdfasdfasdfasdfdsfddddddddddddddddeeeeeeeewwwwwwwwwwwwwddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd')
return ''.join(data)
test_join()
'''
b = '''
# $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options change a
@kjelly
kjelly / gist:7974092
Created December 15, 2013 15:09
一般函數的捷徑,適用於那些參數沒 *args, 和 **kwargs 的函數 執行這類的函數比較快 詳見: Python 原碼解析 -- 深度探索動態語言核心技術 p223 或註解
/* The fast_function() function optimize calls for which no argument
tuple is necessary; the objects are passed directly from the stack.
For the simplest case -- a function that takes only positional
arguments and is called with only positional arguments -- it
inlines the most primitive frame setup code from
PyEval_EvalCodeEx(), which vastly reduces the checks that must be
done before evaluating the frame.
*/
static PyObject *
@kjelly
kjelly / gist:7987413
Created December 16, 2013 14:05
python 要存取區域變數,可以透過 frame.f_locals。然而,不代表要取得區域變數都是透過 dict。實際上,區域變數是放在 f_localsplus,然後透過 PyFrame_FastToLocals 將 f_localsplus 與 f_locals 建立起關聯。不使用 dict 是可以減少 dict 查詢的成本。
void
PyFrame_FastToLocals(PyFrameObject *f)
{
/* Merge fast locals into f->f_locals */
PyObject *locals, *map;
PyObject **fast;
PyObject *error_type, *error_value, *error_traceback;
PyCodeObject *co;
Py_ssize_t j;
int ncells, nfreevars;
@kjelly
kjelly / gist:7987698
Created December 16, 2013 14:24
python 在處理 closure 時,編譯時期就會偵測是否有使用 closure ,有的話,則會特別產生一些給 closure 用的 opcode。下面例子,會產生一些給 closure 用的 opcode。如果你把 print value 改成其他名稱,則不會產生 closure 的 opcode。
def get_func():
value = 's'
def test():
print value
return test
import dis
dis.dis(get_func)
@kjelly
kjelly / gist:8300078
Last active January 2, 2016 11:59
探討 python 的 __del__ 行為
import weakref
class Case1(object):
def __del__(self):
print 'del case1'
class Case2(object):
_instance = None
@kjelly
kjelly / gist:9579145
Created March 16, 2014 05:57
libseccomp example. use `gcc test_seccomp.c -lseccomp` to compile
#include <stdio.h>
#include <stdlib.h>
#include <seccomp.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int main()
{
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import *
import random
app = QApplication(sys.argv)
@kjelly
kjelly / gist:9584023
Created March 16, 2014 14:25
Python multi-inheritance , __init__ 。正常的 code
class A(object):
def __init__(self, parent=None):
super(A, self).__init__()
print 'init A class'
class B(object):
def __init__(self, parent=None):
super(B, self).__init__()
print 'init B class'