<commit_before>.. _jitclass:

=======================================
Compiling python classes with @jitclass
=======================================

.. note::

  This is a early version of jitclass support. Not all compiling features are
  exposed or implemented, yet.


Numba supports code generation for classes via the :func:`numba.jitclass`
decorator.  A class can be marked for optimization using this decorator along
with a specification of the types of each field.  We call the resulting class
object a jitclass.  All methods of a jitclass is compiled into nopython
functions.  The data of a jitclass instance is allocated on the heap as a
C-compatible structure so that any compiled functions can have direct access
to the underlying data, bypassing the interpreter.


Basic usage
===========

Here's an example of a jitclass::

  import numpy as np
  from numba import jitclass          # import the decorator
  from numba import int32, float32    # import the types

  spec = [
      ('value', int32),               # a simple scalar field
      ('array', float32[:]),          # an array field
  ]

  @jitclass(spec)
  class Bag(object):
      def __init__(self, value):
          self.value = value<commit_msg>fix a small typo in jitclass docs<commit_after>.. _jitclass:

=======================================
Compiling python classes with @jitclass
=======================================

.. note::

  This is a early version of jitclass support. Not all compiling features are
  exposed or implemented, yet.


Numba supports code generation for classes via the :func:`numba.jitclass`
decorator.  A class can be marked for optimization using this decorator along
with a specification of the types of each field.  We call the resulting class
object a jitclass.  All methods of a jitclass are compiled into nopython
functions.  The data of a jitclass instance is allocated on the heap as a
C-compatible structure so that any compiled functions can have direct access
to the underlying data, bypassing the interpreter.


Basic usage
===========

Here's an example of a jitclass::

  import numpy as np
  from numba import jitclass          # import the decorator
  from numba import int32, float32    # import the types

  spec = [
      ('value', int32),               # a simple scalar field
      ('array', float32[:]),          # an array field
  ]

  @jitclass(spec)
  class Bag(object):
      def __init__(self, value):
          self.value = value
{'fraction_non_alphanumeric': 0.12393681652490887, 'fraction_numerical': 0.0064803564196030785, 'mean_word_length': 3.4827586206896552, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4599596', 'n_tokens_mistral': 665, 'n_tokens_neox': 625, 'n_words': 307}