Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hash-NoShareKeys

Hash::NoShareKeys

Provide a function Hash::NoShareKeys::apply to disable sharing of hash keys on the specified hash (reference).

See test.pl for a usage example.

Memory consumption on a single hash with one million keys (tested on a Linux Mint 18 system with self-compiled perl 5.28.0, unthreaded):

  • with key sharing: size=80666224
  • without key sharing: size=56666224

Performance:

  • with key sharing: ~0.42s
  • without key sharing: ~0.36s
/MYMETA.json
/MYMETA.yml
/Makefile
/NoShareKeys.bs
/NoShareKeys.c
/NoShareKeys.o
/blib
/pm_to_blib
use strict;
use ExtUtils::MakeMaker;
WriteMakefile
(
'NAME' => 'Hash::NoShareKeys',
'VERSION_FROM' => 'NoShareKeys.pm',
);
package Hash::NoShareKeys;
our $VERSION = '0.01';
require XSLoader;
XSLoader::load();
1;
__END__
/* -*- c-basic-offset:2 -*- */
#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#ifdef __cplusplus
}
#endif
MODULE = Hash::NoShareKeys PACKAGE = Hash::NoShareKeys
PROTOTYPES: DISABLE
void
apply(HV* hv)
CODE:
HvSHAREKEYS_off(hv);
#!/usr/bin/perl -w
# -*- cperl -*-
#
# Author: Slaven Rezic
#
use strict;
use Test::More 'no_plan';
use Hash::NoShareKeys;
use Devel::Peek;
use Devel::Size 'size';
use Time::HiRes 'time';
#use blib "../Devel-PLstrtab/";
#use Devel::PLstrtab;
for my $apply (0, 1) {
my %h;
Dump \%h;
if ($apply) {
Hash::NoShareKeys::apply(\%h);
}
my $t0 = time;
for (1..1000000) { $h{$_} = undef }
my $t1 = time;
diag "duration=" . ($t1-$t0);
Dump \%h;
#diag scalar %h;
diag "size=" . size \%h;
#Devel::PLstrtab::dumpToStderr();
}
pass "yes";
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.