Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Trading compile time for faster runtime?

by cavac (Parson)
on Apr 22, 2022 at 08:51 UTC ( [id://11143194]=note: print w/replies, xml ) Need Help??


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";'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-03-29 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found