Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Re^3: A memory efficient hash, trading off speed - does it already exist?

by JPaul (Hermit)
on Feb 06, 2003 at 17:57 UTC ( [id://233208]=note: print w/replies, xml ) Need Help??


in reply to Re^3: A memory efficient hash, trading off speed - does it already exist?
in thread A memory efficient hash, trading off speed - does it already exist?

This requires I know what the following symbol is.
The point of the hash is to look up potential following symbols of the keyword, unless there's someway to glob hash keys without going through them to compile a list, it's not going to work at all.

JP,
-- Alexander Widdlemouse undid his bellybutton and his bum dropped off --

  • Comment on Re: Re^3: A memory efficient hash, trading off speed - does it already exist?

Replies are listed 'Best First'.
Re^5: A memory efficient hash, trading off speed - does it already exist?
by Aristotle (Chancellor) on Feb 07, 2003 at 12:57 UTC
    Then keep a key with a list of symbols.
    $hash{"Keyword"} = "Symbol_1\0Symbol_2\0Symbol_3"; my $key = "Keyword"; for my $symbol (split /\0/, $hash{$key}, -1) { my $weight = $hash{"$key\0$symbol"}; # ... }

    Makeshifts last the longest.

      So where is the advantage of that over what I chose to use?
      $hash{"Keyword"} = "Symbol_1 \x255 5 \O Symbol_2 \x255 4";
      I use a single hash (As opposed to two - the lookup hash and the one with values), and when I'm doing accesses I throw it into a temporary hash for quicker lookups on multiple hits to the same keyword.

      JP,
      -- Alexander Widdlemouse undid his bellybutton and his bum dropped off --

        Good question.

        My proposal is a single hash only too though. You can store the keyword -> symbollist and the keyword+symbol -> weighting entries in the same hash, provided your keywords never have a \0, but in that case the keyword+symbol or symol+weight packing will break as well anyway.

        Obviously though, if you split the symbol string anyway, interleaving the weights into the same string comes at very little extra cost. In that case, my proposal doesn't really have a lot of merrit.

        It will shine when you frequently know the symbol you're looking for beforhand and can thus skip the splitting step - then you can look up the weight directly. Looking up the same symbols frequently actually would be an argument in favour of this approach, as its key/value pairs constitute a much smaller amount of data each, allowing the OS and/or DBM drives to keep a lot more individual entries in their respective caches. It will still take up considerably more total memory than your approach (infrastructure for one extra key per symbol, plus the size of an extra copy of the keyword and symbol to store as the key) - but in the case of infrequent accesses to the symbol list key it actually works in favour of caching.

        Another option to consider if you need to discover possible values might be BerkeleyDB's B-tree mode, which lets you store multiple values with identical keys and then query them in sequence. It's usually more expensive and less efficient than hash mode though, so you might do well to do something like use 53 DBMs keyed on the first character of the keyword (one for each letter, case sensitive, and one for non-letter characters) to split up the expense among individually more manageable data sets.

        Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://233208]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-16 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found