Skip to content

Commit

Permalink
Precompie Encoding::Builtin objects
Browse files Browse the repository at this point in the history
This was rather more involved than just prefixing with BEGIN, as .bless
does not appear to work at that point in the setting yet.  And passing
HLL Lists at that point, also doesn't work.  So use more primitive means
to create the Encoding::Builtin objects.
  • Loading branch information
lizmat committed Jun 20, 2017
1 parent fff4333 commit 79ce1a9
Showing 1 changed file with 56 additions and 26 deletions.
82 changes: 56 additions & 26 deletions src/core/Encoding/Builtin.pm
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
class Encoding::Builtin does Encoding {
has Str $.name;
has @.alternative-names;
has $!alternative-names;

method new() {
X::Cannot::New.new(class => self.WHAT).throw
}

method alternative-names() { @!alternative-names }
method SET-SELF(\name, \alternatives) {
nqp::stmts(
($!name := name),
($!alternative-names := alternatives),
self
)
}

method alternative-names() { $!alternative-names }

method decoder(*%options --> Encoding::Decoder) {
Encoding::Decoder::Builtin.new($!name, |%options)
Expand Down Expand Up @@ -35,27 +43,49 @@ class Encoding::Builtin does Encoding {
}
}

Encoding::Registry.register(Encoding::Builtin.bless(
:name<utf8>, :alternative-names<utf-8>
));
Encoding::Registry.register(Encoding::Builtin.bless(
:name<utf8-c8>, :alternative-names<utf-8-c8>
));
Encoding::Registry.register(Encoding::Builtin.bless(
:name<ascii>
));
Encoding::Registry.register(Encoding::Builtin.bless(
:name<iso-8859-1>, :alternative-names<
iso_8859-1:1987 iso_8859-1 iso-ir-100 latin1 latin-1 csisolatin1
l1 ibm819 cp819
>
));
Encoding::Registry.register(Encoding::Builtin.bless(
:name<windows-1252>
));
Encoding::Registry.register(Encoding::Builtin.bless(
:name<utf16>, :alternative-names<utf-16>
));
Encoding::Registry.register(Encoding::Builtin.bless(
:name<utf32>, :alternative-names<utf-32>
));
Encoding::Registry.register(
BEGIN nqp::create(Encoding::Builtin).SET-SELF(
"utf8", nqp::list("utf-8")

This comment has been minimized.

Copy link
@jnthn

jnthn Jun 20, 2017

Member

Why the nqp::list? The first thing that's going to happen is the registry will call alternative-names, which will then on-demand wrap it into a Perl 6 level List, causing a runtime allocation. Whereas if that was unchanged then it'd just make a List at compile time, and not having to do the runtime wrapping.

This comment has been minimized.

Copy link
@lizmat

lizmat Jun 20, 2017

Author Contributor

Taking the HLL route gives:

Stage parse : Cannot invoke this object (REPR: Null; VMNull)
at SETTING::src/core/Encoding/Builtin.pm:48 (:)

Also, an nqp::list is already upgraded by the time it reaches the sub, afaict:

$ 6 'use nqp; sub a(\a) { say a.^name }; a nqp::list(1,2,3)'
List

so there would be no runtime allocation?

This comment has been minimized.

Copy link
@jnthn

jnthn Jun 20, 2017

Member

Ah, OK, fair enough. The object invoke thing is weird though.

)
);
Encoding::Registry.register(
BEGIN nqp::create(Encoding::Builtin).SET-SELF(
"utf8-c8", nqp::list("utf-8-c8")
)
);
Encoding::Registry.register(
BEGIN nqp::create(Encoding::Builtin).SET-SELF(
"ascii", nqp::list
)
);
Encoding::Registry.register(
BEGIN nqp::create(Encoding::Builtin).SET-SELF(
"iso-8859-1",
nqp::list(
"iso_8859-1:1987",
"iso_8859-1",
"iso-ir-100",
"latin1",
"latin-1",
"csisolatin1:",
"l1",
"ibm819",
"cp819"
)
)
);
Encoding::Registry.register(
BEGIN nqp::create(Encoding::Builtin).SET-SELF(
"windows-1252", nqp::list
)
);
Encoding::Registry.register(
BEGIN nqp::create(Encoding::Builtin).SET-SELF(
"utf16", nqp::list("utf-16")
)
);
Encoding::Registry.register(
BEGIN nqp::create(Encoding::Builtin).SET-SELF(
"utf32", nqp::list("utf-32")
)
);

1 comment on commit 79ce1a9

@lizmat
Copy link
Contributor Author

@lizmat lizmat commented on 79ce1a9 Jun 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Precompile, rather :-)

Please sign in to comment.