Answers.Body: <p>It is actually!</p>
  <p>Some engines have what are known as fully legal move generators, the kind of move generator you're talking about, where only completely legal moves are returned.</p>
  <p>This approach is fine, but not every move is actually tested during the search phase, so it's actually (generally) more efficient to test move legality on a move-by-move basis.</p>
  <p>This is the approach used in many of the current top chess engines like <a href="http://ccrl.chessdom.com/ccrl/4040/" rel="nofollow noreferrer">Stockfish, Etheral, and Koivisto</a>. For example, <a href="https://github.com/official-stockfish/Stockfish/blob/master/src/search.cpp#L875" rel="nofollow noreferrer">here's the relevant code in Stockfish that does this, during the search phase</a>:</p>
  <pre><code>while ((move = mp.next_move()) != MOVE_NONE)
      if (move != excludedMove &amp;&amp; pos.legal(move))
      {
          ...
  </code></pre>
  <p>Notice the call to <a href="https://github.com/official-stockfish/Stockfish/blob/master/src/position.cpp#L494" rel="nofollow noreferrer"><code>pos.legal</code></a>, which tests if a move is legal.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-04T17:21:39.077Z
Answers.Text: Wow, that makes so much more sense!
  
  Just wondering though - would it be too slow to test a move's legality by trying it out then checking, or is it worth it to implement the more complex method that stockfish uses?
Answers.UserDisplayName: AAce3
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-04T21:50:45.343Z
Answers.Text: Sure @AAce3, no problem! To answer your question, Stockfish's approach is generally faster; but many engines use the other method you described, [including my engine](https://github.com/algerbrex/blunder/blob/main/engine/search.go#L372), and it's not unacceptably slow. And further, while move generation speed is important, it actually takes up a relatively small percentage of the total search time, so the method you mentioned isn't particularly penalized either.
Answers.UserDisplayName: Chris
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-04T08:51:32.603Z
Answers.LastActivityDate: 2022-06-04T21:53:05.573Z
Answers.LastEditDate: 2022-06-04T21:53:05.573Z
Answers.UserDisplayName: Chris
Body: <p>Most engines have some method of filtering out pseudo-legal moves from the actual legal ones. However, is there any reason not to keep them? With bit-boards, you can quickly filter out the ones that result in the king landing in the attack mask of the opponent's side when trying a move at search-time. Am I missing something here, or is it faster to filter out before the search?</p>
  
Tags: <engines><illegal-move>
Title: Why isn't legal move filtering done at search time?
ThreadText: # Why isn't legal move filtering done at search time?
  
  (Asked by: username_0 on 2022-06-03)
  
  Most engines have some method of filtering out pseudo-legal moves from the actual legal ones. However, is there any reason not to keep them? With bit-boards, you can quickly filter out the ones that result in the king landing in the attack mask of the opponent's side when trying a move at search-time. Am I missing something here, or is it faster to filter out before the search?
  
  **Tags:** engines, illegal-move
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-06-04. Score: 7 | Accepted answer
  
  It is actually!
  
  Some engines have what are known as fully legal move generators, the kind of move generator you're talking about, where only completely legal moves are returned.
  
  This approach is fine, but not every move is actually tested during the search phase, so it's actually (generally) more efficient to test move legality on a move-by-move basis.
  
  This is the approach used in many of the current top chess engines like [Stockfish, Etheral, and Koivisto](http://ccrl.chessdom.com/ccrl/4040/). For example, [here's the relevant code in Stockfish that does this, during the search phase](https://github.com/official-stockfish/Stockfish/blob/master/src/search.cpp#L875):
  
  ```
  while ((move = mp.next_move()) != MOVE_NONE)
      if (move != excludedMove && pos.legal(move))
      {
          ...
  
  ```
  
  Notice the call to [`pos.legal`](https://github.com/official-stockfish/Stockfish/blob/master/src/position.cpp#L494), which tests if a move is legal.
  
  > **Comments:**
  > **username_0**: Wow, that makes so much more sense!  Just wondering though - would it be too slow to test a move's legality by trying it out then checking, or is it worth it to implement the more complex method that stockfish uses?  
  > **username_1**: Sure @username_0, no problem! To answer your question, Stockfish's approach is generally faster; but many engines use the other method you described, [including my engine](https://github.com/algerbrex/blunder/blob/main/engine/search.go#L372), and it's not unacceptably slow. And further, while move generation speed is important, it actually takes up a relatively small percentage of the total search time, so the method you mentioned isn't particularly penalized either.  
  
  ---
  
  