Giter VIP home page Giter VIP logo

Comments (9)

Uzlopak avatar Uzlopak commented on June 24, 2024

Well. we could reduce the for loop overhead by doing 4 masking operations instead of one.

from undici.

ronag avatar ronag commented on June 24, 2024

As I indicated earlier. The masking is as far as I know totally useless for backend and could just be skipped...

from undici.

Uzlopak avatar Uzlopak commented on June 24, 2024

Maybe instead of i + 4 < length, you precalculate that too

const lengthFor4 = length - (length & 3)

from undici.

tsctx avatar tsctx commented on June 24, 2024
import { randomBytes } from "crypto";
import { group, bench, run } from "mitata";

function maskForOne(mask, buffer) {
  const alloc = Buffer.allocUnsafe(buffer.length);
  for (let i = 0; i < buffer.length; ++i) {
    alloc[i] = buffer[i] ^ mask[i & 3];
  }
  return alloc;
}

function maskForFour(mask, buffer) {
  const length = buffer.length;
  const alloc = Buffer.allocUnsafe(buffer.length);
  const lengthFor4 = length - (length & 3);
  if (length > 3) {
    for (let i = 0; i < lengthFor4; i += 4) {
      alloc[i] = buffer[i] ^ mask[0];
      alloc[i + 1] = buffer[i + 1] ^ mask[1];
      alloc[i + 2] = buffer[i + 2] ^ mask[2];
      alloc[i + 3] = buffer[i + 3] ^ mask[3];
    }
  }
  for (let i = lengthFor4; i < length; ++i) {
    alloc[i] = buffer[i] ^ mask[i & 3];
  }
  return alloc;
}

group("mask", () => {
  const buffer = new Uint8Array(randomBytes(1024 * 4).buffer);
  const mask = new Uint8Array(randomBytes(4).buffer);
  bench("for 1", () => maskForOne(mask, buffer));
  bench("for 4", () => maskForFour(mask, buffer));
});

await run();
• mask
------------------------------------------------- -----------------------------
for 1      13'381 ns/iter     (6'200 ns … 536 µs)  7'400 ns    123 µs    188 µs
for 4       9'444 ns/iter   (5'800 ns … 4'131 µs)  7'000 ns    122 µs    197 µs

summary for mask
  for 4
   1.42x faster than for 1

from undici.

Uzlopak avatar Uzlopak commented on June 24, 2024

maybe for the last potential 3 operations a duffs device?

from undici.

tsctx avatar tsctx commented on June 24, 2024

yes?

from undici.

Uzlopak avatar Uzlopak commented on June 24, 2024

Something like this. Sry, I still suffer from a heat stroke and cant provide better code.

switch (length & 3) {
  case 3:
    buffer[i + 3] ^ mask[3]
  case 2:
    buffer[i + 2] ^ mask[2]
  case 2:
    buffer[i + 1] ^ mask[1]
  ...

from undici.

tsctx avatar tsctx commented on June 24, 2024

Take care and get well soon.

from undici.

tsctx avatar tsctx commented on June 24, 2024

I give this up

from undici.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.