Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Key question

7 views
Skip to first unread message

Simon Glover

unread,
Jan 5, 2005, 8:56:48 PM1/5/05
to perl6-i...@perl.org, Dan Sugalski

Or rather, a question about keys: what should the following two code
snippets do?

1) new P0, .Key
set P0, "1"
set N0, P0
print N0
end

2) new P0, .Key
set P0, "1"
set I0, P0
print I0
end

At the moment, the first one throws an exception ('Key not a number!'),
while the second one goes into an infinite loop and eventually
segfaults. As I see it, there are three possibilities:

i) In both cases, we throw an exception.
ii) We try to convert the string to an float (or int) using
string_to_num (or string_to_int). [In which case, what
do we do if the string is something like "asdf"?]
iii) We declare that trying to get numeric information out of
non-numeric keys is undefined behaviour, and so Parrot may do
anything (including segfault).

Which of these should Parrot do?

Simon

Leopold Toetsch

unread,
Jan 6, 2005, 3:37:59 AM1/6/05
to Simon Glover, perl6-i...@perl.org
Simon Glover <sc...@amnh.org> wrote:

It's basically ii) C<key_integer> is missing the case of KEY_string_FLAG
so that recursively C<get_integer> is called on that key.

But we should generalize keys eventually. Keys can provide an index for
aggregates and allow chaining of indices for nested aggregates. Arrays
are simple: the key is an integer. But hashes currently don't support
non-string keys easily. We should be able to use arbitrary PMCs as
hash keys. The way to go here is to use the VTABLE_hash method to
generate an index aka hash value usable for accessing hashes.

A Key PMC can directly contain one of the Parrot native types I,N,S or
point to a PMC [1].
- the hashval of an Integer is the value
- the hashval of a Float should follow Python semantics. At least
hash(x.0) := hash(x) := x should be given.
- strings with a numerical value should probably hash to that number
- for PMCs the hash function should do the right thing. I.e. if the
PMC is a scalar one of the above rules should be used.

[1] This is currently broken.

> Simon

leo

Sam Ruby

unread,
Jan 6, 2005, 7:39:30 AM1/6/05
to l...@toetsch.at, perl6-i...@perl.org
Leopold Toetsch wrote:
>
> But we should generalize keys eventually. Keys can provide an index for
> aggregates and allow chaining of indices for nested aggregates. Arrays
> are simple: the key is an integer. But hashes currently don't support
> non-string keys easily. We should be able to use arbitrary PMCs as
> hash keys. The way to go here is to use the VTABLE_hash method to
> generate an index aka hash value usable for accessing hashes.

See: http://xrl.us/emnk

Except for fromkeys, get_string, and __new__, the logic is not Python
specific, and could easily be refactored into a common base class for
others to use.

- Sam Ruby

Leopold Toetsch

unread,
Jan 7, 2005, 4:34:02 AM1/7/05
to Sam Ruby, perl6-i...@perl.org
Sam Ruby <ru...@intertwingly.net> wrote:

> See: http://xrl.us/emnk

=> dynclasses/pydic.pmc

> Except for fromkeys, get_string, and __new__, the logic is not Python
> specific, and could easily be refactored into a common base class for
> others to use.

Yep. The problem is that all current usage of hashes inside Parrot is
utilizing a STRING* key. OTOH HLLs are using PMCs in the normal case.

I've already proposed to get rid of the STRING* type. It's neither
low-level (there is no hardware CPU support for it like there is for
FLOATVAL) nor does a separate STRING type have any performance benefits.
Just the opposite - it leads to code duplication due to STRING and PMC
variants of opcodes and vtables.

sizeof(STRING) is 40 bytes (32-bit, optimized w/o C<version>). That's
exactly double the size of a PMC. Thus STRINGs are really big
structures. Wrapping them into a PMC for HLLs usage makes things just
worse.

By unifying STRING and PMC structures the hashing problem would just vanish.

> - Sam Ruby

leo

0 new messages