Skip to content

Instantly share code, notes, and snippets.

View kekyo's full-sized avatar
📡
I'm looking for a job

Kouji Matsui kekyo

📡
I'm looking for a job
View GitHub Profile
@kekyo
kekyo / mysetjmp.asm
Created February 10, 2019 14:43
Self experiment implementation for the setjmp/longjmp at the x86-64 and masm platform.
.code
; struct
; {
; uint64_t Frame;
; uint64_t Rbx;
; uint64_t Rsp;
; uint64_t Rbp;
; uint64_t Rsi;
; uint64_t Rdi;
@kekyo
kekyo / MainWindow.xaml
Created April 6, 2019 09:43
DelegateCommand on WPF
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Button x:Name="button">
TEST
@kekyo
kekyo / make.bat
Created April 8, 2019 13:07
Embedding subtitles into mp4 container using ffmpeg
setlocal EnableDelayedExpansion
for %%p in (original\*.mp4) do (
rem echo "%%p" "%%~dp%%~pp%%~np.srt"
..\..\ffmpeg\bin\ffmpeg.exe -i "%%p" -i "%%~dp%%~pp%%~np.en.srt" -map 0:v -map 0:a -map 1:s -metadata:s:s:0 language=eng -c:v copy -c:a copy -c:s mov_text "%%~np.mp4"
)
@kekyo
kekyo / Program.cs
Created May 20, 2019 14:36
Extract member symbol names, include kanji dot notation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace ExtractSymbols
{
class Program
{
@kekyo
kekyo / task.md
Last active July 8, 2019 00:51
Task.Runで非同期APIをシミュレートすべきかどうかの判断

朝バタバタしてクリアじゃなかった... ちょっと間違ってましたこういうことです:

  • 多数同時並行するタスクがある場合
    • Windowsの場合はI/O完了ポートの「ブロックされたスレッドを検出するとワーカースレッドを世に放つ」機能があるので、同期APIがブロックしても、ワーカースレッド上限まではOK(ThreadPool.SetMaxThreadsの第二引数completionPortThreads)。
    • Windowsではない環境では必ず上記機能が使えるとは言えない(多分単純スレッドプール)ので、同期APIによるスレッドブロックが発生すると、同時並行動作数はそれ以上伸びない。これを避けたい場合、Task.Run を使わざるを得ない。
  • 多数同時並行したいタスクがない、GUIの場合
    • 同期APIでブロックすると、それがUIスレッドの場合はUIが固まってしまうので、それを避けたいなら Task.Run を使わざるを得ない。但し、継続処理がUIを操作する場合はUIスレッドにマーシャリングすることになり、そのコストはかなり高いことに注意(≒await後の処理でUI部品を操作する場合など)。
  • 多数同時並行したいタスクがない場合
    • 同期APIと非同期APIを比較すると、非同期APIのほうが余分なハンドリングを必要とするコストがあるので、この場合は同期APIをそのまま使うのがいい。

Taskを使うかValueTaskを使うかによる差については、本来はすべてをTaskを返す非同期APIとして定義したいが、ほとんどの場合同期的に完了してしまう(つまり一瞬で操作が完了する、待機する可能性が0ではないが殆ど待機しないというような)処理を、いつもTaskで管理するのはコストが高いので、代わりにValueTaskを使って初めから完了しているものはコストをほとんど0とするようにするという話です。

@kekyo
kekyo / Program.cs
Last active August 24, 2019 21:57
Awaiting on Monitor lock vs. AsyncEx
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Nito.AsyncEx;
namespace ConsoleApp4
{
public static class Program
@kekyo
kekyo / Program.cs
Created August 24, 2019 21:50
Continuation vs Awaiting.
using System;
using System.Threading.Tasks;
namespace ConsoleApp5
{
public static class Program
{
private static async Task ContinuationWithAwaitingSimple()
{
Console.WriteLine("Before");
@kekyo
kekyo / MainWindow.xaml
Last active August 25, 2019 13:52
Async operation in WPF
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<DockPanel>
<Button DockPanel.Dock="Bottom" Click="Button_Click" Content="Append" />
<TextBlock x:Name="input" />
@kekyo
kekyo / MainWindow.xaml
Created August 25, 2019 14:14
Causes reentrant by invalid monitor lock usage with async/await method.
<Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<DockPanel>
<Button DockPanel.Dock="Bottom" Click="Button_Click" Content="Append" />
<TextBlock x:Name="input" />
@kekyo
kekyo / Program.cs
Created October 1, 2019 04:55
Generic covariance in C#
using System;
using System.Collections.Generic;
namespace ConsoleApp6
{
class Program
{
public interface IBar
{
// ....