Skip to content

Commit

Permalink
Removed Positional role from BSON::Document
Browse files Browse the repository at this point in the history
  • Loading branch information
MARTIMM committed Feb 25, 2017
1 parent 1ce77c7 commit af5ef67
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 290 deletions.
2 changes: 1 addition & 1 deletion META.info
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
},
"source-url": "git://github.com/MARTIMM/BSON.git",
"perl": "6.c",
"version": "0.9.31"
"version": "0.9.32"
}
17 changes: 13 additions & 4 deletions benchmarks/bench-readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ Timing 50 iterations of 32 inserts... (* is current BSON::Document use)
D9 Bugfixes and improvements
D10 Native encoding/decoding for doubles
D11 version 2016.06-178-gf7c6e60 built on MoarVM version 2016.06-9-g8fc21d5
D12 2016-11-8, 2016.10-204-g824b29f built on MoarVM version 2016.10-37-gf769569
D12 2016-11-08, 2016.10-204-g824b29f built on MoarVM version 2016.10-37-gf769569
D13 2017-02-25. 017.02-56-g9f10434 built on MoarVM version 2017.02-7-g3d85900
D14 2017-02-25. Dropped positional role from BSON::Document..

H Original BSON methods with hashes


D1 8.0726 wallclock secs @ 6.1938/s (n=50)
Expand All @@ -28,8 +29,16 @@ Timing 50 iterations of 32 inserts... (* is current BSON::Document use)
D8 10.0837 wallclock secs @ 4.9585/s (n=50) Doen't help much
D9 7.8202 wallclock secs @ 6.3937/s (n=50) Perl 2015 12 24
D10 6.4880 wallclock secs @ 7.7066/s (n=50) again a bit better
D11* 2.7751 wallclock secs @ 18.0171/s (n=50) big improvement
D12* 2.5247 wallclock secs @ 19.8041/s (n=50)
D11 2.7751 wallclock secs @ 18.0171/s (n=50) big improvement
D12 2.5247 wallclock secs @ 19.8041/s (n=50) +
D13 2.3827 wallclock secs @ 20.9844/s (n=50) +
D14* 2.3011 wallclock secs @ 21.7285/s (n=50) +




H Original BSON methods with hashes. I think this was about 2015 06 or so.
In the mean time Hashing should be faster too

H 3.1644 wallclock secs @ 15.8006/s (n=50)

Expand Down
2 changes: 2 additions & 0 deletions doc/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ See [semantic versioning](http://semver.org/). Please note point 4. on
that page: *Major version zero (0.y.z) is for initial development. Anything may
change at any time. The public API should not be considered stable*.

* 0.9.32
* Removed Positional role from BSON::Document. This means no $d[$i] anymore. There was no use for it. Less code, less parsing and less object building time.
* 0.9.31
* Added some tests in Document to check for proper pair data.
* 0.9.30
Expand Down
64 changes: 4 additions & 60 deletions lib/BSON/Document.pm6
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use v6.c;

# There are some *-native() and *-emulated() subs kept for later benchmarks when
# perl evolves.
#TODO There are some *-native() and *-emulated() subs kept for later benchmarks
# when perl evolves.

use BSON;
use BSON::ObjectId;
use BSON::Regex;
use BSON::Javascript;
use BSON::Binary;

#-------------------------------------------------------------------------------
unit package BSON:auth<https://github.com/MARTIMM>;

#-------------------------------------------------------------------------------
# BSON type codes
#
constant C-DOUBLE = 0x01;
constant C-STRING = 0x02;
constant C-DOCUMENT = 0x03;
Expand All @@ -38,14 +38,12 @@ constant C-MAX-KEY = 0x7F;

#-------------------------------------------------------------------------------
# Fixed sizes
#
constant C-INT32-SIZE = 4;
constant C-INT64-SIZE = 8;
constant C-DOUBLE-SIZE = 8;

#-------------------------------------------------------------------------------
#class Document does Associative does Positional {
class Document does Associative does Positional {
class Document does Associative {

subset Index of Int where $_ >= 0;

Expand All @@ -66,7 +64,6 @@ class Document does Associative does Positional {

#-----------------------------------------------------------------------------
# Make new document and initialize with a list of pairs
#
#TODO better type checking: List $pairs where all($_) ~~ Pair
#TODO better API
multi method new ( List $pairs, *%h ) {
Expand Down Expand Up @@ -240,11 +237,6 @@ class Document does Associative does Positional {
return $perl;
}

#-----------------------------------------------------------------------------
# submethod WHAT ( --> BSON::Document ) {
# BSON::Document;
# }

#-----------------------------------------------------------------------------
submethod Str ( --> Str ) {
self.perl;
Expand Down Expand Up @@ -534,54 +526,6 @@ class Document does Associative does Positional {
);
}

#`{{
#-----------------------------------------------------------------------------
# Positional role methods
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
method AT-POS ( Index $idx --> Any ) {
$idx < @!keys.elems ?? @!values[$idx] !! Any;
}
#-----------------------------------------------------------------------------
method EXISTS-POS ( Index $idx --> Bool ) {
$idx < @!keys.elems;
}
#-----------------------------------------------------------------------------
method DELETE-POS ( Index $idx --> Any ) {
$idx < @!keys.elems ?? (self{@!keys[$idx]}:delete) !! Nil;
}
#-----------------------------------------------------------------------------
method ASSIGN-POS ( Index $idx, $new! --> Nil ) {
# If index is at a higher position then the last one then only extend
# one place (like a push) with a generated key name such as key21 when
# [21] was used. Furthermore when a key like key21 has been used
# before the array is not extended but the key location is used
# instead.
#
my $key = $idx >= @!keys.elems ?? 'key' ~ $idx !! @!keys[$idx];
self{$key} = $new;
}
#-----------------------------------------------------------------------------
# Cannot use binding because when value changes the object cannot know that
# the location is changed. This is nessesary to encode the key, value pair.
#
method BIND-POS ( Index $idx, \new ) {
die X::BSON::Parse-document.new(
:operation("\$d[$idx] := {new}")
:error("Cannot use binding")
);
}
}}

#-----------------------------------------------------------------------------
# Must be defined because of Positional and Associative sources of of()
#-----------------------------------------------------------------------------
Expand Down
93 changes: 0 additions & 93 deletions t/300-document.t
Original file line number Diff line number Diff line change
Expand Up @@ -125,58 +125,6 @@ subtest "Test document, associative", {
}
}

#`{{
#-------------------------------------------------------------------------------
subtest {
my BSON::Document $d .= new: ('a' ... 'z') Z=> 120..145;
is $d[0], 120, "\$d[0] = $d[0]";
is $d[1], 121, "\$d[1] = $d[1]";
$d[1] = 2000;
is $d[1], 2000, "assign \$d[1] = $d[1]";
is $d<b>, 2000, "assign \$<b> = \$d[1] = $d[1]";
is $d<b>, $d[$d.find-key('b')],
"Same values on key 'b'($d<b>) and found index {$d.find-key('b')}($d[1])";
ok $d[0]:exists, "First pair $d[0] exists";
$d[1000] = 'text';
is $d[26], 'text', "assign \$d[1000] = \$d[26] = '$d[26]'";
is $d<key1000>, 'text', "assign \$d<key1000> = \$d[26] = '$d[26]'";
is $d.find-key(1000), 'key1000', "Get key from index 1000";
is $d[2000], Any, "Any undefined field returns 'Any'";
ok $d[26]:exists, '$d[26] exists';
ok ! ($d[27]:exists), '$d[27] does not exist';
is $d[25]:delete, 145, '$d[25] deleted was 145';
ok $d[25]:exists, '$d[25] does still exist, shifted from \$d[26]';
is $d[25], 'text', "\$d[25] = '$d[25]'";
ok ! ($d[26]:exists), '$d[26] does not exist anymore';
try {
is $d[4], 124, "Pre binding: \$d[4] = $d[4]";
my $x = 10;
$d[4] := $x;
is $d[4], 10, "Bound: \$d[4] = $d[4] == \$x = $x";
$x = 11;
is $d[4], 11, "Bound: \$d[4] = $d[4] == \$x = $x";
CATCH {
when X::BSON::Parse-document {
my $s = ~$_;
$s ~~ s:g/\n//;
ok .message ~~ ms/'Cannot' 'use' 'binding'/, $s;
}
}
}
}, "Test document, positional";
}}

#-------------------------------------------------------------------------------
subtest "Test document, other", {

Expand Down Expand Up @@ -205,37 +153,6 @@ subtest "Document nesting 1", {

is $d<c><b>, 110, "\$d<c><b> = $d<c><b>";
is $d<c><p>, 1, "\$d<c><p> = $d<c><p>";

#`{{
is $d<c>[1], 2, "\$d<c>[1] = $d<c>[1]";
is $d<c>[3], 110, "\$d<c>[3] = $d<c>[3]";
is $d[2][2], 100, "\$d[2][2] = $d[2][2]";
is $d[2][3], 110, "\$d[2][3] = $d[2][3]";
is $d[1][0], 11, "\$d[1][0] = $d[1][0]";
is $d[1][0][0], 11, "\$d[1][0][0] = $d[1][0][0]";
try {
say $d[1][2];
CATCH {
when X::OutOfRange {
ok .message ~~ m/'Index out of range. Is: 2, should be in 0..0'/,
'$d[1][2]: ' ~ $_;
}
}
}
try {
is $d[2][5], Any, '$d[2][5]: not out of range but not defined';
CATCH {
when X::OutOfRange {
ok .message ~~ m/'Index out of range. Is: 2, should be in 0..0'/,
'$d[2][5]: ' ~ $_;
}
}
}
}}
}

#-------------------------------------------------------------------------------
Expand Down Expand Up @@ -281,8 +198,6 @@ subtest "Exception tests", {
$d.encode;
CATCH {
#say .WHAT;
#.say;
when X::BSON::Parse-document {
my $m = .message;
$m ~~ s:g/\n//;
Expand All @@ -297,8 +212,6 @@ subtest "Exception tests", {
$d.encode;
CATCH {
#say .WHAT;
#.say;
when X::BSON::Parse-document {
my $m = .message;
$m ~~ s:g/\n//;
Expand Down Expand Up @@ -380,7 +293,6 @@ subtest "Exception tests", {
$d.encode;
CATCH {
#.say;
when X::BSON::NYS {
my $m = .message;
$m ~~ s:g/\n//;
Expand Down Expand Up @@ -426,11 +338,6 @@ subtest "Exception tests", {
}
}
}
# my BSON::Document $d .= new;
# $d( 1, 2, 'test', ( ('a' ... 'd') Z=> 20 .. 13), :w<fd>);
}
#-------------------------------------------------------------------------------
Expand Down
80 changes: 2 additions & 78 deletions t/310-document.t
Original file line number Diff line number Diff line change
Expand Up @@ -51,88 +51,12 @@ subtest {
0x10, 0x62, 0x00, 0x67, 0x0a, 0x00, 0x00, # 10 'b' 2663
0x00
);
#say $d.perl;

$edoc = $d.encode;
is-deeply $edoc, $etst, 'Encoded document still correct after addition';

}, "Document encoding associative";

#`{{
#-------------------------------------------------------------------------------
subtest {
my BSON::Document $d .= new;
$d[0] = 122;
$d[1] = 123;
$d[2] = 124;
$d[3] = 125;
is $d[2], 124, "\$d[2] = $d[2]";
is $d<key2>, 124, "\$d<key2> = $d<key2>";
my Buf $etst = Buf.new(
0x2d, 0x00 xx 3,
0x10, 0x6b, 0x65, 0x79, 0x30, 0x00, 0x7a, 0x00 xx 3, # 10 'key0' 122
0x10, 0x6b, 0x65, 0x79, 0x31, 0x00, 0x7b, 0x00 xx 3, # 10 'key1' 123
0x10, 0x6b, 0x65, 0x79, 0x32, 0x00, 0x7c, 0x00 xx 3, # 10 'key2' 124
0x10, 0x6b, 0x65, 0x79, 0x33, 0x00, 0x7d, 0x00 xx 3, # 10 'key3' 125
0x00
);
my Buf $edoc = $d.encode;
is-deeply $edoc, $etst, 'Encoded document is correct';
$d[2]:delete;
$etst = Buf.new(
0x23, 0x00 xx 3,
0x10, 0x6b, 0x65, 0x79, 0x30, 0x00, 0x7a, 0x00 xx 3, # 10 'key0' 122
0x10, 0x6b, 0x65, 0x79, 0x31, 0x00, 0x7b, 0x00 xx 3, # 10 'key1' 123
0x10, 0x6b, 0x65, 0x79, 0x33, 0x00, 0x7d, 0x00 xx 3, # 10 'key3' 125
0x00
);
$edoc = $d.encode;
is-deeply $edoc, $etst, 'Encoded document is correct after deletion';
# Generated key is key3. Already there so modifies instead of adding.
#
$d[3] = 10;
is $d[2], 10, "\$d[2] = $d[2]";
is $d<key3>, 10, "\$d<key3> = $d<key3>";
$etst = Buf.new(
0x23, 0x00 xx 3,
0x10, 0x6b, 0x65, 0x79, 0x30, 0x00, 0x7a, 0x00 xx 3, # 10 'key0' 122
0x10, 0x6b, 0x65, 0x79, 0x31, 0x00, 0x7b, 0x00 xx 3, # 10 'key1' 123
0x10, 0x6b, 0x65, 0x79, 0x33, 0x00, 0x0a, 0x00 xx 3, # 10 'key3' 10
0x00
);
$edoc = $d.encode;
is-deeply $edoc, $etst, 'Encoded document is correct after modifying';
# Generated key is key20. Not there so adds. But only after adding a new entry
#
$d<a> = 1;
$d[20] = 11;
is $d[4], 11, "\$d[4] = $d[4]";
is $d<key20>, 11, "\$d<key20> = $d<key20>";
$etst = Buf.new(
0x35, 0x00 xx 3,
0x10, 0x6b, 0x65, 0x79, 0x30, 0x00, 0x7a, 0x00 xx 3, # 10 'key0' 122
0x10, 0x6b, 0x65, 0x79, 0x31, 0x00, 0x7b, 0x00 xx 3, # 10 'key1' 123
0x10, 0x6b, 0x65, 0x79, 0x33, 0x00, 0x0a, 0x00 xx 3, # 10 'key3' 10
0x10, 0x61, 0x00, 0x01, 0x00 xx 3, # 10 'a' 1
0x10, 0x6b, 0x65, 0x79, 0x32, 0x30, 0x00, 0x0b, 0x00 xx 3,
# 10 'key20' 11
0x00
);
$edoc = $d.encode;
is-deeply $edoc, $etst, 'Encoded document is correct after adding';
}, "Document encoding positional";
}}

#-------------------------------------------------------------------------------
subtest {
my BSON::Document $d .= new;
Expand All @@ -145,7 +69,7 @@ subtest {
0x00
);
$d.decode($new-data);

is $d<key0>, 122, "\$d<key0> => $d<key0>";

}, "Document decoding";
Expand Down
Loading

0 comments on commit af5ef67

Please sign in to comment.