[Python-ideas] multiple objects handling

Peter V. Saveliev peet at altlinux.ru
Fri Aug 12 22:49:14 CEST 2011



I have a stream of objects, each of dictionary type. Actually, they
represent events and are emitted by an asynchronous thread.

Sample event:

	'type': 'neigh',
	'action': 'add',
	'index': 2,
	'dest': '',
	'lladdr': '0:2:b3:39:2e:4c',
	'timestamp': 'Wed Aug 10 17:20:28 2011',
	'probes': None,
	'cacheinfo': None

I shoud take an action depending on event's 'type' (neigh[bor], link,
address, route etc.) and 'action' (add, del) items.

Now I see two ways:
* build a tree of «if/elif» statements within one function
* create a dictionary that points to different methods

The issue is that a method call is slower that jump within one function.
But a dictionary lookup is faster than «if/elif» statements (if I have a
lot of variants and do not want to rebuild B-like tree of «if»
statements by hands).

Is there a way to combine these methods? Can I create a dictionary of
jump instructions?

So far I see that JUMP_ABSOLUTE and JUMP_FORWARD take an integer as a
parameter. It is reasonable, 'cause it is faster for if/elif/for etc.
statements. And thus I can not use a dictionary to store offsets and
then use them for one JUMP instruction.

But can we have another separate codeop, e.g. something like
JUMP_BY_TOS, that takes offset (or absolute address) from the top of the


Yes, I understand that it seems like `goto` statement.


Anyway, it can significantly speed up any stream parsing.



Peter V. Saveliev

More information about the Python-ideas mailing list