Token导航 LogoToken导航TokenDH.com
研究检索只读github未标认证来源可访问许可证需确认审计通过

motoko-performance-optimizationsmotoko 性能优化

Agent Skill

motoko-performance-optimizations 用于查找、检索和筛选相关信息,适合在 Codex、Claude、Cursor、Gemini CLI 中需要根据关键词、任务场景或来源线索快速定位候选结果时使用。可结合来源仓库、安装命令和原始 README 继续核验具体用法。安装前建议确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写。

总安装

225

周安装

9

GitHub Stars

公开资料未说明

下载量

73
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:motoko-performance-optimizations(motoko 性能优化)
来源仓库:https://github.com/research-ag/motoko-skills
仓库路径:skills/motoko-performance-optimizations
安装命令:
npx skills add https://github.com/research-ag/motoko-skills --skill motoko-performance-optimizations
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/research-ag/motoko-skills --skill motoko-performance-optimizations

简介

motoko-performance-optimizations 用于查找、检索和筛选相关信息。

  • 适合根据关键词、任务场景或来源线索快速定位候选结果。
  • 可通过 npx skills add 命令从指定仓库安装,需结合原始 README 核验用法。
  • 安装前建议确认权限范围、维护状态及是否触发联网或文件读写。
  • 适用宿主包括 Codex、Claude、Cursor、Gemini CLI,接入前应确认版本、权限和运行环境要求。

SKILL.md

Motoko Performance Optimizations

What This Is

An extensible guide for speeding up Motoko code safely and predictably. It focuses on mechanical, behavior-preserving improvements: allocation reduction, fixed-width arithmetic, block processing, efficient Text building, and clear loop shapes. Use this skill when you want to improve throughput/latency without changing semantics.

  • Benchmarking details and harnesses live in: skills/benchmarks-generation/SKILL.md
  • Style and safe refactors that often precede perf work: skills/code-improvements/SKILL.md
  • Dot-notation improvements that reduce verbosity/overhead: skills/dot-notation-migration/SKILL.md

Quick Wins (General)

  • Minimize allocations in hot paths

- Avoid materializing entire buffers just to iterate (e.g., prefer indexing Blob directly). - Reuse lengths and capacities; cache size() calls to a local variable.

  • Prefer fixed-width arithmetic in tight loops

- Keep shifts/masks on Nat32/Nat64 intermediates; avoid widening to Nat mid-loop.

  • Build Text in larger chunks

- Avoid per-character Text.fromChar + many small # appends; emit ASCII/UTF‑8 blocks and append once per block.

  • Shape loops for the steady state

- Process uniform blocks in the main loop; handle a tiny tail separately. - Hoist invariants (sizes, constants) and keep the inner loop straight-line.

  • Verify after each change

- Rebuild, run unit tests/property checks, then benchmark representative inputs.

Technique Areas Overview

  • Byte/Bit Hot‑Path Techniques (detailed below)
  • Allocation & Memory Management (coming soon)
  • Text/String Building Patterns (coming soon)
  • Numeric/Arithmetic & Fixed‑Width Ops (coming soon)
  • Data Structures & Algorithms (coming soon)
  • Async/Await & Inter‑canister Patterns (coming soon)
  • Candid/Serialization Efficiency (coming soon)
  • Caching & Memoization (coming soon)

Related Skills

  • Benchmarks: skills/benchmarks-generation/SKILL.md
  • Code Quality Cleanup: skills/code-improvements/SKILL.md
  • Dot‑notation Migration: skills/dot-notation-migration/SKILL.md
  • General Style: skills/motoko-general-style-guidelines/SKILL.md

Byte/Bit Hot‑Path Techniques

These techniques target code that iterates over Blob or [Nat8], performs bit packing/unpacking, or emits Text (e.g., encoders/decoders, checksums, binary parsers). They reduce allocations, avoid expensive integer widening, and reshape loops for better throughput while preserving correctness.

When To Use

  • You see hot loops over bytes and bit operations
  • Code constructs Text character-by-character or via many small concatenations
  • Blob is converted to [Nat8] only to iterate or index

Prerequisites

  • Motoko compiler (moc) at a reasonably recent version (1.3.0+ recommended)
  • Familiarity with fixed-width integer types (Nat8, Nat16, Nat32, Nat64) and wrapping arithmetic (+%)
  • For benchmarking guidance, see: skills/benchmarks-generation/SKILL.md

Quick Wins Checklist (Heuristics)

Scan candidate code for these patterns and replace accordingly:

  • Blob handling

- If you see let bytes = Blob.toArray(data) just to iterate or index → Prefer indexing Blob directly: data[i]. - Cache data.size() to a local; choose an appropriate width (often Nat64).

  • Integer conversions & bit ops

- Avoid widening Nat8 -> Nat (arbitrary precision) for bit operations. - Use staged widening on fixed-width types: b.toNat16().toNat32() before shifts and masks. - Perform <<, >>, &, | on Nat32/Nat64 intermediates, then narrow at the end if needed. - Use wrapping arithmetic +% for loop indices when overflow cannot occur by invariant; prefer a wide counter (e.g., Nat64).

  • Alphabets / lookup tables

- Avoid Char/Text tables in hot paths. Store alphabets as [Nat8] (UTF-8/ASCII codes) for direct byte emission.

  • Text construction

- Avoid per-character Text.fromChar plus repeated # concatenations. - Emit text in blocks: build a small ASCII Blob (e.g., 4–8 bytes), Text.decodeUtf8 once, then append once per block. - Minimize # operations; 1 append per 4–8 output chars is far cheaper than many appends.

  • Loop shape

- Process input in uniform blocks (e.g., 3 bytes → 4 chars), then handle a small tail (0–2 bytes) separately. - Hoist invariants (sizes, constants); keep inner loops straight-line.

  • Correctness guards

- For theoretically unreachable states (e.g., ASCII decode failure), insert a Prim.trap("…") with a clear message. - Double-check off-by-one errors at block boundaries (e.g., next_i <= sz).

Before → After Patterns

  1. Avoid Blob→Array conversion
// Before
let bytes = Blob.toArray(data);
var i = 0;
let b1 = bytes[i];

// After
let sz = Nat64.fromIntWrap(data.size());
var i : Nat64 = 0;
let b1 = data[i.toNat()];
  1. Prefer fixed-width ints and staged widening
// Before (goes through arbitrary-precision Nat)
let n = (Nat32.fromNat(Nat8.toNat(b1)) << 16)
      | (Nat32.fromNat(Nat8.toNat(b2)) << 8)
      |  Nat32.fromNat(Nat8.toNat(b3));

// After (fixed-width path)
let n = (b1.toNat16().toNat32() << 16)
      | (b2.toNat16().toNat32() << 8)
      |  b3.toNat16().toNat32();
  1. Replace Char/Text alphabet + per‑char concat with [Nat8] + block decode
// Before
private let alphabet : [Char] = ['A', 'B', /*…*/, '/'];
let c1 = Text.fromChar(alphabet[idx1]);
let c2 = Text.fromChar(alphabet[idx2]);
result #= c1 # c2 # c3 # c4; // many small concats

// After
private let alphabet : [Nat8] = [65, 66, /*…*/, 47]; // ASCII bytes
let bytes = Blob.fromArray([
  alphabet[idx1], alphabet[idx2], alphabet[idx3], alphabet[idx4]
]);
switch (Text.decodeUtf8(bytes)) {
  case (?t) { result := result # t } // one append per block
  case (_)  { Prim.trap("Cannot happen: Utf8 decode error …") }
};
  1. Blocked processing with tail
// Example: main loop over full blocks, then a small tail path
var i : Nat64 = 0;
var next_i : Nat64 = block; // e.g., 3, 6, etc.
while (next_i <= sz) {
  // read <block> bytes, produce <k> output chars
  i := next_i; next_i +%= block;
};
while (i < sz) {
  // read remaining bytes (tail), produce padded output as needed
  i +%= tailStep;
};

Step‑by‑Step Procedure (Agent Playbook)

  1. Scoping

- Identify hot byte/bit paths: encoders/decoders, hashing, binary parsers. - If needed, prepare a small benchmark harness (see skills/benchmarks-generation/SKILL.md).

  1. Baseline

- Run a benchmark on realistic inputs (sequential bytes, random/mixed); record results.

  1. Transformations (apply incrementally; keep each commit focused)

- Remove Blob.toArray used solely for iteration or random access; index Blob directly. - Cache sizes; switch to Nat64 index; use +% where safe. - Replace Char/Text alphabets with [Nat8] tables. - Convert per‑char concatenations into block emission using Blob.fromArray + Text.decodeUtf8. - Use Nat16/Nat32/Nat64 intermediates for shifts and masks; avoid Nat widening in hot loops. - Reshape loops into large uniform blocks plus a small tail path. - Eliminate dead temporaries; prefer straight‑line code. - Add explicit Prim.trap for logically unreachable decode errors.

  1. Validation

- Unit tests: small ASCII examples, padding/edge cases, long sequential bytes; assert output length and alphabet membership where relevant. - Optional property checks: random inputs vs a reference implementation. - Benchmarks: verify improvements or parity across patterns and sizes (use skills/benchmarks-generation/SKILL.md).

Common Pitfalls

  1. Unnecessary widening to Nat in tight loops

- Why it hurts: arbitrary-precision arithmetic is slower and allocates. - Fix: keep operations on Nat32/Nat64 intermediates; stage Nat8 -> Nat16 -> Nat32.

  1. Per-character Text operations

- Why it hurts: Text.fromChar + repeated # creates many small allocations. - Fix: emit ASCII bytes into a Blob block and Text.decodeUtf8 once per block, then append once.

  1. Materializing Blob as [Nat8] to iterate

- Why it hurts: full-buffer allocation and copy on the hot path. - Fix: index Blob directly and cache size().

  1. Overflow checks on loop counters

- Why it hurts: extra checks per iteration when they are provably unnecessary. - Fix: widen counter to Nat64 and use +% under a clear no-overflow invariant.

  1. Off-by-one at block boundaries

- Symptom: traps or incorrect output near the end of input. - Fix: use next_i <= sz for the main loop; handle the remaining tail explicitly.

Safety & Edge Cases

  • Wrapping arithmetic: Use +% only when an invariant guarantees no overflow in the chosen width; prefer widening to Nat64.
  • Text.decodeUtf8: Safe for ASCII bytes (e.g., codec alphabets and = padding). Keep a defensive Prim.trap for auditability.
  • Bounds: Cleanly separate the fast block loop and the tail; test edge sizes (e.g., 0, 1, 2, 3, 5, 6, 7 depending on block size).
  • Allocation profile: Ensure you removed buffer materialization (Blob.toArray) and minimized concatenations.

Validation Checklist (copy/paste)

  • Tests cover: empty, minimal inputs, pad edges, long sequential bytes, multi-sentence ASCII where applicable.
  • Output length formula holds (as defined by the codec or algorithm).
  • Alphabet membership check passes; no stray chars (for encoders/decoders).
  • Bench shows improvement or parity; no regressions for mixed vs zero patterns (see skills/benchmarks-generation/SKILL.md).
  • No traps at block boundaries; indices don’t overflow.

Commit Message Templates (optional)

Keep one change per commit when possible. Examples:

  • Skip conversion from Blob to Array in encoder.
  • Avoid unnecessary Char→Text conversion; use [Nat8] alphabet.
  • Reuse data.size() and use Nat64 indices.
  • Convert Nat8 to Nat32 via Nat16 for bit ops.
  • Process input in uniform blocks and handle tail separately.
  • Use +% for loop index under explicit invariant; widen index width.
  • Replace many small # concatenations with a single append of a decoded block.
  • Remove temporary c1..c4 variables in tight loops.
  • Trap in unreachable decodeUtf8 error path.

Outcome

Applying these techniques typically yields:

  • Fewer allocations by avoiding full-buffer materialization and per-char Text ops
  • Faster bit-packing/unpacking via fixed-width arithmetic
  • Reduced concatenation overhead by emitting larger Text chunks
  • Clearer separation of fast-path block processing and tail handling
  • Safer, more auditable code via explicit invariants and traps

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

36.74%
按下载量换算27

Claude

31.05%
按下载量换算23

Cursor

18.72%
按下载量换算14

Gemini CLI

9.87%
按下载量换算7

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

只读

该 Skill 主要提供规则、说明或参考内容,本身偏只读;真正读写文件、联网或执行命令仍取决于宿主 Agent 的任务。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills