Squirrel

The programming language
Welcome to Squirrel Sign in | Join | Help
in Search

Added Hashed String Literals

Last post 03-05-2008, 2:14 AM by David Walters. 3 replies.
Sort Posts: Previous Next
  •  03-04-2008, 8:19 AM 2392

    Added Hashed String Literals

    Hello, I really like Squirrel but recently I discovered the need for a little addition to the language: Compile time string hashing. I wanted to help out so I went ahead and implemented the feature.

    The patch can be found here, I created it using Beyond Compare so I'm not sure if it will work with GNU patch - but the modifications are quite restricted and to manual apply it wouldn't be too hard.

    When the patch is applied a new string prefix operator '#' is added such that during lexing the string following is passed to a user provided hashing function. The lexer then creates an integer token instead of a string using the value you've generated.

    You can now write Squirrel code like this and get.

       a <- #"WALK_ANIMATION"
       print ( a );

    > a = -347238472348  // or whatever you return



    The interface is very simple, you write a hash function with the signature

     (SQInteger)HASHFUNCTION( const SQChar* string );


     and bind it to the compiler using:

      sq_setcompilerhashfunction( SQVM, HASHFUNCTION );


    I've use this feature, as you might have guessed, to speed up runtime animation lookups. I hope this helps other people too.

    Regards,
    David Walters

  •  03-04-2008, 11:02 AM 2393 in reply to 2392

    Re: Added Hashed String Literals

    maybe using a string name in place of an operator # for the string hashing function....  the operator # often has different meanings in other script languages like Perl so Squirrel may want to reserve it for future use...
  •  03-04-2008, 8:53 PM 2394 in reply to 2393

    Re: Added Hashed String Literals

    squirrel strings already carry a precomputed hash value, wouldn't be the same to add a function like sq_gethash(v,idx) ?

    Alberto

  •  03-05-2008, 2:14 AM 2395 in reply to 2394

    Re: Added Hashed String Literals

    My first thought was 'oops' - I didn't realise this data was already stored for string literals. But as I think about it you do get a few small advantages from hashing in the lexer.

    * Reduced memory overhead (and possibly reduced dynamic allocations) for compiled code.
    * Obfuscation of strings in compiled code, which would help for scripted passwords.
    * Flexible and consistent hashing function, by specifying your own function you can use an algorithm that suits your application. ( Although if I were to improve this patch I would set the default compiler hash function to be the standard Squirrel one, having no default and an error case was a mistake. )

    I'm not sure if this patch is a candidate for inclusion in your language, but I hope it demonstrates a useful new feature that may be of benefit to someone.

    As atai points out the '#' prefix probably isn't the perfect choice for decoration due to conflicting usage in other languages ( it was easier to code detection for than say (hash)"FOO" ). Also I understand the overhead involved in updating the documentation and the apparent bloat which might go against your project aims.

    David

View as RSS news feed in XML
Powered by Community Server, by Telligent Systems