http://qs321.pair.com?node_id=11143194


in reply to Trading compile time for faster runtime?

I don't know anything about your exact requirements and what your scripts do. But i have a feeling you are doing a lot of data munching. I assume you tried putting your data in a database like PostgreSQL and implementing the time critical parts in SQL?

Example: For a year now i had some performance problems on my DNS server written in Perl. It had to do with white/blacklisting of domains. Basically, it had to do about a million string matches for every request, plus a few thousand regexp matches. I moved the whole matching algorithm into PostgreSQL. It isn't all that optimized yet, but it runs in a fraction of the time:

CREATE OR REPLACE FUNCTION pagecamel.nameserver_isforcenx(search_domai +n_name text) RETURNS boolean LANGUAGE plpgsql AS $function$ DECLARE tempvar boolean := false; BEGIN -- Check whitelist (non-regex) SELECT INTO tempvar EXISTS(SELECT 1 FROM pagecamel.nameserver_forc +enxdomain_whitelist WHERE is_regex = false AND search_domain_name = doma +in_match); IF tempvar = true THEN -- whitelisted RETURN FALSE; END IF; -- Check whitelist (regex) SELECT INTO tempvar EXISTS(SELECT 1 FROM pagecamel.nameserver_forc +enxdomain_whitelist WHERE is_regex = true AND search_domain_name ~* doma +in_match); IF tempvar = true THEN -- whitelisted RETURN FALSE; END IF; -- Check blacklist (non-regex) SELECT INTO tempvar EXISTS(SELECT 1 FROM pagecamel.nameserver_forc +enxdomain WHERE is_regex = false AND search_domain_name = doma +in_match); IF tempvar = true THEN -- blacklisted RETURN TRUE; END IF; -- Check blacklist (regex) SELECT INTO tempvar EXISTS(SELECT 1 FROM pagecamel.nameserver_forc +enxdomain WHERE is_regex = true AND search_domain_name ~* doma +in_match); IF tempvar = true THEN -- blacklisted RETURN TRUE; END IF; -- Neither, so NOT blacklisted RETURN false; END; $function$;

Perl is quite good in general. But when it comes to handling large amounts of data, no "normal" scripting language comes even close to a modern SQL database engine like PostgreSQL. People on those projects spent the last few decades optimizing every last tenth of a percent of performance.

Yeah, there is probably a way to optimize that function using the WITH() clause and/or having some special type of INDEX on the table that's somehow optimized to handle regular expressions or something.

perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'