amphp / dns

Async DNS resolution for PHP based on Amp.
https://amphp.org/dns
MIT License
157 stars 32 forks source link

Remove amphp/file dependency #78

Closed ostrolucky closed 5 years ago

ostrolucky commented 5 years ago

I'm only using amphp/socket and there is following dependency tree that is useless to me: amphp/socker -> amphp/file -> amphp/sync, amphp/parallel

So after removing this dependency, my consumers will have to install 3 packages less.

Additionally, replacing it with bytestream increases performance almost 10x!

Index: bench/HostLoaderBench.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- bench/HostLoaderBench.php   (date 1545833204000)
+++ bench/HostLoaderBench.php   (date 1545833204000)
@@ -0,0 +1,17 @@
+<?php
+
+namespace Amp\Dns\Bench;
+
+use Amp\Dns\HostLoader;
+use Amp\Loop;
+
+class HostLoaderBench
+{
+    public function benchHostLoader()
+    {
+        Loop::run(function () {
+            $loader = new HostLoader(__DIR__ . "/../test/data/hosts");
+            yield $loader->loadHosts();
+        });
+    }
+}
Index: lib/HostLoader.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- lib/HostLoader.php  (revision 836b0896b0105298373669754ece8ad8577c0854)
+++ lib/HostLoader.php  (date 1545834232000)
@@ -2,6 +2,7 @@

 namespace Amp\Dns;

+use Amp\ByteStream\ResourceInputStream;
 use Amp\File;
 use Amp\Promise;
 use Amp\Uri\InvalidDnsNameException;
@@ -26,7 +27,8 @@
             $data = [];

             try {
-                $contents = yield File\get($this->path);
+                $resource = new ResourceInputStream(fopen($this->path, 'r'), filesize($this->path));
+                $contents = yield $resource->read();
             } catch (File\FilesystemException $e) {
                 return [];
             }

Without this change:

benchHostLoader               I99 P0    [μ Mo]/r: 404.861 400.640 (μs)  [μSD μRSD]/r: 16.964μs 4.19%

1 subjects, 100 iterations, 100 revs, 0 rejects, 0 failures, 0 warnings
(best [mean mode] worst) = 376.960 [404.861 400.640] 490.050 (μs)
⅀T: 40,486.090μs μSD/r 16.964μs μRSD/r: 4.190%

With this change:

benchHostLoader               I99 P0    [μ Mo]/r: 46.553 44.866 (μs)    [μSD μRSD]/r: 2.447μs 5.26%

1 subjects, 100 iterations, 100 revs, 0 rejects, 0 failures, 0 warnings
(best [mean mode] worst) = 43.520 [46.553 44.866] 54.270 (μs)
⅀T: 4,655.250μs μSD/r 2.447μs μRSD/r: 5.257%