roxen.lists.pike.general

Subject Author Date
Arrays, multisets, mappings: avoiding unnecessary memory (re)assigns Arjan van Staalduijnen <Arjan[dot]van[dot]Staalduijnen[at]rtl[dot]nl> 07-09-2009
In various pieces of code I am bluntly doing frequent operations such
as:

	ids -= (< identifier >);
or
	ids |= ({ identifier });
or
	m_delete(ids, identifier);

without previously checking if 'identifier' is actually available in
'ids'. In most delete operations it is most-likely 'identifier' was
already removed earlier, while for the add operations 'identifier' in
most cases it is already an element of the ids variable.

Since operations on arrays, multisets and mappings are usually
destructive I am guessing my pieces of code might actually result in
various memory copy operations, even though the output has the same
value as the input.

Is my assumption correct, and would it be a good thing to check
has_index/has_value before adding or removing any identifier for the
above-mentioned examples, or does pike take care of that internally?

If pike handles the check internally, is there still a purpose to do a
has_value-check before adding an element, just to avoid the creating of
a temporary array (containing just the identifier value) for an
operation like:

	ids |= ({ identifier });

or would the penalty of the extra has_value cost more than it would
save?


Regards,

Arjan